mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-24 10:48:53 +01:00
examples/blocks: Remove keyStates and use inpututil instead (#415)
This commit is contained in:
parent
7dc36747a6
commit
ae07488190
@ -22,6 +22,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/hajimehoshi/ebiten"
|
"github.com/hajimehoshi/ebiten"
|
||||||
|
"github.com/hajimehoshi/ebiten/inpututil"
|
||||||
)
|
)
|
||||||
|
|
||||||
type GamepadScene struct {
|
type GamepadScene struct {
|
||||||
@ -34,7 +35,7 @@ func (s *GamepadScene) Update(state *GameState) error {
|
|||||||
if s.currentIndex == 0 {
|
if s.currentIndex == 0 {
|
||||||
state.Input.gamepadConfig.Reset()
|
state.Input.gamepadConfig.Reset()
|
||||||
}
|
}
|
||||||
if state.Input.StateForKey(ebiten.KeyEscape) == 1 {
|
if inpututil.IsKeyJustPressed(ebiten.KeyEscape) {
|
||||||
state.Input.gamepadConfig.Reset()
|
state.Input.gamepadConfig.Reset()
|
||||||
state.SceneManager.GoTo(&TitleScene{})
|
state.SceneManager.GoTo(&TitleScene{})
|
||||||
}
|
}
|
||||||
|
@ -25,6 +25,7 @@ import (
|
|||||||
|
|
||||||
"github.com/hajimehoshi/ebiten"
|
"github.com/hajimehoshi/ebiten"
|
||||||
"github.com/hajimehoshi/ebiten/ebitenutil"
|
"github.com/hajimehoshi/ebiten/ebitenutil"
|
||||||
|
"github.com/hajimehoshi/ebiten/inpututil"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -219,7 +220,7 @@ func (s *GameScene) Update(state *GameState) error {
|
|||||||
|
|
||||||
if s.gameover {
|
if s.gameover {
|
||||||
// TODO: Gamepad key?
|
// TODO: Gamepad key?
|
||||||
if state.Input.StateForKey(ebiten.KeySpace) == 1 {
|
if inpututil.IsKeyJustPressed(ebiten.KeySpace) {
|
||||||
state.SceneManager.GoTo(&TitleScene{})
|
state.SceneManager.GoTo(&TitleScene{})
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
@ -18,24 +18,16 @@ package blocks
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/hajimehoshi/ebiten"
|
"github.com/hajimehoshi/ebiten"
|
||||||
|
"github.com/hajimehoshi/ebiten/inpututil"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Input manages the input state including gamepads and keyboards.
|
// Input manages the input state including gamepads and keyboards.
|
||||||
type Input struct {
|
type Input struct {
|
||||||
keyStates map[ebiten.Key]int
|
|
||||||
anyGamepadButtonPressed bool
|
anyGamepadButtonPressed bool
|
||||||
virtualGamepadButtonStates map[virtualGamepadButton]int
|
virtualGamepadButtonStates map[virtualGamepadButton]int
|
||||||
gamepadConfig gamepadConfig
|
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
|
// IsAnyGamepadButtonPressed returns a boolean value indicating
|
||||||
// whether any gamepad button is pressed.
|
// whether any gamepad button is pressed.
|
||||||
func (i *Input) IsAnyGamepadButtonPressed() bool {
|
func (i *Input) IsAnyGamepadButtonPressed() bool {
|
||||||
@ -50,17 +42,6 @@ func (i *Input) stateForVirtualGamepadButton(b virtualGamepadButton) int {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (i *Input) Update() {
|
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
|
const gamepadID = 0
|
||||||
i.anyGamepadButtonPressed = false
|
i.anyGamepadButtonPressed = false
|
||||||
for b := ebiten.GamepadButton(0); b <= ebiten.GamepadButtonMax; b++ {
|
for b := ebiten.GamepadButton(0); b <= ebiten.GamepadButtonMax; b++ {
|
||||||
@ -83,38 +64,35 @@ func (i *Input) Update() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (i *Input) IsRotateRightJustPressed() bool {
|
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 true
|
||||||
}
|
}
|
||||||
return i.stateForVirtualGamepadButton(virtualGamepadButtonButtonB) == 1
|
return i.stateForVirtualGamepadButton(virtualGamepadButtonButtonB) == 1
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *Input) IsRotateLeftJustPressed() bool {
|
func (i *Input) IsRotateLeftJustPressed() bool {
|
||||||
if i.StateForKey(ebiten.KeyZ) == 1 {
|
if inpututil.IsKeyJustPressed(ebiten.KeyZ) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
return i.stateForVirtualGamepadButton(virtualGamepadButtonButtonA) == 1
|
return i.stateForVirtualGamepadButton(virtualGamepadButtonButtonA) == 1
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *Input) StateForLeft() int {
|
func (i *Input) StateForLeft() int {
|
||||||
v := i.StateForKey(ebiten.KeyLeft)
|
if v := inpututil.KeyPressDuration(ebiten.KeyLeft); 0 < v {
|
||||||
if 0 < v {
|
|
||||||
return v
|
return v
|
||||||
}
|
}
|
||||||
return i.stateForVirtualGamepadButton(virtualGamepadButtonLeft)
|
return i.stateForVirtualGamepadButton(virtualGamepadButtonLeft)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *Input) StateForRight() int {
|
func (i *Input) StateForRight() int {
|
||||||
v := i.StateForKey(ebiten.KeyRight)
|
if v := inpututil.KeyPressDuration(ebiten.KeyRight); 0 < v {
|
||||||
if 0 < v {
|
|
||||||
return v
|
return v
|
||||||
}
|
}
|
||||||
return i.stateForVirtualGamepadButton(virtualGamepadButtonRight)
|
return i.stateForVirtualGamepadButton(virtualGamepadButtonRight)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *Input) StateForDown() int {
|
func (i *Input) StateForDown() int {
|
||||||
v := i.StateForKey(ebiten.KeyDown)
|
if v := inpututil.KeyPressDuration(ebiten.KeyDown); 0 < v {
|
||||||
if 0 < v {
|
|
||||||
return v
|
return v
|
||||||
}
|
}
|
||||||
return i.stateForVirtualGamepadButton(virtualGamepadButtonDown)
|
return i.stateForVirtualGamepadButton(virtualGamepadButtonDown)
|
||||||
|
@ -21,6 +21,7 @@ import (
|
|||||||
|
|
||||||
"github.com/hajimehoshi/ebiten"
|
"github.com/hajimehoshi/ebiten"
|
||||||
"github.com/hajimehoshi/ebiten/ebitenutil"
|
"github.com/hajimehoshi/ebiten/ebitenutil"
|
||||||
|
"github.com/hajimehoshi/ebiten/inpututil"
|
||||||
)
|
)
|
||||||
|
|
||||||
var imageBackground *ebiten.Image
|
var imageBackground *ebiten.Image
|
||||||
@ -48,7 +49,7 @@ func anyGamepadAbstractButtonPressed(i *Input) bool {
|
|||||||
|
|
||||||
func (s *TitleScene) Update(state *GameState) error {
|
func (s *TitleScene) Update(state *GameState) error {
|
||||||
s.count++
|
s.count++
|
||||||
if state.Input.StateForKey(ebiten.KeySpace) == 1 {
|
if inpututil.IsKeyJustPressed(ebiten.KeySpace) {
|
||||||
state.SceneManager.GoTo(NewGameScene())
|
state.SceneManager.GoTo(NewGameScene())
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user