From edc6b611de752a224082583cd00d19ee6da04c5e Mon Sep 17 00:00:00 2001 From: "Juan J. Martinez" Date: Sat, 11 Mar 2023 22:21:06 +0000 Subject: Tidy up, state functions to state module --- src/Game.hs | 39 +++------------------------------------ src/Game/State.hs | 37 ++++++++++++++++++++++++++++++++++++- 2 files changed, 39 insertions(+), 37 deletions(-) diff --git a/src/Game.hs b/src/Game.hs index 7951d09..8c416e3 100644 --- a/src/Game.hs +++ b/src/Game.hs @@ -8,7 +8,6 @@ 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 @@ -27,9 +26,6 @@ gameWidth, gameHeight :: CInt gameScale :: CInt gameScale = 3 -maxLives :: Int -maxLives = 4 - windowWidth, windowHeight :: CInt (windowWidth, windowHeight) = (gameWidth * gameScale, gameHeight * gameScale) @@ -59,35 +55,6 @@ 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, - exit = False, - -- doesn't matter where - lastBattery = (0, 0), - levelCompleted = False, - currentLevel = 0 - } - -levelState :: GS.State -> M.Map -> GS.State -levelState s m = - s - { GS.batteries = 0, - GS.totalBatteries = M.totalBatteries m, - GS.hitDelay = hitDelay, - GS.gameOverDelay = 0, - GS.exit = False, - -- doesn't matter where - GS.lastBattery = (0, 0), - GS.levelCompleted = False - } - main :: IO () main = do SDL.initialize [SDL.InitVideo, SDL.InitGameController] @@ -127,7 +94,7 @@ main = do font = font, entities = entities, hud = hud, - state = initialState map', + state = GS.initialState map', controls = controls } SDL.destroyWindow window @@ -205,7 +172,7 @@ gameLoop e = do pure $ env { map = map', - state = (levelState env.state map') {GS.currentLevel = env.state.currentLevel + 1}, + state = (GS.levelState env.state map') {GS.currentLevel = env.state.currentLevel + 1}, entities = entities } | otherwise = pure env @@ -254,7 +221,7 @@ gameOverLoop e = do -- retry last level entities <- E.mkEntities sprites map' controls pure - e {state = (levelState e.state map') {GS.lives = maxLives}, entities = entities} + e {state = (GS.levelState e.state map') {GS.lives = GS.maxLives}, entities = entities} else do H.render renderer hud state title <- S.get sprites "game-over" diff --git a/src/Game/State.hs b/src/Game/State.hs index e05fb61..3773268 100644 --- a/src/Game/State.hs +++ b/src/Game/State.hs @@ -1,4 +1,10 @@ -module Game.State (State (..)) where +module Game.State (State (..), initialState, levelState, maxLives) where + +import Game.Entities.Const (hitDelay) +import qualified Game.Map as M + +maxLives :: Int +maxLives = 4 data State = State { batteries :: Int, @@ -12,3 +18,32 @@ data State = State levelCompleted :: Bool, currentLevel :: Int } + +initialState :: M.Map -> State +initialState m = + State + { batteries = 0, + totalBatteries = M.totalBatteries m, + lives = maxLives, + totalLives = maxLives, + hitDelay = hitDelay, + gameOverDelay = 0, + exit = False, + -- doesn't matter where + lastBattery = (0, 0), + levelCompleted = False, + currentLevel = 0 + } + +levelState :: State -> M.Map -> State +levelState s m = + s + { batteries = 0, + totalBatteries = M.totalBatteries m, + hitDelay = hitDelay, + gameOverDelay = 0, + exit = False, + -- doesn't matter where + lastBattery = (0, 0), + levelCompleted = False + } -- cgit v1.2.3