examples/blocks/blocks: Use gamepads to go back to the title

This commit is contained in:
Hajime Hoshi 2021-04-17 21:55:58 +09:00
parent b604391b21
commit c950e03fb1
3 changed files with 21 additions and 7 deletions

View File

@ -162,8 +162,7 @@ func (c *gamepadConfig) Scan(b virtualGamepadButton) bool {
return false return false
} }
// IsButtonPressed returns a boolean value indicating whether // IsButtonPressed reports whether the given virtual button b is pressed.
// the given virtual button b is pressed.
func (c *gamepadConfig) IsButtonPressed(b virtualGamepadButton) bool { func (c *gamepadConfig) IsButtonPressed(b virtualGamepadButton) bool {
if !c.gamepadIDInitialized { if !c.gamepadIDInitialized {
panic("not reached") panic("not reached")
@ -188,6 +187,21 @@ func (c *gamepadConfig) IsButtonPressed(b virtualGamepadButton) bool {
return false return false
} }
// IsButtonJustPressed reports whether the given virtual button b started to be pressed now.
func (c *gamepadConfig) IsButtonJustPressed(b virtualGamepadButton) bool {
if !c.gamepadIDInitialized {
panic("not reached")
}
c.initializeIfNeeded()
bb, ok := c.buttons[b]
if ok {
return inpututil.IsGamepadButtonJustPressed(c.gamepadID, bb)
}
return false
}
// Name returns the pysical button's name for the given virtual button. // Name returns the pysical button's name for the given virtual button.
func (c *gamepadConfig) ButtonName(b virtualGamepadButton) string { func (c *gamepadConfig) ButtonName(b virtualGamepadButton) string {
if !c.gamepadIDInitialized { if !c.gamepadIDInitialized {

View File

@ -231,8 +231,8 @@ func (s *GameScene) Update(state *GameState) error {
s.field.Update() s.field.Update()
if s.gameover { if s.gameover {
// TODO: Gamepad key? if inpututil.IsKeyJustPressed(ebiten.KeySpace) ||
if inpututil.IsKeyJustPressed(ebiten.KeySpace) { anyGamepadAbstractButtonJustPressed(state.Input) {
state.SceneManager.GoTo(&TitleScene{}) state.SceneManager.GoTo(&TitleScene{})
} }
return nil return nil

View File

@ -39,13 +39,13 @@ type TitleScene struct {
count int count int
} }
func anyGamepadAbstractButtonPressed(i *Input) bool { func anyGamepadAbstractButtonJustPressed(i *Input) bool {
if !i.gamepadConfig.IsInitialized() { if !i.gamepadConfig.IsInitialized() {
return false return false
} }
for _, b := range virtualGamepadButtons { for _, b := range virtualGamepadButtons {
if i.gamepadConfig.IsButtonPressed(b) { if i.gamepadConfig.IsButtonJustPressed(b) {
return true return true
} }
} }
@ -59,7 +59,7 @@ func (s *TitleScene) Update(state *GameState) error {
return nil return nil
} }
if anyGamepadAbstractButtonPressed(state.Input) { if anyGamepadAbstractButtonJustPressed(state.Input) {
state.SceneManager.GoTo(NewGameScene()) state.SceneManager.GoTo(NewGameScene())
return nil return nil
} }