From 1d68ac340c28792b2b498e204ef123730d901443 Mon Sep 17 00:00:00 2001 From: "Juan J. Martinez" Date: Thu, 16 Feb 2023 21:11:18 +0000 Subject: Added lives to the HUD --- data/sprites.json | 6 ++++-- data/sprites.png | Bin 10300 -> 10424 bytes src/Game.hs | 7 ++++++- src/Game/Hud.hs | 4 +++- src/Game/State.hs | 4 +++- 5 files changed, 16 insertions(+), 5 deletions(-) diff --git a/data/sprites.json b/data/sprites.json index e31c02e..a911207 100644 --- a/data/sprites.json +++ b/data/sprites.json @@ -37,10 +37,12 @@ "hud": { "frames": [ { "x": 48, "y": 24, "width": 8, "height": 8 }, - { "x": 56, "y": 24, "width": 8, "height": 8 } + { "x": 56, "y": 24, "width": 8, "height": 8 }, + { "x": 48, "y": 32, "width": 8, "height": 8 }, + { "x": 56, "y": 32, "width": 8, "height": 8 } ], "sets": [ - [0, 1] + [0, 1, 2, 3] ] } } diff --git a/data/sprites.png b/data/sprites.png index 743964a..6b84a40 100644 Binary files a/data/sprites.png and b/data/sprites.png differ diff --git a/src/Game.hs b/src/Game.hs index a7454b8..bc604ad 100644 --- a/src/Game.hs +++ b/src/Game.hs @@ -26,6 +26,9 @@ gameWidth, gameHeight :: CInt gameScale :: CInt gameScale = 3 +maxLives :: Int +maxLives = 4 + windowWidth, windowHeight :: CInt (windowWidth, windowHeight) = (gameWidth * gameScale, gameHeight * gameScale) @@ -77,7 +80,9 @@ main = do newIORef GS.State { batteries = 0, - totalBatteries = M.totalBatteries map' + totalBatteries = M.totalBatteries map', + lives = maxLives, + totalLives = maxLives } hud <- H.mkHud sprites state entities <- newIORef =<< E.mkEntities sprites map' controls state diff --git a/src/Game/Hud.hs b/src/Game/Hud.hs index 3b59558..dd72bb3 100644 --- a/src/Game/Hud.hs +++ b/src/Game/Hud.hs @@ -19,4 +19,6 @@ render :: SDL.Renderer -> Hud -> IO () render renderer hud = do state <- readIORef hud.stateRef let xs = [0 .. state.totalBatteries - 1] - mapM_ (\x -> S.render renderer hud.sprite (4 + x * 8) 178 0 (if state.batteries <= x then 0 else 1)) xs + in mapM_ (\x -> S.render renderer hud.sprite (4 + x * 8) 178 0 (if state.batteries <= x then 0 else 1)) xs + let xs = [0 .. state.totalLives - 1] + in mapM_ (\x -> S.render renderer hud.sprite (320 - 4 - state.totalLives * 8 + x * 8) 178 0 (if state.lives <= x then 2 else 3)) xs diff --git a/src/Game/State.hs b/src/Game/State.hs index 9988936..2223434 100644 --- a/src/Game/State.hs +++ b/src/Game/State.hs @@ -2,5 +2,7 @@ module Game.State (State (..)) where data State = State { batteries :: Int, - totalBatteries :: Int + totalBatteries :: Int, + lives :: Int, + totalLives :: Int } -- cgit v1.2.3