aboutsummaryrefslogtreecommitdiff
path: root/src/Game/Entities.hs
diff options
context:
space:
mode:
authorJuan J. Martinez <jjm@usebox.net>2023-03-01 23:11:31 +0000
committerJuan J. Martinez <jjm@usebox.net>2023-03-01 23:11:31 +0000
commit1b05eefc7a5a4fb688d7b631529324ca08ed1a16 (patch)
tree52f5e126b2af0aa519a30040191a1cbdbc12f8e8 /src/Game/Entities.hs
parent6f9e98cc5cefa9b35f139d3715339d676eff18ed (diff)
downloadspace-plat-hs-1b05eefc7a5a4fb688d7b631529324ca08ed1a16.tar.gz
space-plat-hs-1b05eefc7a5a4fb688d7b631529324ca08ed1a16.zip
Untangle state changes into actions
WIP: still unclear if we really need the IORefs!
Diffstat (limited to 'src/Game/Entities.hs')
-rw-r--r--src/Game/Entities.hs47
1 files changed, 32 insertions, 15 deletions
diff --git a/src/Game/Entities.hs b/src/Game/Entities.hs
index 4e5a686..4d8b12b 100644
--- a/src/Game/Entities.hs
+++ b/src/Game/Entities.hs
@@ -17,6 +17,7 @@ import Game.Entities.Types
import qualified Game.Map as M
import qualified Game.Sprites as S
import qualified Game.State as GS
+import SDL (($~))
import qualified SDL
mkEntities :: S.SpriteSheet -> M.Map -> IORef C.Controls -> IORef GS.State -> IO Entities
@@ -30,9 +31,9 @@ mkEntities sprites m controls stateRef = do
pure $ Entities sprites playerRef stateRef (player : entities)
where
toEntity :: IORef Entity -> M.Object -> IO Entity
- toEntity playerRef (M.SlimeEntity x y) = mkSlime sprites x y (collision playerRef 16) (M.isBlocked m) (hitPlayer stateRef)
- toEntity playerRef (M.RobotEntity x y) = mkRobot sprites x y (collision playerRef 24) (M.isBlocked m) (hitPlayer stateRef)
- toEntity playerRef (M.BatteryEntity x y) = mkBattery sprites x y (collision playerRef 16) (collectedBattery stateRef)
+ toEntity playerRef (M.SlimeEntity x y) = mkSlime sprites x y (collision playerRef 16) (M.isBlocked m)
+ toEntity playerRef (M.RobotEntity x y) = mkRobot sprites x y (collision playerRef 24) (M.isBlocked m)
+ toEntity playerRef (M.BatteryEntity x y) = mkBattery sprites x y (collision playerRef 16)
toEntity _ (M.PlayerEntity _ _) = error "Player already processed"
-- | Return the player's entity position (x, y).
@@ -42,32 +43,48 @@ playerPosition (Entities _ _ _ entities) =
where
player = head entities
-processSpawn :: S.SpriteSheet -> Spawn -> IO Entity
-processSpawn sprites (DustEffectSpawn x y) = mkEffect sprites x y "dust"
-
updateAll :: Entities -> IO Entities
updateAll es = do
-- update the player first (including the reference)
updatedPlayer <- player.update player
void $ writeIORef es.player updatedPlayer
- state <- readIORef es.state
+ state <- readIORef stateRef
-- update hit delay if the player was hit
let playerWasHit = state.hitDelay > 0
- when playerWasHit (writeIORef es.state state {GS.hitDelay = state.hitDelay - 1})
+ when playerWasHit (writeIORef stateRef state {GS.hitDelay = state.hitDelay - 1})
-- then the other entities
updated <- (updatedPlayer :) <$> traverse (updateFilter playerWasHit) others
- -- collect new entities
- new <- traverse (processSpawn es.sprites) (concatMap (\e -> e.spawns) updated)
- -- is the player dead?
- updated' <- do
- s <- readIORef es.state
- pure $ if s.lives == 0 && updatedPlayer.dir /= Dying then (head updated) {dir = Dying, gravity = gravityUp, frame = 0} : tail updated else updated
+ -- process actions
+ updated' <- processActions updated (concatMap (\e -> e.actions) updated)
-- clear spawns (new entities), filter out destroyed entities, and add the new ones
- pure es {entities = map (\e -> e {spawns = []}) (filter (\e -> not e.destroy) updated') ++ new}
+ pure es {entities = map (\e -> e {actions = []}) (filter (\e -> not e.destroy) updated')}
where
+ stateRef = es.state
player = head es.entities
others = tail es.entities
+ processActions :: [Entity] -> [Action] -> IO [Entity]
+ processActions ents (a : t) =
+ case a of
+ ActionAddDustEffect x y -> do
+ effect <- mkEffect es.sprites x y "dust"
+ processActions (ents ++ [effect]) t
+ ActionAddBattery -> do
+ stateRef $~ (\s -> s {GS.batteries = s.batteries + 1})
+ processActions ents t
+ ActionHitPlayer -> do
+ s <- readIORef stateRef
+ ents' <-
+ if s.lives == 1
+ then do
+ writeIORef stateRef s {GS.lives = 0, GS.gameOverDelay = gameOverDelay}
+ pure $ (head ents) {dir = Dying, gravity = gravityUp, frame = 0} : tail ents
+ else do
+ writeIORef stateRef s {GS.lives = s.lives - 1, GS.hitDelay = hitDelay}
+ pure ents
+ processActions ents' t
+ processActions ents [] = pure ents
+
-- Update entities skipping enemies if the player was hit.
updateFilter :: Bool -> Entity -> IO Entity
updateFilter False e = e.update e