aboutsummaryrefslogtreecommitdiff
path: root/src/Game.hs
diff options
context:
space:
mode:
authorJuan J. Martinez <jjm@usebox.net>2023-03-10 23:53:40 +0000
committerJuan J. Martinez <jjm@usebox.net>2023-03-10 23:53:40 +0000
commit45a103da2133b645fac9617cc97c11901d3e258f (patch)
tree0cad00427725ecd1067f64a99a25e78d39e72409 /src/Game.hs
parent7a714cc457323bff6b4a73ec9e7b58c7a63381ec (diff)
downloadspace-plat-hs-45a103da2133b645fac9617cc97c11901d3e258f.tar.gz
space-plat-hs-45a103da2133b645fac9617cc97c11901d3e258f.zip
Change stage
Diffstat (limited to 'src/Game.hs')
-rw-r--r--src/Game.hs39
1 files changed, 35 insertions, 4 deletions
diff --git a/src/Game.hs b/src/Game.hs
index 31aa40b..7951d09 100644
--- a/src/Game.hs
+++ b/src/Game.hs
@@ -36,12 +36,17 @@ windowWidth, windowHeight :: CInt
version :: String
version = "0.1.0"
+-- XXX: placeholder for the map list, perhaps should be loaded from a JSON
+maps :: [String]
+maps = ["data/map1.json", "data/map2.json"]
+
data Env = Env
{ window :: SDL.Window,
renderer :: SDL.Renderer,
canvas :: SDL.Texture,
fullscreen :: Bool,
renderRect :: SDL.Rectangle CInt,
+ tsTexture :: SDL.Texture,
map :: M.Map,
sprites :: S.SpriteSheet,
font :: BF.BitmapFont,
@@ -65,7 +70,22 @@ initialState m =
gameOverDelay = 0,
exit = False,
-- doesn't matter where
- lastBattery = (0, 0)
+ 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 ()
@@ -88,12 +108,12 @@ main = do
tsTexture <- SDL.Image.loadTexture renderer "data/tiles.png"
ssTexture <- SDL.Image.loadTexture renderer "data/sprites.png"
bfTexture <- SDL.Image.loadTexture renderer "data/font.png"
- map' <- M.load "data/map.json" tsTexture
+ map' <- M.load (head maps) 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
- hud <- H.mkHud sprites
+ hud <- H.mkHud sprites font
gameLoop
Env
{ window = window,
@@ -101,6 +121,7 @@ main = do
canvas = canvas,
fullscreen = False,
renderRect = defaultRenderRect,
+ tsTexture = tsTexture,
map = map',
sprites = sprites,
font = font,
@@ -178,6 +199,15 @@ gameLoop e = do
| state.batteries == state.totalBatteries && not state.exit = do
es <- E.addExit env.entities x (y - 8) -- adjusted to player's height
pure env {entities = es, state = state {GS.exit = True}}
+ | state.levelCompleted = do
+ map' <- M.load (maps !! (env.state.currentLevel + 1)) env.tsTexture
+ entities <- E.mkEntities env.sprites map' env.controls
+ pure $
+ env
+ { map = map',
+ state = (levelState env.state map') {GS.currentLevel = env.state.currentLevel + 1},
+ entities = entities
+ }
| otherwise = pure env
where
state = env.state
@@ -223,7 +253,8 @@ gameOverLoop e = do
then do
-- retry last level
entities <- E.mkEntities sprites map' controls
- pure e {state = initialState map', entities = entities}
+ pure
+ e {state = (levelState e.state map') {GS.lives = maxLives}, entities = entities}
else do
H.render renderer hud state
title <- S.get sprites "game-over"