From 71759c028c8e0e894939526ef87b7efd9cb644af Mon Sep 17 00:00:00 2001 From: Suguivy Date: Sun, 21 Feb 2021 16:59:17 +0100 Subject: [PATCH] Added more parameters in JSON (no functional yet) --- maps/test.json | 18 ++++++++++-------- src/Dungeon.hs | 22 +++++++++++++++------- 2 files changed, 25 insertions(+), 15 deletions(-) diff --git a/maps/test.json b/maps/test.json index 8e7df5b..baedb0b 100644 --- a/maps/test.json +++ b/maps/test.json @@ -1,6 +1,6 @@ { - "map" : [ - + "name" : "test", + "layout" : [ "#########################", "####......#####....######", "###.........###.....#####", @@ -26,12 +26,14 @@ "#..###................###", "#..................######", "#########################" - ], - "communications" : { - - "c1" : [[16,0], "c2"] - - } + "tunnels" : [ + { + "name" : "a", + "coord" : [16,0], + "map" : "test2", + "to_tunnel" : "b" + } + ] } diff --git a/src/Dungeon.hs b/src/Dungeon.hs index 149b501..92558d1 100644 --- a/src/Dungeon.hs +++ b/src/Dungeon.hs @@ -1,4 +1,4 @@ -{-# LANGUAGE OverloadedStrings #-} +{-# LANGUAGE OverloadedStrings, TemplateHaskell #-} module Dungeon where @@ -7,6 +7,8 @@ import Data.Matrix import Linear.V2 import Data.Tuple import Data.Maybe +import Lens.Micro.TH +import Lens.Micro data Cell = Solid | Empty @@ -21,18 +23,24 @@ cellMapping = , (Solid, '#') ] -newtype Dungeon = Dungeon (Matrix Cell) +data Dungeon = Dungeon + { _name :: String + , _layout :: Matrix Cell + } + +makeLenses ''Dungeon instance Show Dungeon where - show (Dungeon m) = unlines . map (concatMap show) $ toLists m + show dun = unlines . map (concatMap show) $ toLists $ dun ^. layout instance FromJSON Dungeon where parseJSON = withObject "Dungeon" $ \v -> do - stringMap <- v .: "map" + mapName <- v .: "name" + stringMap <- v .: "layout" let cellMappingR = map swap cellMapping charToCell c = fromMaybe (error "Invalid cell in the .map file") (c `lookup` cellMappingR) cellLists = map charToCell <$> stringMap - return . Dungeon . fromLists $ cellLists + return $ Dungeon mapName (fromLists cellLists) makeDungeonFromFile :: FilePath -> IO Dungeon @@ -43,7 +51,7 @@ makeDungeonFromFile f = do Right dun -> dun dungeonToLists :: Dungeon -> [[Cell]] -dungeonToLists (Dungeon m) = toLists m +dungeonToLists dun = toLists $ dun ^. layout getCell :: V2 Int -> Dungeon -> Cell -getCell (V2 x y) (Dungeon m) = fromMaybe Solid (safeGet (y+1) (x+1) m) +getCell (V2 x y) dun = fromMaybe Solid (safeGet (y+1) (x+1) $ dun ^. layout)