From c5f5c01ab044c2bef3e5c93733bf5b7f03046ba5 Mon Sep 17 00:00:00 2001 From: dbenavid Date: Tue, 5 Sep 2023 07:52:00 -0400 Subject: [PATCH] Cleaned up controller deadzone logic, fixed bug related to uninitiated motion. --- examples/splashmenu/scenes/controller.go | 43 +++++++++++++++++------- 1 file changed, 30 insertions(+), 13 deletions(-) diff --git a/examples/splashmenu/scenes/controller.go b/examples/splashmenu/scenes/controller.go index 4c00056..6d54795 100644 --- a/examples/splashmenu/scenes/controller.go +++ b/examples/splashmenu/scenes/controller.go @@ -156,6 +156,8 @@ type SplashPad struct { heroImpulse impulse jumping bool jumptimer int + + controller bool } func NewSplashPad() SplashPad { @@ -184,6 +186,7 @@ func NewSplashPad() SplashPad { hero: h, jumping: false, jumptimer: 0, + controller: false, } gravity := &force{} @@ -240,6 +243,13 @@ func (s *SplashPad) Update() error { s.hero.jumptime = 0 s.hero.jumping = false } + + //check for user input to transition scene + if inpututil.IsKeyJustPressed(ebiten.KeyQ) { + if s.events[groovy.COMPLETED] != nil { + s.events[groovy.COMPLETED]() + } + } return nil } @@ -468,15 +478,19 @@ func (s *SplashPad) handleLeftMovement() { s.hero.runningLeft = true log.Println(" left gamepad engaged") s.hero.facingLeft = true + s.controller = true } - if -SENSITIVITY < ebiten.GamepadAxisValue(0, 0) && ebiten.GamepadAxisValue(0, 0) < SENSITIVITY { - s.hero.runningLeft = false - if s.hero.runningStopFrame > 0 { - s.hero.runningStopFrame = s.increment - log.Println(" left gamepad released") - } + if s.controller { + if -SENSITIVITY < ebiten.GamepadAxisValue(0, 0) && ebiten.GamepadAxisValue(0, 0) < SENSITIVITY { + s.hero.runningLeft = false + if s.hero.runningStopFrame > 0 { + s.hero.runningStopFrame = s.increment + log.Println(" left gamepad released") + s.controller = false + } + } } //if running, need to update their position @@ -501,7 +515,7 @@ func (s *SplashPad) handleLeftMovement() { deltaT := float32(s.increment - s.hero.runningStopFrame) offset = TOP_SPEED - int(scaler*deltaT) - offset = max(0, offset) //should not be less than zero, otherwise player would move wrong way ?? + offset = int(math.Max(0, float64(offset))) //should not be less than zero, otherwise player would move wrong way ?? log.Printf(" stopping (%d,%d)...", s.hero.posX, s.hero.posY) } else { s.hero.runningStopFrame = 0 @@ -538,15 +552,18 @@ func (s *SplashPad) handleRightMovement() { s.hero.runningRight = true log.Println(" right gamepad engaged") s.hero.facingLeft = false + s.controller = true } + if s.controller { + if -SENSITIVITY < ebiten.GamepadAxisValue(0, 0) && ebiten.GamepadAxisValue(0, 0) < SENSITIVITY { + s.hero.runningRight = false + if s.hero.runningStopFrame > 0 { + s.hero.runningStopFrame = s.increment + log.Println(" right gamepad released") + s.controller = false + } - if -SENSITIVITY < ebiten.GamepadAxisValue(0, 0) && ebiten.GamepadAxisValue(0, 0) < SENSITIVITY { - s.hero.runningRight = false - if s.hero.runningStopFrame > 0 { - s.hero.runningStopFrame = s.increment - log.Println(" right gamepad released") } - } //if running, need to update their position