diff options
author | Juan J. Martinez <jjm@usebox.net> | 2023-03-01 20:02:14 +0000 |
---|---|---|
committer | Juan J. Martinez <jjm@usebox.net> | 2023-03-01 20:02:14 +0000 |
commit | f0507125e06e3e8939f259e9468d797e9a8e668e (patch) | |
tree | 568c3c7f316bfa6af1f1f5f846c0777f46f87d28 /src | |
parent | bfb588a4cfa7f555d9e2991023ffee1fc4708e09 (diff) | |
download | space-plat-hs-f0507125e06e3e8939f259e9468d797e9a8e668e.tar.gz space-plat-hs-f0507125e06e3e8939f259e9468d797e9a8e668e.zip |
Dropped ReaderT as is not really needed
Diffstat (limited to 'src')
-rw-r--r-- | src/Game.hs | 56 |
1 files changed, 26 insertions, 30 deletions
diff --git a/src/Game.hs b/src/Game.hs index 537b889..d8ce8b6 100644 --- a/src/Game.hs +++ b/src/Game.hs @@ -1,6 +1,6 @@ module Game (main) where -import Control.Monad.Reader +import Control.Monad import Data.IORef import Data.Maybe (fromMaybe) import Data.Text (pack) @@ -92,8 +92,7 @@ main = do } hud <- H.mkHud sprites state entities <- newIORef =<< E.mkEntities sprites map' controls state - runReaderT - gameLoop + gameLoop Env { window = window, renderer = renderer, @@ -111,9 +110,8 @@ main = do SDL.destroyWindow window SDL.quit -toggleFullscreen :: ReaderT Env IO () -toggleFullscreen = do - env <- ask +toggleFullscreen :: Env -> IO () +toggleFullscreen env = do let fullscreen = env.fullscreen renderRect = env.renderRect renderer = env.renderer @@ -136,9 +134,8 @@ toggleFullscreen = do rh = gameHeight * scale in renderRect $= newRenderRect -gameLoop :: ReaderT Env IO () -gameLoop = do - env <- ask +gameLoop :: Env -> IO () +gameLoop env = do let renderer = env.renderer canvas = env.canvas renderRect = env.renderRect @@ -148,7 +145,7 @@ gameLoop = do events <- map SDL.eventPayload <$> SDL.pollEvents -- F11 for fullscreen / windowed - when (fromMaybe False $ U.isPressed SDL.KeycodeF11 events) toggleFullscreen + when (fromMaybe False $ U.isPressed SDL.KeycodeF11 events) $ toggleFullscreen env -- ESC or close the window to quit let quit = fromMaybe False (U.isPressed SDL.KeycodeEscape events) || SDL.QuitEvent `elem` events @@ -159,11 +156,10 @@ gameLoop = do SDL.rendererRenderTarget renderer $= Just canvas SDL.clear renderer - void $ liftIO $ do - state <- readIORef stateRef - when (state.gameOverDelay > 1) $ stateRef $= state {GS.gameOverDelay = state.gameOverDelay - 1} - when (state.gameOverDelay /= 1) $ playLoop env - when (state.gameOverDelay == 1) $ gameOverLoop env + state <- readIORef stateRef + when (state.gameOverDelay > 1) $ stateRef $= state {GS.gameOverDelay = state.gameOverDelay - 1} + when (state.gameOverDelay /= 1) playLoop + when (state.gameOverDelay == 1) gameOverLoop SDL.rendererRenderTarget renderer $= Nothing SDL.clear renderer @@ -172,10 +168,10 @@ gameLoop = do SDL.present renderer - gameLoop + gameLoop env where - playLoop :: Env -> IO () - playLoop env = do + playLoop :: IO () + playLoop = do let renderer = env.renderer map' = env.map entities = env.entities @@ -188,18 +184,18 @@ gameLoop = do let (px, py) = E.playerPosition updated -- render map and entities - void $ do - -- set the SDL viewport - viewport <- M.viewport renderer map' px py (fromIntegral gameWidth) (fromIntegral gameHeight - H.height) (Just (0, H.height)) - M.render renderer map' viewport - E.renderVisible renderer updated viewport - - -- reset viewport to draw the HUD - SDL.rendererViewport renderer $= Nothing - H.render renderer hud - - gameOverLoop :: Env -> IO () - gameOverLoop env = do + + -- set the SDL viewport + viewport <- M.viewport renderer map' px py (fromIntegral gameWidth) (fromIntegral gameHeight - H.height) (Just (0, H.height)) + M.render renderer map' viewport + E.renderVisible renderer updated viewport + + -- reset viewport to draw the HUD + SDL.rendererViewport renderer $= Nothing + H.render renderer hud + + gameOverLoop :: IO () + gameOverLoop = do let renderer = env.renderer sprites = env.sprites hud = env.hud |