From 11120a24b6927073d55d2e56b4b911817dedaae2 Mon Sep 17 00:00:00 2001 From: "Juan J. Martinez" Date: Wed, 15 Feb 2023 22:53:22 +0000 Subject: Game state, HUD, ... WIP Exploring ideas. --- src/Game.hs | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) (limited to 'src/Game.hs') diff --git a/src/Game.hs b/src/Game.hs index c267453..a7454b8 100644 --- a/src/Game.hs +++ b/src/Game.hs @@ -7,8 +7,10 @@ import Data.Text (pack) import Foreign.C.Types (CInt) import qualified Game.Controller as C import qualified Game.Entities as E +import qualified Game.Hud as H import qualified Game.Map as M import qualified Game.Sprites as S +import qualified Game.State as GS import Game.Utils (isPressed) import SDL (($=), ($~)) import qualified SDL @@ -19,7 +21,7 @@ name :: String name = "Haskell gamedev [Space Platformer]" gameWidth, gameHeight :: CInt -(gameWidth, gameHeight) = (320, 180) +(gameWidth, gameHeight) = (320, 192) gameScale :: CInt gameScale = 3 @@ -39,7 +41,9 @@ data Env = Env controls :: IORef C.Controls, map :: M.Map, sprites :: S.SpriteSheet, - entities :: IORef E.Entities + entities :: IORef E.Entities, + hud :: H.Hud, + state :: IORef GS.State } defaultRenderRect :: SDL.Rectangle CInt @@ -69,7 +73,14 @@ main = do controls <- newIORef =<< C.init map' <- M.load "data/map.json" tsTexture sprites <- S.load "data/sprites.json" ssTexture - entities <- newIORef =<< E.mkEntities sprites map' controls + state <- + newIORef + GS.State + { batteries = 0, + totalBatteries = M.totalBatteries map' + } + hud <- H.mkHud sprites state + entities <- newIORef =<< E.mkEntities sprites map' controls state runReaderT gameLoop Env @@ -81,7 +92,9 @@ main = do controls = controls, map = map', sprites = sprites, - entities = entities + entities = entities, + hud = hud, + state = state } SDL.destroyWindow window SDL.quit @@ -120,6 +133,7 @@ gameLoop = do controls = env.controls map' = env.map entities = env.entities + hud = env.hud events <- map SDL.eventPayload <$> SDL.pollEvents @@ -141,9 +155,11 @@ gameLoop = do -- render map and entities void $ liftIO $ do M.render renderer map' + H.render renderer hud E.render renderer updated SDL.rendererRenderTarget renderer $= Nothing + SDL.clear renderer rect <- SDL.get renderRect SDL.copy renderer canvas Nothing (Just rect) -- cgit v1.2.3