From e7cd7fb8a5e4285b119bbe5001a18b781f72f34b Mon Sep 17 00:00:00 2001 From: Ivy Date: Mon, 15 Feb 2021 20:57:45 +0100 Subject: [PATCH] Added static maps --- maps/test.map | 7 +++++++ src/Dungeon.hs | 24 ++++++++++++++++++------ src/Game.hs | 6 ++++-- src/Main.hs | 3 ++- 4 files changed, 31 insertions(+), 9 deletions(-) create mode 100644 maps/test.map diff --git a/maps/test.map b/maps/test.map new file mode 100644 index 0000000..4150943 --- /dev/null +++ b/maps/test.map @@ -0,0 +1,7 @@ +######################## +#......................# +#......................# +#......................# +#......................# +#......................# +######################## diff --git a/src/Dungeon.hs b/src/Dungeon.hs index 64e7a8a..2e650ea 100644 --- a/src/Dungeon.hs +++ b/src/Dungeon.hs @@ -1,20 +1,32 @@ module Dungeon where -import Data.Matrix +import Data.Matrix hiding ((<|>)) +import Data.Tuple +import Data.Maybe +import Control.Applicative ((<|>)) -data Cell = Solid | Empty +data Cell = Solid + | Empty + deriving (Eq) instance Show Cell where - show Solid = "#" - show Empty = "." + show cell = [fromJust (lookup cell cellChars <|> Just '?')] + +cellChars :: [(Cell, Char)] +cellChars = + [ (Empty, '.') + , (Solid, '#') + ] newtype Dungeon = Dungeon (Matrix Cell) instance Show Dungeon where show (Dungeon m) = unlines . map (concatMap show) $ toLists m -makeDungeon :: Int -> Int -> Dungeon -makeDungeon w h = Dungeon $ matrix h w $ const Empty +makeDungeonFromFile :: String -> IO Dungeon +makeDungeonFromFile f = do + contents <- readFile f + return $ Dungeon $ fromLists $ map (fromJust . (`lookup` map swap cellChars)) <$> lines contents dungeonToLists :: Dungeon -> [[Cell]] dungeonToLists (Dungeon m) = toLists m diff --git a/src/Game.hs b/src/Game.hs index e54dac4..cc0529d 100644 --- a/src/Game.hs +++ b/src/Game.hs @@ -17,8 +17,10 @@ data Game = Game makeLenses ''Game -newGame :: Game -newGame = Game (makeDungeon 30 10) (Player (0,0)) +newGame :: IO Game +newGame = do + dun <- makeDungeonFromFile "maps/test.map" + return $ Game dun (Player (0,0)) runAction :: Action -> Game -> Maybe Game runAction (Walk N) game = Just $ game & player . pos . _2 -~ 1 diff --git a/src/Main.hs b/src/Main.hs index f313fa8..2c827fe 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -10,7 +10,8 @@ main :: IO () main = do cfg <- standardIOConfig vty <- mkVty cfg - loop vty newGame + game <- newGame + loop vty game shutdown vty where loop vty game = do update vty $ renderGame game