diff options
author | Juan J. Martinez <jjm@usebox.net> | 2023-03-05 11:42:56 +0000 |
---|---|---|
committer | Juan J. Martinez <jjm@usebox.net> | 2023-03-09 12:21:05 +0000 |
commit | beecc1e2af08f81f5cb5d2ebb93aaef59b20fcd5 (patch) | |
tree | f0d1947e0369fdf59a1f626f953c778884873cc0 /src | |
parent | ee50719de81145b5859d957da722cf183e0c8c60 (diff) | |
download | space-plat-hs-beecc1e2af08f81f5cb5d2ebb93aaef59b20fcd5.tar.gz space-plat-hs-beecc1e2af08f81f5cb5d2ebb93aaef59b20fcd5.zip |
Exit stage WIP
Diffstat (limited to 'src')
-rw-r--r-- | src/Game.hs | 17 | ||||
-rw-r--r-- | src/Game/Entities.hs | 20 | ||||
-rw-r--r-- | src/Game/Entities/Exit.hs | 31 | ||||
-rw-r--r-- | src/Game/State.hs | 3 |
4 files changed, 64 insertions, 7 deletions
diff --git a/src/Game.hs b/src/Game.hs index 52ba6a7..850d6ed 100644 --- a/src/Game.hs +++ b/src/Game.hs @@ -62,7 +62,8 @@ initialState m = lives = maxLives, totalLives = maxLives, hitDelay = hitDelay, - gameOverDelay = 0 + gameOverDelay = 0, + exit = False } main :: IO () @@ -155,7 +156,10 @@ gameLoop e = do SDL.rendererRenderTarget renderer $= Just canvas SDL.clear renderer - updatedEnv <- if state.gameOverDelay /= 1 then playLoop (updateState env) else gameOverLoop env + updatedEnv <- + if state.gameOverDelay /= 1 + then playLoop =<< updateState env + else gameOverLoop env SDL.rendererRenderTarget renderer $= Nothing SDL.clear renderer @@ -166,10 +170,13 @@ gameLoop e = do gameLoop updatedEnv where -- update state counters - updateState :: Env -> Env + updateState :: Env -> IO Env updateState env - | state.gameOverDelay > 1 = env {state = state {GS.gameOverDelay = state.gameOverDelay - 1}} - | otherwise = env + | state.gameOverDelay > 1 = pure env {state = state {GS.gameOverDelay = state.gameOverDelay - 1}} + | state.batteries == state.totalBatteries && not state.exit = do + es <- E.addExit env.entities + pure env {entities = es, state = state {GS.exit = True}} + | otherwise = pure env where state = env.state diff --git a/src/Game/Entities.hs b/src/Game/Entities.hs index 2810fcf..2cfdd76 100644 --- a/src/Game/Entities.hs +++ b/src/Game/Entities.hs @@ -1,4 +1,14 @@ -module Game.Entities (Entities, Entity, mkEntities, updateAll, render, renderVisible, playerPosition) where +module Game.Entities + ( Entities, + Entity, + mkEntities, + addExit, + updateAll, + render, + renderVisible, + playerPosition, + ) +where import Control.Monad import Data.Bits (Bits (..)) @@ -9,6 +19,7 @@ import qualified Game.Controller as C import Game.Entities.Common import Game.Entities.Const import Game.Entities.Effect +import Game.Entities.Exit import Game.Entities.Pickup import Game.Entities.Player import Game.Entities.Robot @@ -42,6 +53,13 @@ playerPosition (Entities _ _ entities) = where player = head entities +addExit :: Entities -> IO Entities +addExit es = do + exit <- mkExit es.sprites x y (collision es.player 24) + pure es {entities = es.entities ++ [exit]} + where + (x, y) = playerPosition es + updateAll :: Entities -> GS.State -> IO (Entities, GS.State) updateAll es state = do -- update the player first (including the reference) diff --git a/src/Game/Entities/Exit.hs b/src/Game/Entities/Exit.hs new file mode 100644 index 0000000..075e1f4 --- /dev/null +++ b/src/Game/Entities/Exit.hs @@ -0,0 +1,31 @@ +module Game.Entities.Exit (mkExit) where + +import Game.Entities.Common +import Game.Entities.Const +import Game.Entities.Types +import qualified Game.Sprites as S + +mkExit :: S.SpriteSheet -> Int -> Int -> Collision -> IO Entity +mkExit sprites x y playerCollision = do + s <- S.get sprites "exit" + pure + Entity + { typ = TypePickup, + x = x, + y = y, + delay = frameDelay, + frame = 0, + jumping = False, + gravity = gravityOff, + dir = DirRight, + sprite = s, + update = pure . updateExit playerCollision, + destroy = False, + actions = [] + } + +updateExit :: Collision -> Entity -> Entity +updateExit _ e + | e.delay > 0 = e {delay = e.delay - 1} + | e.frame + 1 < frameLimit e = e {delay = frameDelay, frame = e.frame + 1} + | otherwise = e {delay = frameDelay, frame = 7} diff --git a/src/Game/State.hs b/src/Game/State.hs index f8b5721..8987a2e 100644 --- a/src/Game/State.hs +++ b/src/Game/State.hs @@ -6,5 +6,6 @@ data State = State lives :: Int, totalLives :: Int, hitDelay :: Int, - gameOverDelay :: Int + gameOverDelay :: Int, + exit :: Bool } |