aboutsummaryrefslogtreecommitdiff
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
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!
-rw-r--r--src/Game/Entities.hs47
-rw-r--r--src/Game/Entities/Common.hs20
-rw-r--r--src/Game/Entities/Effect.hs2
-rw-r--r--src/Game/Entities/Pickup.hs14
-rw-r--r--src/Game/Entities/Player.hs4
-rw-r--r--src/Game/Entities/Robot.hs14
-rw-r--r--src/Game/Entities/Slime.hs14
-rw-r--r--src/Game/Entities/Types.hs6
8 files changed, 59 insertions, 62 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
diff --git a/src/Game/Entities/Common.hs b/src/Game/Entities/Common.hs
index ca5eb78..2e447f5 100644
--- a/src/Game/Entities/Common.hs
+++ b/src/Game/Entities/Common.hs
@@ -2,8 +2,6 @@ module Game.Entities.Common
( toSpriteSet,
frameLimit,
collision,
- hitPlayer,
- collectedBattery,
updateFrame,
updateGravity,
)
@@ -13,8 +11,6 @@ import Data.IORef
import Game.Entities.Const
import Game.Entities.Types
import qualified Game.Sprites as S
-import qualified Game.State as GS
-import SDL (($~))
-- | Convert direction into a sprite set.
toSpriteSet :: Dir -> Int
@@ -40,22 +36,6 @@ collision playerRef otherHeight other = do
&& player.y + otherHeight - 4 < other.y + otherHeight
&& other.y + 4 < player.y + 24
--- | Update game state to reflect that the player was hit by an enemy.
-hitPlayer :: IORef GS.State -> IO ()
-hitPlayer stateRef =
- stateRef $~ updatePlayerHit
- where
- updatePlayerHit :: GS.State -> GS.State
- updatePlayerHit s
- -- if we run out of lives, trigger the game over
- | s.lives == 1 = s {GS.lives = 0, GS.gameOverDelay = gameOverDelay}
- | otherwise = s {GS.lives = s.lives - 1, GS.hitDelay = hitDelay}
-
--- | Update game state to reflect that the player picked up a battery.
-collectedBattery :: IORef GS.State -> IO ()
-collectedBattery stateRef =
- stateRef $~ (\s -> s {GS.batteries = s.batteries + 1})
-
-- | Update frame animation for entities that have direction.
updateFrame :: Bool -> Entity -> Entity
updateFrame updated e
diff --git a/src/Game/Entities/Effect.hs b/src/Game/Entities/Effect.hs
index 0a20445..7a2b341 100644
--- a/src/Game/Entities/Effect.hs
+++ b/src/Game/Entities/Effect.hs
@@ -21,7 +21,7 @@ mkEffect sprites x y name = do
sprite = s,
update = pure . updateEffect,
destroy = False,
- spawns = []
+ actions = []
}
updateEffect :: Entity -> Entity
diff --git a/src/Game/Entities/Pickup.hs b/src/Game/Entities/Pickup.hs
index 4aa7326..7c1937c 100644
--- a/src/Game/Entities/Pickup.hs
+++ b/src/Game/Entities/Pickup.hs
@@ -5,8 +5,8 @@ import Game.Entities.Const
import Game.Entities.Types
import qualified Game.Sprites as S
-mkBattery :: S.SpriteSheet -> Int -> Int -> Collision -> IO () -> IO Entity
-mkBattery sprites x y playerCollision collectedBattery' = do
+mkBattery :: S.SpriteSheet -> Int -> Int -> Collision -> IO Entity
+mkBattery sprites x y playerCollision = do
s <- S.get sprites "battery"
pure
Entity
@@ -19,15 +19,15 @@ mkBattery sprites x y playerCollision collectedBattery' = do
gravity = gravityOff,
dir = DirRight,
sprite = s,
- update = updateBattery playerCollision collectedBattery',
+ update = updateBattery playerCollision,
destroy = False,
- spawns = []
+ actions = []
}
-updateBattery :: Collision -> IO () -> Entity -> IO Entity
-updateBattery touchedPlayer collectedBattery' e = do
+updateBattery :: Collision -> Entity -> IO Entity
+updateBattery touchedPlayer e = do
touched <- touchedPlayer e
- if touched then e {destroy = True} <$ collectedBattery' else pure updateBatteryFrame
+ pure $ if touched then e {destroy = True, actions = [ActionAddBattery]} else updateBatteryFrame
where
updateBatteryFrame :: Entity
updateBatteryFrame
diff --git a/src/Game/Entities/Player.hs b/src/Game/Entities/Player.hs
index f0a526c..87e8a7a 100644
--- a/src/Game/Entities/Player.hs
+++ b/src/Game/Entities/Player.hs
@@ -23,7 +23,7 @@ mkPlayer sprites x y controls isBlocked = do
sprite = s,
update = updatePlayer controls isBlocked,
destroy = False,
- spawns = []
+ actions = []
}
updateHorizontal :: IsBlocked -> Bool -> Bool -> Entity -> Entity
@@ -48,7 +48,7 @@ updateVertical isBlocked jump down e
-- make jumping easier with "Coyote time"
|| (e.gravity /= gravityOff && (e.gravity < gravityDown || e.gravity > jumpLimit)) =
e
- | not down = e {jumping = True, gravity = gravityUp, frame = jumpFrame, spawns = e.spawns ++ [DustEffectSpawn e.x (e.y + 8)]}
+ | not down = e {jumping = True, gravity = gravityUp, frame = jumpFrame, actions = [ActionAddDustEffect e.x (e.y + 8)]}
-- go down a 8 pixel tall platform; not ideal to have these values hardcoded here
-- but to be fair, the player height/width is hardcoded as well
| down
diff --git a/src/Game/Entities/Robot.hs b/src/Game/Entities/Robot.hs
index 39b63aa..dd3404a 100644
--- a/src/Game/Entities/Robot.hs
+++ b/src/Game/Entities/Robot.hs
@@ -6,8 +6,8 @@ import Game.Entities.Const
import Game.Entities.Types
import qualified Game.Sprites as S
-mkRobot :: S.SpriteSheet -> Int -> Int -> Collision -> IsBlocked -> IO () -> IO Entity
-mkRobot sprites x y playerCollision isBlocked hitPlayer' = do
+mkRobot :: S.SpriteSheet -> Int -> Int -> Collision -> IsBlocked -> IO Entity
+mkRobot sprites x y playerCollision isBlocked = do
s <- S.get sprites "robot"
pure
Entity
@@ -20,16 +20,16 @@ mkRobot sprites x y playerCollision isBlocked hitPlayer' = do
gravity = gravityOff,
dir = DirRight,
sprite = s,
- update = updateRobot playerCollision isBlocked hitPlayer',
+ update = updateRobot playerCollision isBlocked,
destroy = False,
- spawns = []
+ actions = []
}
-updateRobot :: Collision -> IsBlocked -> IO () -> Entity -> IO Entity
-updateRobot touchedPlayer isBlocked hitPlayer' e = do
+updateRobot :: Collision -> IsBlocked -> Entity -> IO Entity
+updateRobot touchedPlayer isBlocked e = do
touched <- touchedPlayer e
let updated = updateFrame True e
- if touched then fmap (const e) hitPlayer' else pure $ updateMovement updated
+ pure $ if touched then updated {actions = [ActionHitPlayer]} else updateMovement updated
where
updateMovement :: Entity -> Entity
updateMovement ent
diff --git a/src/Game/Entities/Slime.hs b/src/Game/Entities/Slime.hs
index 5ca6fe7..1f50927 100644
--- a/src/Game/Entities/Slime.hs
+++ b/src/Game/Entities/Slime.hs
@@ -6,8 +6,8 @@ import Game.Entities.Const
import Game.Entities.Types
import qualified Game.Sprites as S
-mkSlime :: S.SpriteSheet -> Int -> Int -> Collision -> IsBlocked -> IO () -> IO Entity
-mkSlime sprites x y playerCollision isBlocked hitPlayer' = do
+mkSlime :: S.SpriteSheet -> Int -> Int -> Collision -> IsBlocked -> IO Entity
+mkSlime sprites x y playerCollision isBlocked = do
s <- S.get sprites "slime"
pure
Entity
@@ -20,16 +20,16 @@ mkSlime sprites x y playerCollision isBlocked hitPlayer' = do
gravity = gravityOff,
dir = DirRight,
sprite = s,
- update = updateSlime playerCollision isBlocked hitPlayer',
+ update = updateSlime playerCollision isBlocked,
destroy = False,
- spawns = []
+ actions = []
}
-updateSlime :: Collision -> IsBlocked -> IO () -> Entity -> IO Entity
-updateSlime touchedPlayer isBlocked hitPlayer' e = do
+updateSlime :: Collision -> IsBlocked -> Entity -> IO Entity
+updateSlime touchedPlayer isBlocked e = do
touched <- touchedPlayer e
let updated = updateSlimeFrame
- if touched then fmap (const e) hitPlayer' else pure $ updateMovement updated
+ pure $ if touched then updated {actions = [ActionHitPlayer]} else updateMovement updated
where
updateMovement :: Entity -> Entity
updateMovement ent
diff --git a/src/Game/Entities/Types.hs b/src/Game/Entities/Types.hs
index a50ab2d..e22032b 100644
--- a/src/Game/Entities/Types.hs
+++ b/src/Game/Entities/Types.hs
@@ -4,7 +4,7 @@ module Game.Entities.Types
Collision,
IsBlocked,
Entities (..),
- Spawn (..),
+ Action (..),
Entity (..),
)
where
@@ -28,7 +28,7 @@ data Entities = Entities
entities :: [Entity]
}
-data Spawn = DustEffectSpawn Int Int
+data Action = ActionAddDustEffect Int Int | ActionAddBattery | ActionHitPlayer deriving (Show)
data Entity = Entity
{ typ :: Type,
@@ -42,5 +42,5 @@ data Entity = Entity
sprite :: S.Sprite,
update :: Entity -> IO Entity,
destroy :: Bool,
- spawns :: [Spawn]
+ actions :: [Action]
}