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
}
// IsButtonPressed returns a boolean value indicating whether
// the given virtual button b is pressed.
// IsButtonPressed reports whether the given virtual button b is pressed.
func (c *gamepadConfig) IsButtonPressed(b virtualGamepadButton) bool {
if !c.gamepadIDInitialized {
panic("not reached")
@ -188,6 +187,21 @@ func (c *gamepadConfig) IsButtonPressed(b virtualGamepadButton) bool {
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.
func (c *gamepadConfig) ButtonName(b virtualGamepadButton) string {
if !c.gamepadIDInitialized {

View File

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

View File

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