From 0f54f532f0412acaefca3f66d2f4b48232944334 Mon Sep 17 00:00:00 2001 From: Kiana Sheibani Date: Sat, 28 Jan 2023 16:07:41 -0500 Subject: [PATCH] Separate key-getting function from event handler --- src/Game/Engine.hs | 36 +++++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/src/Game/Engine.hs b/src/Game/Engine.hs index 80455d4..28c57b8 100644 --- a/src/Game/Engine.hs +++ b/src/Game/Engine.hs @@ -4,9 +4,11 @@ module Game.Engine where +import Control.Monad (guard) import Control.Monad.Trans.MSF.Maybe import Control.Monad.Trans.MSF.Reader import Control.Monad.Trans.MSF.Writer +import Data.Functor (($>)) import Data.Maybe import Data.MonadicStreamFunction import Game.Display @@ -15,22 +17,30 @@ import Game.Utils import Graphics.Gloss import Graphics.Gloss.Interface.IO.Game -handleEvents :: Monad m => MSF (DrawerT m) () [Direction] -handleEvents = mapMaybe getDir <$> liftTransS (constM ask) +-- * Input handling + +getKeys :: Monad m => MSF (DrawerT m) () [Key] +getKeys = mapMaybe getKey <$> liftTransS (constM ask) where - getDir :: Event -> Maybe Direction - getDir (EventKey k Down _ _) = case k of - Char 'w' -> Just U - Char 's' -> Just D - Char 'a' -> Just L - Char 'd' -> Just R - SpecialKey KeyUp -> Just U - SpecialKey KeyDown -> Just D - SpecialKey KeyLeft -> Just L - SpecialKey KeyRight -> Just R - _ -> Nothing + getKey :: Event -> Maybe Key + getKey (EventKey k Down _ _) = Just k + getKey _ = Nothing + +handleEvents :: Monad m => MSF (DrawerT m) () [Direction] +handleEvents = mapMaybe getDir <$> getKeys + where + getDir :: Key -> Maybe Direction + getDir (Char 'w') = Just U + getDir (Char 's') = Just D + getDir (Char 'a') = Just L + getDir (Char 'd') = Just R + getDir (SpecialKey KeyUp) = Just U + getDir (SpecialKey KeyDown) = Just D + getDir (SpecialKey KeyLeft) = Just L + getDir (SpecialKey KeyRight) = Just R getDir _ = Nothing +-- * Core game engine tickState :: Maybe Direction -> GameState -> Maybe GameState tickState dir state =