aboutsummaryrefslogtreecommitdiff
path: root/src/Game/Entities
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
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')
-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
7 files changed, 27 insertions, 47 deletions
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]
}