From 5254c8429ee1a7730e2f4edc2a2d947f885c9a43 Mon Sep 17 00:00:00 2001 From: Ivy Date: Thu, 18 Feb 2021 20:00:36 +0100 Subject: [PATCH] Now the player can't walk on solid cells --- src/Dungeon.hs | 8 ++++++-- src/Game.hs | 8 ++++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/Dungeon.hs b/src/Dungeon.hs index 2e650ea..6a4d21a 100644 --- a/src/Dungeon.hs +++ b/src/Dungeon.hs @@ -1,13 +1,14 @@ module Dungeon where import Data.Matrix hiding ((<|>)) +import Linear.V2 import Data.Tuple import Data.Maybe import Control.Applicative ((<|>)) data Cell = Solid - | Empty - deriving (Eq) + | Empty + deriving (Eq) instance Show Cell where show cell = [fromJust (lookup cell cellChars <|> Just '?')] @@ -30,3 +31,6 @@ makeDungeonFromFile f = do dungeonToLists :: Dungeon -> [[Cell]] dungeonToLists (Dungeon m) = toLists m + +getCell :: V2 Int -> Dungeon -> Cell +getCell (V2 x y) (Dungeon m) = fromMaybe Solid (safeGet (y+1) (x+1) m) diff --git a/src/Game.hs b/src/Game.hs index bff50ca..ba49b4a 100644 --- a/src/Game.hs +++ b/src/Game.hs @@ -20,9 +20,13 @@ makeLenses ''Game newGame :: IO Game newGame = do dun <- makeDungeonFromFile "maps/test.map" - return $ Game dun (Player $ V2 0 0) + return $ Game dun (Player $ V2 1 1) runAction :: Action -> Game -> Maybe Game -runAction (Move vec) game = Just $ game & player . pos +~ vec +runAction (Move vec) game = + if ableToMove then Just $ game & player . pos .~ newPos + else Just game + where ableToMove = getCell newPos (game ^. dungeon) == Empty + newPos = (game ^. player . pos) + vec runAction None g = Just g runAction ExitGame _ = Nothing