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 --- data/sprites.json | 11 +++++++++++ data/sprites.png | Bin 15735 -> 16391 bytes src/Game.hs | 50 +++++++++++++++++++++++++++++++++++--------------- 3 files changed, 46 insertions(+), 15 deletions(-) diff --git a/data/sprites.json b/data/sprites.json index bc9a930..9132500 100644 --- a/data/sprites.json +++ b/data/sprites.json @@ -5,6 +5,17 @@ ], "sets": [[0]] }, + "ui": { + "frames": [ + { "x": 0, "y": 208, "width": 16, "height": 16 }, + { "x": 16, "y": 208, "width": 16, "height": 16 }, + { "x": 32, "y": 208, "width": 16, "height": 16 } + ], + "sets": [ + [0, 1, 2] + ] + }, + "player": { "frames": [ { "x": 0, "y": 0, "width": 16, "height": 24 }, diff --git a/data/sprites.png b/data/sprites.png index ee964db..0648070 100644 Binary files a/data/sprites.png and b/data/sprites.png differ 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