From ef0a75834870eced4371edd6d50916d4e1bd432f Mon Sep 17 00:00:00 2001 From: "Juan J. Martinez" Date: Fri, 3 Mar 2023 12:57:17 +0000 Subject: Simple game over menu --- src/Game.hs | 50 +++++++++++++++++++++++++++++++++++--------------- 1 file changed, 35 insertions(+), 15 deletions(-) (limited to 'src/Game.hs') diff --git a/src/Game.hs b/src/Game.hs index 851b7fc..1d93065 100644 --- a/src/Game.hs +++ b/src/Game.hs @@ -8,6 +8,7 @@ import Foreign.C.Types (CInt) import qualified Game.BitmapFont as BF import qualified Game.Controller as C import qualified Game.Entities as E +import Game.Entities.Const (hitDelay) import qualified Game.Hud as H import qualified Game.Map as M import qualified Game.Sprites as S @@ -54,6 +55,17 @@ data Env = Env defaultRenderRect :: SDL.Rectangle CInt defaultRenderRect = SDL.Rectangle (SDL.P $ V2 0 0) (V2 windowWidth windowHeight) +initialState :: M.Map -> GS.State +initialState m = + GS.State + { batteries = 0, + totalBatteries = M.totalBatteries m, + lives = maxLives, + totalLives = maxLives, + hitDelay = hitDelay, + gameOverDelay = 0 + } + main :: IO () main = do SDL.initialize [SDL.InitVideo, SDL.InitGameController] @@ -80,15 +92,6 @@ main = do controls <- newIORef =<< C.init entities <- E.mkEntities sprites map' controls hud <- H.mkHud sprites - let initialState = - GS.State - { batteries = 0, - totalBatteries = M.totalBatteries map', - lives = maxLives, - totalLives = maxLives, - hitDelay = 0, - gameOverDelay = 0 - } gameLoop Env { window = window, @@ -101,7 +104,7 @@ main = do font = font, entities = entities, hud = hud, - state = initialState, + state = initialState map', controls = controls } SDL.destroyWindow window @@ -198,8 +201,25 @@ gameOverLoop e = do sprites = e.sprites state = e.state hud = e.hud - - H.render renderer hud state - title <- S.get sprites "game-over" - S.render renderer title 112 80 0 0 - pure e + font = e.font + map' = e.map + controls = e.controls + + ctl <- readIORef controls + + if ctl.a + then do + -- retry last level + entities <- E.mkEntities sprites map' controls + pure e {state = initialState map', entities = entities} + else do + H.render renderer hud state + title <- S.get sprites "game-over" + S.render renderer title 112 80 0 0 + + BF.renderText renderer font 109 116 "Press to retry" + button <- S.get sprites "ui" + S.render renderer button 143 113 0 1 + + BF.renderText renderer font 112 132 "(or ESC to quit)" + pure e -- cgit v1.2.3