aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJuan J. Martinez <jjm@usebox.net>2023-05-16 22:13:37 +0100
committerJuan J. Martinez <jjm@usebox.net>2023-05-16 22:13:37 +0100
commit5b13a8c276199f3ccdfdcb0d96dd7631b7e1b1fb (patch)
treef3d26b679325aeffe11eed690921a1d130a057b1 /src
parentbce1f36c1e1507e6c503f42c9ae0285d05b52619 (diff)
downloadspace-plat-hs-5b13a8c276199f3ccdfdcb0d96dd7631b7e1b1fb.tar.gz
space-plat-hs-5b13a8c276199f3ccdfdcb0d96dd7631b7e1b1fb.zip
Use play state instead of level complete, that includes game over
Diffstat (limited to 'src')
-rw-r--r--src/Game.hs17
-rw-r--r--src/Game/Entities.hs10
-rw-r--r--src/Game/State.hs10
3 files changed, 20 insertions, 17 deletions
diff --git a/src/Game.hs b/src/Game.hs
index 50d9dc1..7ed7ade 100644
--- a/src/Game.hs
+++ b/src/Game.hs
@@ -155,11 +155,9 @@ gameLoop e = do
SDL.rendererRenderTarget renderer $= Just canvas
SDL.clear renderer
- updatedEnv <-
- if state.gameOverDelay /= 1
- then playLoop updatedToasterEnv
- else gameOverLoop updatedToasterEnv
-
+ updatedEnv <- case state.playState of
+ GS.GameOver -> gameOverLoop updatedToasterEnv
+ _ -> playLoop updatedToasterEnv
T.render renderer updatedEnv.toaster
SDL.rendererRenderTarget renderer $= Nothing
@@ -199,11 +197,16 @@ playLoop e = do
-- update state counters, etc
updateState :: Env -> IO Env
updateState env
- | state.gameOverDelay > 1 = pure env {state = state {GS.gameOverDelay = state.gameOverDelay - 1}}
+ | state.gameOverDelay > 0 = do
+ let delay = state.gameOverDelay - 1
+ pure $
+ if delay > 0
+ then env {state = state {GS.gameOverDelay = delay}}
+ else env {state = state {GS.playState = GS.GameOver}}
| 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
+ | state.playState == GS.ExitDone = do
map' <- M.load (env.mapList !! (env.state.currentLevel + 1)) env.tsTexture
entities <- E.mkEntities env.sprites map' env.controlsRef
pure $
diff --git a/src/Game/Entities.hs b/src/Game/Entities.hs
index 3cb2510..6ddf589 100644
--- a/src/Game/Entities.hs
+++ b/src/Game/Entities.hs
@@ -71,7 +71,7 @@ addExit es x y = do
updateAll :: Entities -> GS.State -> IO (Entities, GS.State)
updateAll es state = do
-- update the player first (including the reference), unless the level is completed
- updatedPlayer <- if state.levelCompleted /= GS.ExitOff then pure player else player.update player
+ updatedPlayer <- if state.playState /= GS.InPlay then pure player else player.update player
void $ writeIORef es.player updatedPlayer
-- then the other entities
updated <- (updatedPlayer :) <$> traverse (updateFilter $ state.hitDelay > 0) others
@@ -111,12 +111,12 @@ updateAll es state = do
processActions s' ents' t
ActionExitStarted ->
processActions
- s {GS.levelCompleted = GS.ExitStarted}
+ s {GS.playState = GS.ExitStarted}
-- the player is not in the action, changing the type disables collision detection
((head ents) {typ = TypeEffect} : tail ents)
t
- ActionEntryDone -> processActions s {GS.levelCompleted = GS.ExitOff} ents t
- ActionExitDone -> processActions s {GS.levelCompleted = GS.ExitDone} ents t
+ ActionEntryDone -> processActions s {GS.playState = GS.InPlay} ents t
+ ActionExitDone -> processActions s {GS.playState = GS.ExitDone} ents t
ActionAddBlast x y d playerCollision isBlocked -> do
blast <- mkBlast es.sprites x y d playerCollision isBlocked
processActions s (ents ++ [blast]) t
@@ -156,7 +156,7 @@ render renderer es state = do
-- always render player last
-- won't draw all the frames if the player was hit
-- or we are exiting the level
- if testBit state.hitDelay 2 || state.levelCompleted /= GS.ExitOff then pure () else renderOne player
+ if testBit state.hitDelay 2 || state.playState /= GS.InPlay then pure () else renderOne player
where
player = head es.entities
others = tail es.entities
diff --git a/src/Game/State.hs b/src/Game/State.hs
index 083e6d4..df2d4fa 100644
--- a/src/Game/State.hs
+++ b/src/Game/State.hs
@@ -1,4 +1,4 @@
-module Game.State (State (..), initialState, levelState, maxLives, ExitState (..)) where
+module Game.State (State (..), initialState, levelState, maxLives, PlayState (..)) where
import Game.Entities.Const (hitDelay)
import qualified Game.Map as M
@@ -6,7 +6,7 @@ import qualified Game.Map as M
maxLives :: Int
maxLives = 4
-data ExitState = ExitOff | ExitEntry | ExitStarted | ExitDone deriving (Eq)
+data PlayState = InPlay | IntoStage | ExitStarted | ExitDone | GameOver deriving (Eq)
data State = State
{ batteries :: Int,
@@ -17,7 +17,7 @@ data State = State
gameOverDelay :: Int,
exit :: Bool,
lastBattery :: (Int, Int),
- levelCompleted :: ExitState,
+ playState :: PlayState,
currentLevel :: Int
}
@@ -33,7 +33,7 @@ initialState m =
exit = False,
-- doesn't matter where
lastBattery = (0, 0),
- levelCompleted = ExitEntry,
+ playState = IntoStage,
currentLevel = 0
}
@@ -47,5 +47,5 @@ levelState s m =
exit = False,
-- doesn't matter where
lastBattery = (0, 0),
- levelCompleted = ExitEntry
+ playState = IntoStage
}