Now maps are in JSON format

This commit is contained in:
Ivy 2021-02-19 19:52:43 +01:00
parent 88e2522ab9
commit 3f88d88ad7
6 changed files with 54 additions and 29 deletions

31
maps/test.json Normal file
View File

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

View File

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

View File

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

View File

@ -1,5 +1,8 @@
{-# LANGUAGE OverloadedStrings #-}
module Dungeon where module Dungeon where
import Data.Aeson
import Data.Aeson.Types
import Data.Matrix import Data.Matrix
import Linear.V2 import Linear.V2
import Data.Tuple import Data.Tuple
@ -25,10 +28,16 @@ instance Show Dungeon where
makeDungeonFromFile :: String -> IO Dungeon makeDungeonFromFile :: String -> IO Dungeon
makeDungeonFromFile f = do 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 let cellMappingR = map swap cellMapping
charToCell c = fromMaybe (error "Invalid cell in the .map file") (c `lookup` cellMappingR) 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 return . Dungeon . fromLists $ cellLists
dungeonToLists :: Dungeon -> [[Cell]] dungeonToLists :: Dungeon -> [[Cell]]

7
src/Dungeon/Loader.hs Normal file
View File

@ -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

View File

@ -19,7 +19,7 @@ makeLenses ''Game
newGame :: IO Game newGame :: IO Game
newGame = do newGame = do
dun <- makeDungeonFromFile "maps/test.map" dun <- makeDungeonFromFile "maps/test.json"
return $ Game dun (Player $ V2 1 24) return $ Game dun (Player $ V2 1 24)
runAction :: Action -> Game -> Maybe Game runAction :: Action -> Game -> Maybe Game