From 9f1811975e996a3bf70c3e834a872e59c8ade984 Mon Sep 17 00:00:00 2001 From: "Juan J. Martinez" Date: Fri, 14 Apr 2023 08:37:14 +0100 Subject: Load the map list from a JSON file --- src/Game.hs | 11 +++++------ src/Game/Map.hs | 9 +++++++++ 2 files changed, 14 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/Game.hs b/src/Game.hs index be34a08..c2f6083 100644 --- a/src/Game.hs +++ b/src/Game.hs @@ -32,10 +32,6 @@ 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, @@ -43,6 +39,7 @@ data Env = Env fullscreen :: Bool, renderRect :: SDL.Rectangle CInt, tsTexture :: SDL.Texture, + mapList :: [String], map :: M.Map, sprites :: S.SpriteSheet, font :: BF.BitmapFont, @@ -75,7 +72,8 @@ 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 (head maps) tsTexture + mapList <- M.loadMapList "data/maps.json" + map' <- M.load (head mapList) tsTexture sprites <- S.load "data/sprites.json" ssTexture font <- BF.load "data/font.json" bfTexture controls <- newIORef C.init @@ -89,6 +87,7 @@ main = do fullscreen = False, renderRect = defaultRenderRect, tsTexture = tsTexture, + mapList = mapList, map = map', sprites = sprites, font = font, @@ -167,7 +166,7 @@ gameLoop e = 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 == GS.ExitDone = do - map' <- M.load (maps !! (env.state.currentLevel + 1)) env.tsTexture + map' <- M.load (env.mapList !! (env.state.currentLevel + 1)) env.tsTexture entities <- E.mkEntities env.sprites map' env.controls pure $ env diff --git a/src/Game/Map.hs b/src/Game/Map.hs index ea20b17..9616971 100644 --- a/src/Game/Map.hs +++ b/src/Game/Map.hs @@ -3,6 +3,7 @@ module Game.Map Object (..), objects, totalBatteries, + loadMapList, load, render, isBlocked, @@ -117,6 +118,14 @@ data Viewport = Viewport Int Int Int Int data Map = Map MapData SDL.Texture +-- | Loads a list of maps from JSON file. +loadMapList :: String -> IO [String] +loadMapList filename = do + d <- readFile filename + case decode d :: Result [String] of + Ok s -> pure s + Error e -> error e + -- | Loads a map from a JSON file. load :: String -> SDL.Texture -> IO Map load filename tex = do -- cgit v1.2.3