Browse Source

Now maps are in JSON format

main
Ivy 2 years ago
parent
commit
3f88d88ad7
  1. 31
      maps/test.json
  2. 25
      maps/test.map
  3. 5
      roguelike.cabal
  4. 13
      src/Dungeon.hs
  5. 7
      src/Dungeon/Loader.hs
  6. 2
      src/Game.hs

31
maps/test.json

@ -0,0 +1,31 @@
{
"map" : [
"#########################",
"####......#####....######",
"###.........###.....#####",
"##....#......###....#####",
"##...###......##.....####",
"##..######....##......###",
"##..######....###.....###",
"#...########..#####....##",
"#....#######..######...##",
"#....#####.......###....#",
"##...####...#.....##....#",
"###........######.###...#",
"###.......##......###...#",
"###...#..###.########...#",
"##...#######.......###..#",
"#....#############.#....#",
"#.....####.###.....##..##",
"#.........########.....##",
"##...##############....##",
"###...###..###.######..##",
"###.......##....####...##",
"####.#####......###...###",
"#..###................###",
"#..................######",
"#########################"
]
}

25
maps/test.map

@ -1,25 +0,0 @@
#########################
####......#####....######
###.........###.....#####
##....#......###....#####
##...###......##.....####
##..######....##......###
##..######....###.....###
#...########..#####....##
#....#######..######...##
#....#####.......###....#
##...####...#.....##....#
###........######.###...#
###.......##......###...#
###...#..###.########...#
##...#######.......###..#
#....#############.#....#
#.....####.###.....##..##
#.........########.....##
##...##############....##
###...###..###.######..##
###.......##....####...##
####.#####......###...###
#..###................###
#..................######
#########################

5
roguelike.cabal

@ -29,4 +29,7 @@ executable roguelike
unordered-containers,
microlens-th,
microlens,
linear
linear,
aeson,
bytestring,
utf8-string

13
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]]

7
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

2
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

Loading…
Cancel
Save