From 7b3aa61462dc5a1135e01beedcbc98efe47ffb13 Mon Sep 17 00:00:00 2001 From: "Juan J. Martinez" Date: Sat, 15 Apr 2023 22:59:15 +0100 Subject: "Toaster" notifications To provide feedback to the user when a controller is plugged/unplugged. --- src/Game.hs | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) (limited to 'src/Game.hs') diff --git a/src/Game.hs b/src/Game.hs index c2f6083..d0092b6 100644 --- a/src/Game.hs +++ b/src/Game.hs @@ -12,6 +12,7 @@ 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 qualified Game.Toaster as T import SDL (($=)) import qualified SDL import qualified SDL.Image @@ -45,8 +46,9 @@ data Env = Env font :: BF.BitmapFont, entities :: E.Entities, hud :: H.Hud, + toaster :: T.Toaster, state :: GS.State, - controls :: IORef C.Controls + controlsRef :: IORef C.Controls } defaultRenderRect :: SDL.Rectangle CInt @@ -76,9 +78,10 @@ main = do map' <- M.load (head mapList) tsTexture sprites <- S.load "data/sprites.json" ssTexture font <- BF.load "data/font.json" bfTexture - controls <- newIORef C.init - entities <- E.mkEntities sprites map' controls + controlsRef <- newIORef C.init + entities <- E.mkEntities sprites map' controlsRef hud <- H.mkHud sprites font + toaster <- T.mkToaster font (fromIntegral gameHeight) gameLoop Env { window = window, @@ -93,8 +96,9 @@ main = do font = font, entities = entities, hud = hud, + toaster = toaster, state = GS.initialState map', - controls = controls + controlsRef = controlsRef } SDL.destroyWindow window SDL.quit @@ -133,22 +137,30 @@ gameLoop e = do let renderer = env.renderer canvas = env.canvas renderRect = env.renderRect - controls = env.controls + controlsRef = env.controlsRef state = env.state -- ESC or close the window to quit let quit = fromMaybe False (C.isPressed SDL.KeycodeEscape events) || SDL.QuitEvent `elem` events unless quit $ do -- update controls - writeIORef controls =<< (`C.update` events) =<< readIORef controls + updatedToasterEnv <- do + ctl <- readIORef controlsRef + C.update ctl env.toaster events + >>= ( \(ctl', toaster) -> do + writeIORef controlsRef ctl' + pure env {toaster = T.update toaster} + ) SDL.rendererRenderTarget renderer $= Just canvas SDL.clear renderer updatedEnv <- if state.gameOverDelay /= 1 - then playLoop =<< updateState env - else gameOverLoop env + then playLoop =<< updateState updatedToasterEnv + else gameOverLoop updatedToasterEnv + + T.render renderer updatedEnv.toaster SDL.rendererRenderTarget renderer $= Nothing SDL.clear renderer @@ -167,7 +179,7 @@ gameLoop e = do pure env {entities = es, state = state {GS.exit = True}} | state.levelCompleted == GS.ExitDone = do map' <- M.load (env.mapList !! (env.state.currentLevel + 1)) env.tsTexture - entities <- E.mkEntities env.sprites map' env.controls + entities <- E.mkEntities env.sprites map' env.controlsRef pure $ env { map = map', @@ -211,14 +223,14 @@ gameOverLoop e = do hud = e.hud font = e.font map' = e.map - controls = e.controls + controlsRef = e.controlsRef - ctl <- readIORef controls + ctl <- readIORef controlsRef if ctl.a then do -- retry last level - entities <- E.mkEntities sprites map' controls + entities <- E.mkEntities sprites map' controlsRef pure e {state = (GS.levelState e.state map') {GS.lives = GS.maxLives}, entities = entities} else do -- cgit v1.2.3