From e5cbf917ad8b12b48932bc4fa13a044bc8159a74 Mon Sep 17 00:00:00 2001 From: "Juan J. Martinez" Date: Thu, 2 Mar 2023 12:54:47 +0000 Subject: Even less IORef Also reviewed comments and made the action to add effects more general. --- src/Game/Hud.hs | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) (limited to 'src/Game/Hud.hs') diff --git a/src/Game/Hud.hs b/src/Game/Hud.hs index 880de50..a5d3e22 100644 --- a/src/Game/Hud.hs +++ b/src/Game/Hud.hs @@ -1,6 +1,5 @@ module Game.Hud (Hud, mkHud, render, height) where -import Data.IORef import qualified Game.Sprites as S import qualified Game.State as GS import qualified SDL @@ -8,21 +7,16 @@ import qualified SDL height :: Int height = 16 -data Hud = Hud - { sprite :: S.Sprite, - stateRef :: IORef GS.State - } +newtype Hud = Hud S.Sprite -mkHud :: S.SpriteSheet -> IORef GS.State -> IO Hud -mkHud sprites stateRef = do - sprite <- S.get sprites "hud" - pure Hud {sprite = sprite, stateRef = stateRef} +mkHud :: S.SpriteSheet -> IO Hud +mkHud sprites = do + Hud <$> S.get sprites "hud" -render :: SDL.Renderer -> Hud -> IO () -render renderer hud = do - state <- readIORef hud.stateRef +render :: SDL.Renderer -> Hud -> GS.State -> IO () +render renderer (Hud sprite) state = do let xs = [0 .. state.totalBatteries - 1] - in mapM_ (\x -> S.render renderer hud.sprite (4 + x * 8) 4 0 (if state.batteries <= x then 0 else 1)) xs + 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 hud.sprite (320 - 4 - state.totalLives * 8 + x * 8) 4 0 (if state.lives <= x then 2 else 3)) xs + mapM_ (\x -> S.render renderer sprite (320 - 4 - state.totalLives * 8 + x * 8) 4 0 (if state.lives <= x then 2 else 3)) xs -- cgit v1.2.3