From 6c4ea0fd4831e887103fafe6587e1c6a73914833 Mon Sep 17 00:00:00 2001 From: "Juan J. Martinez" Date: Tue, 7 Feb 2023 22:52:34 +0000 Subject: Avoid repeat for buttons a, b and menu --- src/Game.hs | 2 +- src/Game/Controller.hs | 29 +++++++++++++++-------------- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/src/Game.hs b/src/Game.hs index 5a974c6..48a8e21 100644 --- a/src/Game.hs +++ b/src/Game.hs @@ -120,7 +120,7 @@ gameLoop = do let quit = fromMaybe False (isPressed SDL.KeycodeEscape events) || SDL.QuitEvent `elem` events unless quit $ do -- update controls - controls $~ C.update events + controls $~ flip C.update events SDL.rendererRenderTarget renderer $= Just canvas SDL.clear renderer diff --git a/src/Game/Controller.hs b/src/Game/Controller.hs index b5cd685..77039ac 100644 --- a/src/Game/Controller.hs +++ b/src/Game/Controller.hs @@ -1,6 +1,6 @@ module Game.Controller (Controls (..), init, update) where -import Data.Maybe (fromMaybe, isNothing) +import Data.Maybe (fromMaybe, isJust, isNothing) import Data.Vector ((!?)) import Game.Utils (isPressed, isPressedGamepad) import qualified SDL @@ -37,8 +37,8 @@ init :: IO Controls init = do Controls False False False False False False False <$> getJoystick -updateGamepad :: [SDL.EventPayload] -> Controls -> Controls -updateGamepad events controls +updateGamepad :: Controls -> [SDL.EventPayload] -> Controls +updateGamepad controls events | isNothing $ controls.joy = controls -- XXX: deal with disconnection/reconnection | otherwise = @@ -47,23 +47,24 @@ updateGamepad events controls down = fromMaybe controls.down $ isPressedGamepad SDL.ControllerButtonDpadDown events, left = fromMaybe controls.left $ isPressedGamepad SDL.ControllerButtonDpadLeft events, right = fromMaybe controls.right $ isPressedGamepad SDL.ControllerButtonDpadRight events, - a = fromMaybe controls.a $ isPressedGamepad SDL.ControllerButtonA events, - b = fromMaybe controls.b $ isPressedGamepad SDL.ControllerButtonB events, - menu = fromMaybe controls.menu $ isPressedGamepad SDL.ControllerButtonStart events + a = fromMaybe False $ isPressedGamepad SDL.ControllerButtonA events, + b = fromMaybe False $ isPressedGamepad SDL.ControllerButtonB events, + menu = fromMaybe False $ isPressedGamepad SDL.ControllerButtonStart events } -updateKeyboard :: [SDL.EventPayload] -> Controls -> Controls -updateKeyboard events controls = +updateKeyboard :: Controls -> [SDL.EventPayload] -> Controls +updateKeyboard controls events = controls { up = fromMaybe controls.up $ isPressed SDL.KeycodeUp events, down = fromMaybe controls.down $ isPressed SDL.KeycodeDown events, left = fromMaybe controls.left $ isPressed SDL.KeycodeLeft events, right = fromMaybe controls.right $ isPressed SDL.KeycodeRight events, - a = fromMaybe controls.a $ isPressed SDL.KeycodeZ events, - b = fromMaybe controls.b $ isPressed SDL.KeycodeX events, - menu = fromMaybe controls.menu $ isPressed SDL.KeycodeReturn events + a = fromMaybe False $ isPressed SDL.KeycodeZ events, + b = fromMaybe False $ isPressed SDL.KeycodeX events, + menu = fromMaybe False $ isPressed SDL.KeycodeReturn events } -update :: [SDL.EventPayload] -> Controls -> Controls -update events controls = do - updateGamepad events $ updateKeyboard events controls +update :: Controls -> [SDL.EventPayload] -> Controls +update controls + | isJust controls.joy = updateGamepad controls + | otherwise = updateKeyboard controls -- cgit v1.2.3