examples/blocks: Remove keyStates and use inpututil instead (#415)

This commit is contained in:
Hajime Hoshi 2018-02-04 23:35:09 +09:00
parent 7dc36747a6
commit ae07488190
4 changed files with 12 additions and 31 deletions

View File

@ -22,6 +22,7 @@ import (
"strings"
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/inpututil"
)
type GamepadScene struct {
@ -34,7 +35,7 @@ func (s *GamepadScene) Update(state *GameState) error {
if s.currentIndex == 0 {
state.Input.gamepadConfig.Reset()
}
if state.Input.StateForKey(ebiten.KeyEscape) == 1 {
if inpututil.IsKeyJustPressed(ebiten.KeyEscape) {
state.Input.gamepadConfig.Reset()
state.SceneManager.GoTo(&TitleScene{})
}

View File

@ -25,6 +25,7 @@ import (
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/ebitenutil"
"github.com/hajimehoshi/ebiten/inpututil"
)
var (
@ -219,7 +220,7 @@ func (s *GameScene) Update(state *GameState) error {
if s.gameover {
// TODO: Gamepad key?
if state.Input.StateForKey(ebiten.KeySpace) == 1 {
if inpututil.IsKeyJustPressed(ebiten.KeySpace) {
state.SceneManager.GoTo(&TitleScene{})
}
return nil

View File

@ -18,24 +18,16 @@ package blocks
import (
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/inpututil"
)
// Input manages the input state including gamepads and keyboards.
type Input struct {
keyStates map[ebiten.Key]int
anyGamepadButtonPressed bool
virtualGamepadButtonStates map[virtualGamepadButton]int
gamepadConfig gamepadConfig
}
// StateForKey returns time length indicating how long the key is pressed.
func (i *Input) StateForKey(key ebiten.Key) int {
if i.keyStates == nil {
return 0
}
return i.keyStates[key]
}
// IsAnyGamepadButtonPressed returns a boolean value indicating
// whether any gamepad button is pressed.
func (i *Input) IsAnyGamepadButtonPressed() bool {
@ -50,17 +42,6 @@ func (i *Input) stateForVirtualGamepadButton(b virtualGamepadButton) int {
}
func (i *Input) Update() {
if i.keyStates == nil {
i.keyStates = map[ebiten.Key]int{}
}
for key := ebiten.Key(0); key <= ebiten.KeyMax; key++ {
if !ebiten.IsKeyPressed(ebiten.Key(key)) {
i.keyStates[key] = 0
continue
}
i.keyStates[key]++
}
const gamepadID = 0
i.anyGamepadButtonPressed = false
for b := ebiten.GamepadButton(0); b <= ebiten.GamepadButtonMax; b++ {
@ -83,38 +64,35 @@ func (i *Input) Update() {
}
func (i *Input) IsRotateRightJustPressed() bool {
if i.StateForKey(ebiten.KeySpace) == 1 || i.StateForKey(ebiten.KeyX) == 1 {
if inpututil.IsKeyJustPressed(ebiten.KeySpace) || inpututil.IsKeyJustPressed(ebiten.KeyX) {
return true
}
return i.stateForVirtualGamepadButton(virtualGamepadButtonButtonB) == 1
}
func (i *Input) IsRotateLeftJustPressed() bool {
if i.StateForKey(ebiten.KeyZ) == 1 {
if inpututil.IsKeyJustPressed(ebiten.KeyZ) {
return true
}
return i.stateForVirtualGamepadButton(virtualGamepadButtonButtonA) == 1
}
func (i *Input) StateForLeft() int {
v := i.StateForKey(ebiten.KeyLeft)
if 0 < v {
if v := inpututil.KeyPressDuration(ebiten.KeyLeft); 0 < v {
return v
}
return i.stateForVirtualGamepadButton(virtualGamepadButtonLeft)
}
func (i *Input) StateForRight() int {
v := i.StateForKey(ebiten.KeyRight)
if 0 < v {
if v := inpututil.KeyPressDuration(ebiten.KeyRight); 0 < v {
return v
}
return i.stateForVirtualGamepadButton(virtualGamepadButtonRight)
}
func (i *Input) StateForDown() int {
v := i.StateForKey(ebiten.KeyDown)
if 0 < v {
if v := inpututil.KeyPressDuration(ebiten.KeyDown); 0 < v {
return v
}
return i.stateForVirtualGamepadButton(virtualGamepadButtonDown)

View File

@ -21,6 +21,7 @@ import (
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/ebitenutil"
"github.com/hajimehoshi/ebiten/inpututil"
)
var imageBackground *ebiten.Image
@ -48,7 +49,7 @@ func anyGamepadAbstractButtonPressed(i *Input) bool {
func (s *TitleScene) Update(state *GameState) error {
s.count++
if state.Input.StateForKey(ebiten.KeySpace) == 1 {
if inpututil.IsKeyJustPressed(ebiten.KeySpace) {
state.SceneManager.GoTo(NewGameScene())
return nil
}