mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-24 02:38:53 +01:00
example/blocks: Use gamepad
This commit is contained in:
parent
a3adf50850
commit
d2d32d3956
@ -30,22 +30,20 @@ type GameState struct {
|
||||
type Game struct {
|
||||
once sync.Once
|
||||
sceneManager *SceneManager
|
||||
input *Input
|
||||
input Input
|
||||
}
|
||||
|
||||
func NewGame() *Game {
|
||||
game := &Game{
|
||||
return &Game{
|
||||
sceneManager: NewSceneManager(NewTitleScene()),
|
||||
input: NewInput(),
|
||||
}
|
||||
return game
|
||||
}
|
||||
|
||||
func (game *Game) Update(r *ebiten.Image) error {
|
||||
game.input.Update()
|
||||
game.sceneManager.Update(&GameState{
|
||||
SceneManager: game.sceneManager,
|
||||
Input: game.input,
|
||||
Input: &game.input,
|
||||
})
|
||||
game.sceneManager.Draw(r)
|
||||
return nil
|
||||
|
110
example/blocks/blocks/gamepadscene.go
Normal file
110
example/blocks/blocks/gamepadscene.go
Normal file
@ -0,0 +1,110 @@
|
||||
// Copyright 2015 Hajime Hoshi
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package blocks
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/hajimehoshi/ebiten"
|
||||
"github.com/hajimehoshi/ebiten/example/internal"
|
||||
"image/color"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type GamepadScene struct {
|
||||
currentIndex int
|
||||
countAfterSetting int
|
||||
buttonStates []string
|
||||
}
|
||||
|
||||
func NewGamepadScene() *GamepadScene {
|
||||
return &GamepadScene{}
|
||||
}
|
||||
|
||||
func (s *GamepadScene) Update(state *GameState) error {
|
||||
if s.currentIndex == 0 {
|
||||
state.Input.gamepadConfig.Reset()
|
||||
}
|
||||
if state.Input.StateForKey(ebiten.KeyEscape) == 1 {
|
||||
state.Input.gamepadConfig.Reset()
|
||||
state.SceneManager.GoTo(NewTitleScene())
|
||||
}
|
||||
|
||||
if s.buttonStates == nil {
|
||||
s.buttonStates = make([]string, len(gamepadStdButtons))
|
||||
}
|
||||
for i, b := range gamepadStdButtons {
|
||||
if i < s.currentIndex {
|
||||
s.buttonStates[i] = strings.ToUpper(state.Input.gamepadConfig.Name(b))
|
||||
continue
|
||||
}
|
||||
if s.currentIndex == i {
|
||||
s.buttonStates[i] = "_"
|
||||
continue
|
||||
}
|
||||
s.buttonStates[i] = ""
|
||||
}
|
||||
|
||||
if 0 < s.countAfterSetting {
|
||||
s.countAfterSetting--
|
||||
if s.countAfterSetting <= 0 {
|
||||
state.SceneManager.GoTo(NewTitleScene())
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
b := gamepadStdButtons[s.currentIndex]
|
||||
if state.Input.gamepadConfig.Scan(0, b) {
|
||||
s.currentIndex++
|
||||
if s.currentIndex == len(gamepadStdButtons) {
|
||||
s.countAfterSetting = 60
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *GamepadScene) Draw(screen *ebiten.Image) error {
|
||||
screen.Fill(color.Black)
|
||||
|
||||
if s.buttonStates == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
f := `GAMEPAD CONFIGURATION
|
||||
(PRESS ESC TO CANCEL)
|
||||
|
||||
|
||||
MOVE LEFT: %s
|
||||
|
||||
MOVE RIGHT: %s
|
||||
|
||||
DROP: %s
|
||||
|
||||
ROTATE LEFT: %s
|
||||
|
||||
ROTATE RIGHT: %s
|
||||
|
||||
|
||||
|
||||
%s`
|
||||
msg := ""
|
||||
if s.currentIndex == len(gamepadStdButtons) {
|
||||
msg = "OK!"
|
||||
}
|
||||
str := fmt.Sprintf(f, s.buttonStates[0], s.buttonStates[1], s.buttonStates[2], s.buttonStates[3], s.buttonStates[4], msg)
|
||||
if err := internal.ArcadeFont.DrawTextWithShadow(screen, str, 16, 16, 1, color.White); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
@ -238,6 +238,7 @@ func (s *GameScene) Update(state *GameState) error {
|
||||
s.field.Update()
|
||||
|
||||
if s.gameover {
|
||||
// TODO: Gamepad key?
|
||||
if state.Input.StateForKey(ebiten.KeySpace) == 1 {
|
||||
state.SceneManager.GoTo(NewTitleScene())
|
||||
}
|
||||
@ -263,23 +264,23 @@ func (s *GameScene) Update(state *GameState) error {
|
||||
piece := s.currentPiece
|
||||
x := s.currentPieceX
|
||||
y := s.currentPieceY
|
||||
if state.Input.StateForKey(ebiten.KeySpace) == 1 || state.Input.StateForKey(ebiten.KeyX) == 1 {
|
||||
if state.Input.IsRotateRightTrigger() {
|
||||
s.currentPieceAngle = s.field.RotatePieceRight(piece, x, y, angle)
|
||||
moved = angle != s.currentPieceAngle
|
||||
}
|
||||
if state.Input.StateForKey(ebiten.KeyZ) == 1 {
|
||||
if state.Input.IsRotateLeftTrigger() {
|
||||
s.currentPieceAngle = s.field.RotatePieceLeft(piece, x, y, angle)
|
||||
moved = angle != s.currentPieceAngle
|
||||
}
|
||||
if l := state.Input.StateForKey(ebiten.KeyLeft); l == 1 || (10 <= l && l%2 == 0) {
|
||||
if l := state.Input.StateForLeft(); l == 1 || (10 <= l && l%2 == 0) {
|
||||
s.currentPieceX = s.field.MovePieceToLeft(piece, x, y, angle)
|
||||
moved = x != s.currentPieceX
|
||||
}
|
||||
if r := state.Input.StateForKey(ebiten.KeyRight); r == 1 || (10 <= r && r%2 == 0) {
|
||||
if r := state.Input.StateForRight(); r == 1 || (10 <= r && r%2 == 0) {
|
||||
s.currentPieceX = s.field.MovePieceToRight(piece, x, y, angle)
|
||||
moved = y != s.currentPieceX
|
||||
}
|
||||
if d := state.Input.StateForKey(ebiten.KeyDown); (d-1)%2 == 0 {
|
||||
if d := state.Input.StateForDown(); (d-1)%2 == 0 {
|
||||
s.currentPieceY = s.field.DropPiece(piece, x, y, angle)
|
||||
moved = y != s.currentPieceY
|
||||
if moved {
|
||||
@ -303,7 +304,7 @@ func (s *GameScene) Update(state *GameState) error {
|
||||
if moved {
|
||||
s.landingCount = 0
|
||||
} else if !s.field.Flushing() && !s.field.PieceDroppable(piece, s.currentPieceX, s.currentPieceY, angle) {
|
||||
if 0 < state.Input.StateForKey(ebiten.KeyDown) {
|
||||
if 0 < state.Input.StateForDown() {
|
||||
s.landingCount += 10
|
||||
} else {
|
||||
s.landingCount++
|
||||
|
@ -16,23 +16,34 @@ package blocks
|
||||
|
||||
import (
|
||||
"github.com/hajimehoshi/ebiten"
|
||||
"github.com/hajimehoshi/ebiten/exp/gamepad"
|
||||
)
|
||||
|
||||
type Input struct {
|
||||
keyStates [256]int
|
||||
gamepadButtonStates [4]int
|
||||
var gamepadStdButtons = []gamepad.StdButton{
|
||||
gamepad.StdButtonLL,
|
||||
gamepad.StdButtonLR,
|
||||
gamepad.StdButtonLD,
|
||||
gamepad.StdButtonRD,
|
||||
gamepad.StdButtonRR,
|
||||
}
|
||||
|
||||
func NewInput() *Input {
|
||||
return &Input{}
|
||||
type Input struct {
|
||||
keyStates [256]int
|
||||
gamepadButtonStates [256]int
|
||||
gamepadStdButtonStates [16]int
|
||||
gamepadConfig gamepad.Configuration
|
||||
}
|
||||
|
||||
func (i *Input) StateForKey(key ebiten.Key) int {
|
||||
return i.keyStates[key]
|
||||
}
|
||||
|
||||
func (i *Input) StateForGamepadButton(button ebiten.GamepadButton) int {
|
||||
return i.gamepadButtonStates[button]
|
||||
func (i *Input) StateForGamepadButton(b ebiten.GamepadButton) int {
|
||||
return i.gamepadButtonStates[b]
|
||||
}
|
||||
|
||||
func (i *Input) stateForGamepadStdButton(b gamepad.StdButton) int {
|
||||
return i.gamepadStdButtonStates[b]
|
||||
}
|
||||
|
||||
func (i *Input) Update() {
|
||||
@ -52,4 +63,50 @@ func (i *Input) Update() {
|
||||
}
|
||||
i.gamepadButtonStates[b]++
|
||||
}
|
||||
|
||||
for _, b := range gamepadStdButtons {
|
||||
if !i.gamepadConfig.IsButtonPressed(gamepadID, b) {
|
||||
i.gamepadStdButtonStates[b] = 0
|
||||
continue
|
||||
}
|
||||
i.gamepadStdButtonStates[b]++
|
||||
}
|
||||
}
|
||||
|
||||
func (i *Input) IsRotateRightTrigger() bool {
|
||||
if i.StateForKey(ebiten.KeySpace) == 1 || i.StateForKey(ebiten.KeyX) == 1 {
|
||||
return true
|
||||
}
|
||||
return i.stateForGamepadStdButton(gamepad.StdButtonRR) == 1
|
||||
}
|
||||
|
||||
func (i *Input) IsRotateLeftTrigger() bool {
|
||||
if i.StateForKey(ebiten.KeyZ) == 1 {
|
||||
return true
|
||||
}
|
||||
return i.stateForGamepadStdButton(gamepad.StdButtonRD) == 1
|
||||
}
|
||||
|
||||
func (i *Input) StateForLeft() int {
|
||||
v := i.StateForKey(ebiten.KeyLeft)
|
||||
if 0 < v {
|
||||
return v
|
||||
}
|
||||
return i.stateForGamepadStdButton(gamepad.StdButtonLL)
|
||||
}
|
||||
|
||||
func (i *Input) StateForRight() int {
|
||||
v := i.StateForKey(ebiten.KeyRight)
|
||||
if 0 < v {
|
||||
return v
|
||||
}
|
||||
return i.stateForGamepadStdButton(gamepad.StdButtonLR)
|
||||
}
|
||||
|
||||
func (i *Input) StateForDown() int {
|
||||
v := i.StateForKey(ebiten.KeyDown)
|
||||
if 0 < v {
|
||||
return v
|
||||
}
|
||||
return i.stateForGamepadStdButton(gamepad.StdButtonLD)
|
||||
}
|
||||
|
@ -67,10 +67,38 @@ func NewTitleScene() *TitleScene {
|
||||
}
|
||||
}
|
||||
|
||||
func anyGamepadStdButtonPressed(i *Input) bool {
|
||||
for _, b := range gamepadStdButtons {
|
||||
if i.gamepadConfig.IsButtonPressed(0, b) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func anyGamepadButtonPressed(i *Input) bool {
|
||||
bn := ebiten.GamepadButton(ebiten.GamepadButtonNum(0))
|
||||
for b := ebiten.GamepadButton(0); b < bn; b++ {
|
||||
if i.StateForGamepadButton(b) == 1 {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (s *TitleScene) Update(state *GameState) error {
|
||||
s.count++
|
||||
if state.Input.StateForKey(ebiten.KeySpace) == 1 {
|
||||
state.SceneManager.GoTo(NewGameScene())
|
||||
return nil
|
||||
}
|
||||
if anyGamepadStdButtonPressed(state.Input) {
|
||||
state.SceneManager.GoTo(NewGameScene())
|
||||
return nil
|
||||
}
|
||||
if anyGamepadButtonPressed(state.Input) {
|
||||
state.SceneManager.GoTo(NewGamepadScene())
|
||||
return nil
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@ -86,7 +114,10 @@ func (s *TitleScene) Draw(r *ebiten.Image) error {
|
||||
message := "PRESS SPACE TO START"
|
||||
x := (ScreenWidth - internal.ArcadeFont.TextWidth(message)) / 2
|
||||
y := ScreenHeight - 48
|
||||
return internal.ArcadeFont.DrawTextWithShadow(r, message, x, y, 1, color.NRGBA{0x80, 0, 0, 0xff})
|
||||
if err := internal.ArcadeFont.DrawTextWithShadow(r, message, x, y, 1, color.NRGBA{0x80, 0, 0, 0xff}); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *TitleScene) drawTitleBackground(r *ebiten.Image, c int) error {
|
||||
|
170
exp/gamepad/gamepad.go
Normal file
170
exp/gamepad/gamepad.go
Normal file
@ -0,0 +1,170 @@
|
||||
// Copyright 2015 Hajime Hoshi
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
// Package gamepad offers abstract gamepad buttons and configuration.
|
||||
package gamepad
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/hajimehoshi/ebiten"
|
||||
)
|
||||
|
||||
// A StdButton represents a standard gamepad button.
|
||||
// See also: http://www.w3.org/TR/gamepad/
|
||||
// [UL0] [UR0]
|
||||
// [UL1] [UR1]
|
||||
//
|
||||
// [LU] [CC] [RU]
|
||||
// [LL][LR] [CL][CR] [RL][RR]
|
||||
// [LD] [RD]
|
||||
// [AL] [AR]
|
||||
type StdButton int
|
||||
|
||||
const (
|
||||
StdButtonNone StdButton = iota
|
||||
StdButtonLL
|
||||
StdButtonLR
|
||||
StdButtonLU
|
||||
StdButtonLD
|
||||
StdButtonCL
|
||||
StdButtonCC
|
||||
StdButtonCR
|
||||
StdButtonRL
|
||||
StdButtonRR
|
||||
StdButtonRU
|
||||
StdButtonRD
|
||||
StdButtonUL0
|
||||
StdButtonUL1
|
||||
StdButtonUR0
|
||||
StdButtonUR1
|
||||
StdButtonAL
|
||||
StdButtonAR
|
||||
)
|
||||
|
||||
const threshold = 0.75
|
||||
|
||||
type axis struct {
|
||||
id int
|
||||
positive bool
|
||||
}
|
||||
|
||||
type Configuration struct {
|
||||
current StdButton
|
||||
buttons map[StdButton]ebiten.GamepadButton
|
||||
axes map[StdButton]axis
|
||||
assignedButtons map[ebiten.GamepadButton]struct{}
|
||||
assignedAxes map[axis]struct{}
|
||||
}
|
||||
|
||||
func (c *Configuration) initializeIfNeeded() {
|
||||
if c.buttons == nil {
|
||||
c.buttons = map[StdButton]ebiten.GamepadButton{}
|
||||
}
|
||||
if c.axes == nil {
|
||||
c.axes = map[StdButton]axis{}
|
||||
}
|
||||
if c.assignedButtons == nil {
|
||||
c.assignedButtons = map[ebiten.GamepadButton]struct{}{}
|
||||
}
|
||||
if c.assignedAxes == nil {
|
||||
c.assignedAxes = map[axis]struct{}{}
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Configuration) Reset() {
|
||||
c.buttons = nil
|
||||
c.axes = nil
|
||||
c.assignedButtons = nil
|
||||
c.assignedAxes = nil
|
||||
}
|
||||
|
||||
func (c *Configuration) Scan(index int, b StdButton) bool {
|
||||
c.initializeIfNeeded()
|
||||
|
||||
delete(c.buttons, b)
|
||||
delete(c.axes, b)
|
||||
|
||||
ebn := ebiten.GamepadButton(ebiten.GamepadButtonNum(index))
|
||||
for eb := ebiten.GamepadButton(0); eb < ebn; eb++ {
|
||||
if _, ok := c.assignedButtons[eb]; ok {
|
||||
continue
|
||||
}
|
||||
if ebiten.IsGamepadButtonPressed(index, eb) {
|
||||
c.buttons[b] = eb
|
||||
c.assignedButtons[eb] = struct{}{}
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
an := ebiten.GamepadAxisNum(index)
|
||||
for a := 0; a < an; a++ {
|
||||
v := ebiten.GamepadAxis(index, a)
|
||||
// Check v <= 1.0 because there is a bug that a button returns an axis value wrongly and the value may be over 1.
|
||||
if threshold <= v && v <= 1.0 {
|
||||
if _, ok := c.assignedAxes[axis{a, true}]; !ok {
|
||||
c.axes[b] = axis{a, true}
|
||||
c.assignedAxes[axis{a, true}] = struct{}{}
|
||||
return true
|
||||
}
|
||||
}
|
||||
if -1.0 <= v && v <= -threshold {
|
||||
if _, ok := c.assignedAxes[axis{a, false}]; !ok {
|
||||
c.axes[b] = axis{a, false}
|
||||
c.assignedAxes[axis{a, false}] = struct{}{}
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func (c *Configuration) IsButtonPressed(id int, b StdButton) bool {
|
||||
c.initializeIfNeeded()
|
||||
|
||||
bb, ok := c.buttons[b]
|
||||
if ok {
|
||||
return ebiten.IsGamepadButtonPressed(0, bb)
|
||||
}
|
||||
a, ok := c.axes[b]
|
||||
if ok {
|
||||
v := ebiten.GamepadAxis(0, a.id)
|
||||
if a.positive {
|
||||
return threshold <= v && v <= 1.0
|
||||
} else {
|
||||
return -1.0 <= v && v <= -threshold
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (c *Configuration) Name(b StdButton) string {
|
||||
c.initializeIfNeeded()
|
||||
|
||||
bb, ok := c.buttons[b]
|
||||
if ok {
|
||||
return fmt.Sprintf("Button %d", bb)
|
||||
}
|
||||
|
||||
a, ok := c.axes[b]
|
||||
if ok {
|
||||
if a.positive {
|
||||
return fmt.Sprintf("Axis %d+", a.id)
|
||||
} else {
|
||||
return fmt.Sprintf("Axis %d-", a.id)
|
||||
}
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
Loading…
Reference in New Issue
Block a user