diff --git a/src/Dungeon.hs b/src/Dungeon.hs index 3cccd01..7bf232b 100644 --- a/src/Dungeon.hs +++ b/src/Dungeon.hs @@ -2,11 +2,11 @@ module Dungeon where import Data.Aeson -import Data.Aeson.Types import Data.Matrix import Linear.V2 import Data.Tuple import Data.Maybe +import Data.Either data Cell = Solid | Empty @@ -26,19 +26,19 @@ newtype Dungeon = Dungeon (Matrix Cell) instance Show Dungeon where show (Dungeon m) = unlines . map (concatMap show) $ toLists m -makeDungeonFromFile :: String -> IO Dungeon +instance FromJSON Dungeon where + parseJSON = withObject "Dungeon" $ \v -> do + stringMap <- v .: "map" + 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 + + +makeDungeonFromFile :: FilePath -> IO Dungeon makeDungeonFromFile f = do - --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 <$> stringMap - return . Dungeon . fromLists $ cellLists + eithDun <- eitherDecodeFileStrict f + return $ fromRight (error "") eithDun dungeonToLists :: Dungeon -> [[Cell]] dungeonToLists (Dungeon m) = toLists m diff --git a/src/Dungeon/Loader.hs b/src/Dungeon/Loader.hs deleted file mode 100644 index 05b1b2e..0000000 --- a/src/Dungeon/Loader.hs +++ /dev/null @@ -1,7 +0,0 @@ -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 7a1610d..b892140 100644 --- a/src/Game.hs +++ b/src/Game.hs @@ -20,7 +20,7 @@ makeLenses ''Game newGame :: IO Game newGame = do dun <- makeDungeonFromFile "maps/test.json" - return $ Game dun (Player $ V2 1 24) + return $ Game dun (Player $ V2 1 23) runAction :: Action -> Game -> Maybe Game runAction (Move vec) game = Just $ if ableToMove