Now the player can't walk on solid cells

This commit is contained in:
Ivy 2021-02-18 20:00:36 +01:00
parent 7e09931173
commit 5254c8429e
2 changed files with 12 additions and 4 deletions

View File

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

View File

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