aboutsummaryrefslogtreecommitdiff
path: root/src/Game/Hud.hs
blob: dd72bb37369ba63a75d09b424b16b3d431c71535 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
module Game.Hud (Hud, mkHud, render) where

import Data.IORef
import qualified Game.Sprites as S
import qualified Game.State as GS
import qualified SDL

data Hud = Hud
  { sprite :: S.Sprite,
    stateRef :: IORef GS.State
  }

mkHud :: S.SpriteSheet -> IORef GS.State -> IO Hud
mkHud sprites stateRef = do
  sprite <- S.get sprites "hud"
  pure Hud {sprite = sprite, stateRef = stateRef}

render :: SDL.Renderer -> Hud -> IO ()
render renderer hud = do
  state <- readIORef hud.stateRef
  let xs = [0 .. state.totalBatteries - 1]
   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