aboutsummaryrefslogtreecommitdiff
path: root/src/Game
diff options
context:
space:
mode:
authorJuan J. Martinez <jjm@usebox.net>2023-03-10 23:53:40 +0000
committerJuan J. Martinez <jjm@usebox.net>2023-03-10 23:53:40 +0000
commit45a103da2133b645fac9617cc97c11901d3e258f (patch)
tree0cad00427725ecd1067f64a99a25e78d39e72409 /src/Game
parent7a714cc457323bff6b4a73ec9e7b58c7a63381ec (diff)
downloadspace-plat-hs-45a103da2133b645fac9617cc97c11901d3e258f.tar.gz
space-plat-hs-45a103da2133b645fac9617cc97c11901d3e258f.zip
Change stage
Diffstat (limited to 'src/Game')
-rw-r--r--src/Game/Entities.hs1
-rw-r--r--src/Game/Entities/Exit.hs20
-rw-r--r--src/Game/Entities/Types.hs2
-rw-r--r--src/Game/Hud.hs13
-rw-r--r--src/Game/State.hs4
5 files changed, 27 insertions, 13 deletions
diff --git a/src/Game/Entities.hs b/src/Game/Entities.hs
index 21b6ba5..bd9729b 100644
--- a/src/Game/Entities.hs
+++ b/src/Game/Entities.hs
@@ -98,6 +98,7 @@ updateAll es state = do
ents
)
processActions s' ents' t
+ ActionExitLevel -> processActions s {GS.levelCompleted = True} ents t
processActions s ents [] = pure (s, ents)
-- Update entities skipping enemies if the player was hit
diff --git a/src/Game/Entities/Exit.hs b/src/Game/Entities/Exit.hs
index 075e1f4..12f160d 100644
--- a/src/Game/Entities/Exit.hs
+++ b/src/Game/Entities/Exit.hs
@@ -19,13 +19,21 @@ mkExit sprites x y playerCollision = do
gravity = gravityOff,
dir = DirRight,
sprite = s,
- update = pure . updateExit playerCollision,
+ update = updateExit playerCollision,
destroy = False,
actions = []
}
-updateExit :: Collision -> Entity -> Entity
-updateExit _ e
- | e.delay > 0 = e {delay = e.delay - 1}
- | e.frame + 1 < frameLimit e = e {delay = frameDelay, frame = e.frame + 1}
- | otherwise = e {delay = frameDelay, frame = 7}
+updateExit :: Collision -> Entity -> IO Entity
+updateExit touchedPlayer e = do
+ touched <- if e.frame < fullyOpen then pure False else touchedPlayer e
+ pure $ if touched then e {destroy = True, actions = [ActionExitLevel]} else update
+ where
+ fullyOpen :: Int
+ fullyOpen = 7
+
+ update :: Entity
+ update
+ | e.delay > 0 = e {delay = e.delay - 1}
+ | e.frame + 1 < frameLimit e = e {delay = frameDelay, frame = e.frame + 1}
+ | otherwise = e {delay = frameDelay, frame = fullyOpen}
diff --git a/src/Game/Entities/Types.hs b/src/Game/Entities/Types.hs
index 64b4637..392a86f 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 Int Int | ActionHitPlayer
+data Action = ActionAddEffect Int Int EffectName | ActionAddBattery Int Int | ActionHitPlayer | ActionExitLevel
data Entity = Entity
{ typ :: Type,
diff --git a/src/Game/Hud.hs b/src/Game/Hud.hs
index a5d3e22..c4d5234 100644
--- a/src/Game/Hud.hs
+++ b/src/Game/Hud.hs
@@ -1,5 +1,6 @@
module Game.Hud (Hud, mkHud, render, height) where
+import qualified Game.BitmapFont as BM
import qualified Game.Sprites as S
import qualified Game.State as GS
import qualified SDL
@@ -7,16 +8,18 @@ import qualified SDL
height :: Int
height = 16
-newtype Hud = Hud S.Sprite
+data Hud = Hud S.Sprite BM.BitmapFont
-mkHud :: S.SpriteSheet -> IO Hud
-mkHud sprites = do
- Hud <$> S.get sprites "hud"
+mkHud :: S.SpriteSheet -> BM.BitmapFont -> IO Hud
+mkHud sprites font = do
+ sprite <- S.get sprites "hud"
+ pure $ Hud sprite font
render :: SDL.Renderer -> Hud -> GS.State -> IO ()
-render renderer (Hud sprite) state = do
+render renderer (Hud sprite font) state = do
let xs = [0 .. state.totalBatteries - 1]
in mapM_ (\x -> S.render renderer sprite (4 + x * 8) 4 0 (if state.batteries <= x then 0 else 1)) xs
let xs = [0 .. state.totalLives - 1]
in -- magic numbers
mapM_ (\x -> S.render renderer sprite (320 - 4 - state.totalLives * 8 + x * 8) 4 0 (if state.lives <= x then 2 else 3)) xs
+ BM.renderText renderer font 140 4 $ "Stage " ++ show (state.currentLevel + 1)
diff --git a/src/Game/State.hs b/src/Game/State.hs
index 840c0ae..e05fb61 100644
--- a/src/Game/State.hs
+++ b/src/Game/State.hs
@@ -8,5 +8,7 @@ data State = State
hitDelay :: Int,
gameOverDelay :: Int,
exit :: Bool,
- lastBattery :: (Int, Int)
+ lastBattery :: (Int, Int),
+ levelCompleted :: Bool,
+ currentLevel :: Int
}