aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJuan J. Martinez <jjm@usebox.net>2023-03-05 14:55:25 +0000
committerJuan J. Martinez <jjm@usebox.net>2023-03-09 12:21:05 +0000
commit7a714cc457323bff6b4a73ec9e7b58c7a63381ec (patch)
tree23305ab55b3c79fb6bb62c3df56baa01b28890fe
parentbeecc1e2af08f81f5cb5d2ebb93aaef59b20fcd5 (diff)
downloadspace-plat-hs-7a714cc457323bff6b4a73ec9e7b58c7a63381ec.tar.gz
space-plat-hs-7a714cc457323bff6b4a73ec9e7b58c7a63381ec.zip
Exit placement on the last battery
-rw-r--r--src/Game.hs9
-rw-r--r--src/Game/Entities.hs10
-rw-r--r--src/Game/Entities/Pickup.hs2
-rw-r--r--src/Game/Entities/Types.hs2
-rw-r--r--src/Game/State.hs3
5 files changed, 14 insertions, 12 deletions
diff --git a/src/Game.hs b/src/Game.hs
index 850d6ed..31aa40b 100644
--- a/src/Game.hs
+++ b/src/Game.hs
@@ -63,7 +63,9 @@ initialState m =
totalLives = maxLives,
hitDelay = hitDelay,
gameOverDelay = 0,
- exit = False
+ exit = False,
+ -- doesn't matter where
+ lastBattery = (0, 0)
}
main :: IO ()
@@ -169,16 +171,17 @@ gameLoop e = do
gameLoop updatedEnv
where
- -- update state counters
+ -- 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
+ es <- E.addExit env.entities x (y - 8) -- adjusted to player's height
pure env {entities = es, state = state {GS.exit = True}}
| otherwise = pure env
where
state = env.state
+ (x, y) = state.lastBattery
playLoop :: Env -> IO Env
playLoop e = do
diff --git a/src/Game/Entities.hs b/src/Game/Entities.hs
index 2cfdd76..21b6ba5 100644
--- a/src/Game/Entities.hs
+++ b/src/Game/Entities.hs
@@ -53,12 +53,10 @@ playerPosition (Entities _ _ entities) =
where
player = head entities
-addExit :: Entities -> IO Entities
-addExit es = do
+addExit :: Entities -> Int -> Int -> IO Entities
+addExit es x y = do
exit <- mkExit es.sprites x y (collision es.player 24)
pure es {entities = es.entities ++ [exit]}
- where
- (x, y) = playerPosition es
updateAll :: Entities -> GS.State -> IO (Entities, GS.State)
updateAll es state = do
@@ -86,8 +84,8 @@ updateAll es state = do
ActionAddEffect x y name -> do
effect <- mkEffect es.sprites x y name
processActions s (ents ++ [effect]) t
- ActionAddBattery ->
- processActions s {GS.batteries = s.batteries + 1} ents t
+ ActionAddBattery x y ->
+ processActions s {GS.batteries = s.batteries + 1, GS.lastBattery = (x, y)} ents t
ActionHitPlayer -> do
let (s', ents') =
if s.lives == 1
diff --git a/src/Game/Entities/Pickup.hs b/src/Game/Entities/Pickup.hs
index 7c1937c..a933efc 100644
--- a/src/Game/Entities/Pickup.hs
+++ b/src/Game/Entities/Pickup.hs
@@ -27,7 +27,7 @@ mkBattery sprites x y playerCollision = do
updateBattery :: Collision -> Entity -> IO Entity
updateBattery touchedPlayer e = do
touched <- touchedPlayer e
- pure $ if touched then e {destroy = True, actions = [ActionAddBattery]} else updateBatteryFrame
+ pure $ if touched then e {destroy = True, actions = [ActionAddBattery e.x e.y]} else updateBatteryFrame
where
updateBatteryFrame :: Entity
updateBatteryFrame
diff --git a/src/Game/Entities/Types.hs b/src/Game/Entities/Types.hs
index dce4ffa..64b4637 100644
--- a/src/Game/Entities/Types.hs
+++ b/src/Game/Entities/Types.hs
@@ -29,7 +29,7 @@ data Entities = Entities
-- | The effect name must match the sprite name in the spritesheet.
type EffectName = String
-data Action = ActionAddEffect Int Int EffectName | ActionAddBattery | ActionHitPlayer
+data Action = ActionAddEffect Int Int EffectName | ActionAddBattery Int Int | ActionHitPlayer
data Entity = Entity
{ typ :: Type,
diff --git a/src/Game/State.hs b/src/Game/State.hs
index 8987a2e..840c0ae 100644
--- a/src/Game/State.hs
+++ b/src/Game/State.hs
@@ -7,5 +7,6 @@ data State = State
totalLives :: Int,
hitDelay :: Int,
gameOverDelay :: Int,
- exit :: Bool
+ exit :: Bool,
+ lastBattery :: (Int, Int)
}