diff options
author | Juan J. Martinez <jjm@usebox.net> | 2023-02-07 22:52:34 +0000 |
---|---|---|
committer | Juan J. Martinez <jjm@usebox.net> | 2023-02-07 22:52:34 +0000 |
commit | 6c4ea0fd4831e887103fafe6587e1c6a73914833 (patch) | |
tree | 4d31449ec0066a7c9e36150c0b4ea25e60b23c4f /src/Game/Controller.hs | |
parent | 0fa864f7ffaa1cdb461ea6ef50d63aebb5d20e79 (diff) | |
download | space-plat-hs-6c4ea0fd4831e887103fafe6587e1c6a73914833.tar.gz space-plat-hs-6c4ea0fd4831e887103fafe6587e1c6a73914833.zip |
Avoid repeat for buttons a, b and menu
Diffstat (limited to 'src/Game/Controller.hs')
-rw-r--r-- | src/Game/Controller.hs | 29 |
1 files changed, 15 insertions, 14 deletions
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 |