diff options
author | Juan J. Martinez <jjm@usebox.net> | 2023-04-14 08:37:14 +0100 |
---|---|---|
committer | Juan J. Martinez <jjm@usebox.net> | 2023-04-14 08:37:14 +0100 |
commit | 9f1811975e996a3bf70c3e834a872e59c8ade984 (patch) | |
tree | 22f5cd683e9c353fb73929eaab2b9d35cd8296b0 | |
parent | 35d0d3911f43aca4eb34f785cdf1ad63e60863e7 (diff) | |
download | space-plat-hs-9f1811975e996a3bf70c3e834a872e59c8ade984.tar.gz space-plat-hs-9f1811975e996a3bf70c3e834a872e59c8ade984.zip |
Load the map list from a JSON file
-rw-r--r-- | data/maps.json | 5 | ||||
-rw-r--r-- | src/Game.hs | 11 | ||||
-rw-r--r-- | src/Game/Map.hs | 9 |
3 files changed, 19 insertions, 6 deletions
diff --git a/data/maps.json b/data/maps.json new file mode 100644 index 0000000..2b56827 --- /dev/null +++ b/data/maps.json @@ -0,0 +1,5 @@ +[ + "data/map1.json", + "data/map2.json", + "data/map1.json" +] 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 |