diff --git a/maps/test.json b/maps/test.json new file mode 100644 index 0000000..1aa74dd --- /dev/null +++ b/maps/test.json @@ -0,0 +1,31 @@ +{ + "map" : [ + + "#########################", + "####......#####....######", + "###.........###.....#####", + "##....#......###....#####", + "##...###......##.....####", + "##..######....##......###", + "##..######....###.....###", + "#...########..#####....##", + "#....#######..######...##", + "#....#####.......###....#", + "##...####...#.....##....#", + "###........######.###...#", + "###.......##......###...#", + "###...#..###.########...#", + "##...#######.......###..#", + "#....#############.#....#", + "#.....####.###.....##..##", + "#.........########.....##", + "##...##############....##", + "###...###..###.######..##", + "###.......##....####...##", + "####.#####......###...###", + "#..###................###", + "#..................######", + "#########################" + + ] +} diff --git a/maps/test.map b/maps/test.map deleted file mode 100644 index 2f0b033..0000000 --- a/maps/test.map +++ /dev/null @@ -1,25 +0,0 @@ -######################### -####......#####....###### -###.........###.....##### -##....#......###....##### -##...###......##.....#### -##..######....##......### -##..######....###.....### -#...########..#####....## -#....#######..######...## -#....#####.......###....# -##...####...#.....##....# -###........######.###...# -###.......##......###...# -###...#..###.########...# -##...#######.......###..# -#....#############.#....# -#.....####.###.....##..## -#.........########.....## -##...##############....## -###...###..###.######..## -###.......##....####...## -####.#####......###...### -#..###................### -#..................###### -######################### diff --git a/roguelike.cabal b/roguelike.cabal index fa81a73..a978aaf 100644 --- a/roguelike.cabal +++ b/roguelike.cabal @@ -29,4 +29,7 @@ executable roguelike unordered-containers, microlens-th, microlens, - linear + linear, + aeson, + bytestring, + utf8-string diff --git a/src/Dungeon.hs b/src/Dungeon.hs index a362043..3cccd01 100644 --- a/src/Dungeon.hs +++ b/src/Dungeon.hs @@ -1,5 +1,8 @@ +{-# LANGUAGE OverloadedStrings #-} module Dungeon where +import Data.Aeson +import Data.Aeson.Types import Data.Matrix import Linear.V2 import Data.Tuple @@ -25,10 +28,16 @@ instance Show Dungeon where makeDungeonFromFile :: String -> IO Dungeon makeDungeonFromFile f = do - contents <- readFile f + --contents <- readFile f + ef <- eitherDecodeFileStrict f :: IO (Either String Object) + let obj = case ef of (Right o) -> o + (Left l) -> error l + let stringMap = case parse (.: "map") obj :: Result [String] of + (Success m) -> m + (Error s) -> error s let cellMappingR = map swap cellMapping charToCell c = fromMaybe (error "Invalid cell in the .map file") (c `lookup` cellMappingR) - cellLists = map charToCell <$> lines contents + cellLists = map charToCell <$> stringMap return . Dungeon . fromLists $ cellLists dungeonToLists :: Dungeon -> [[Cell]] diff --git a/src/Dungeon/Loader.hs b/src/Dungeon/Loader.hs new file mode 100644 index 0000000..05b1b2e --- /dev/null +++ b/src/Dungeon/Loader.hs @@ -0,0 +1,7 @@ +module Dungeon.Loader where + +import Data.Aeson +import Data.ByteString.Lazy.UTF8 (fromString) + +loadDungeon :: String -> IO (Either String Object) +loadDungeon = eitherDecodeFileStrict diff --git a/src/Game.hs b/src/Game.hs index 55cd107..7a1610d 100644 --- a/src/Game.hs +++ b/src/Game.hs @@ -19,7 +19,7 @@ makeLenses ''Game newGame :: IO Game newGame = do - dun <- makeDungeonFromFile "maps/test.map" + dun <- makeDungeonFromFile "maps/test.json" return $ Game dun (Player $ V2 1 24) runAction :: Action -> Game -> Maybe Game