From bce1f36c1e1507e6c503f42c9ae0285d05b52619 Mon Sep 17 00:00:00 2001 From: "Juan J. Martinez" Date: Tue, 16 May 2023 21:50:18 +0100 Subject: Update state of the play loop in the play loop --- src/Game.hs | 48 +++++++++++++++++++++++++----------------------- 1 file changed, 25 insertions(+), 23 deletions(-) (limited to 'src/Game.hs') diff --git a/src/Game.hs b/src/Game.hs index c5fbdef..50d9dc1 100644 --- a/src/Game.hs +++ b/src/Game.hs @@ -157,7 +157,7 @@ gameLoop e = do updatedEnv <- if state.gameOverDelay /= 1 - then playLoop =<< updateState updatedToasterEnv + then playLoop updatedToasterEnv else gameOverLoop updatedToasterEnv T.render renderer updatedEnv.toaster @@ -169,27 +169,6 @@ gameLoop e = do SDL.present renderer gameLoop updatedEnv - where - -- update state counters, etc - updateState :: Env -> IO Env - updateState env - | state.gameOverDelay > 1 = pure env {state = state {GS.gameOverDelay = state.gameOverDelay - 1}} - | state.batteries == state.totalBatteries && not state.exit = do - es <- E.addExit env.entities x (y - 8) -- adjusted to player's height - pure env {entities = es, state = state {GS.exit = True}} - | state.levelCompleted == GS.ExitDone = do - map' <- M.load (env.mapList !! (env.state.currentLevel + 1)) env.tsTexture - entities <- E.mkEntities env.sprites map' env.controlsRef - pure $ - env - { map = map', - state = (GS.levelState env.state map') {GS.currentLevel = env.state.currentLevel + 1}, - entities = entities - } - | otherwise = pure env - where - state = env.state - (x, y) = state.lastBattery playLoop :: Env -> IO Env playLoop e = do @@ -200,6 +179,8 @@ playLoop e = do (updated, state) <- E.updateAll entities e.state + env <- updateState e {state = state, entities = updated, map = map'} + -- to update the map viewport let (px, py) = E.playerPosition updated @@ -213,7 +194,28 @@ playLoop e = do SDL.rendererViewport renderer $= Nothing H.render renderer hud state - pure e {state = state, entities = updated, map = map'} + pure env + where + -- update state counters, etc + updateState :: Env -> IO Env + updateState env + | state.gameOverDelay > 1 = pure env {state = state {GS.gameOverDelay = state.gameOverDelay - 1}} + | state.batteries == state.totalBatteries && not state.exit = do + es <- E.addExit env.entities x (y - 8) -- adjusted to player's height + pure env {entities = es, state = state {GS.exit = True}} + | state.levelCompleted == GS.ExitDone = do + map' <- M.load (env.mapList !! (env.state.currentLevel + 1)) env.tsTexture + entities <- E.mkEntities env.sprites map' env.controlsRef + pure $ + env + { map = map', + state = (GS.levelState env.state map') {GS.currentLevel = env.state.currentLevel + 1}, + entities = entities + } + | otherwise = pure env + where + state = env.state + (x, y) = state.lastBattery gameOverLoop :: Env -> IO Env gameOverLoop e = do -- cgit v1.2.3