diff options
author | Juan J. Martinez <jjm@usebox.net> | 2023-05-16 21:50:18 +0100 |
---|---|---|
committer | Juan J. Martinez <jjm@usebox.net> | 2023-05-16 21:50:18 +0100 |
commit | bce1f36c1e1507e6c503f42c9ae0285d05b52619 (patch) | |
tree | 00c7f381c89e9beeced852f089d46fd480e2825b | |
parent | 89d002c1213b393ed6a78e1d86ae189581080742 (diff) | |
download | space-plat-hs-bce1f36c1e1507e6c503f42c9ae0285d05b52619.tar.gz space-plat-hs-bce1f36c1e1507e6c503f42c9ae0285d05b52619.zip |
Update state of the play loop in the play loop
-rw-r--r-- | src/Game.hs | 48 |
1 files changed, 25 insertions, 23 deletions
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 |