Now player uses V2 for position

This commit is contained in:
Ivy 2021-02-18 18:25:13 +01:00
parent e7cd7fb8a5
commit b46f642728
4 changed files with 18 additions and 14 deletions

View File

@ -29,4 +29,5 @@ executable roguelike
matrix, matrix,
unordered-containers, unordered-containers,
microlens-th, microlens-th,
microlens microlens,
linear

View File

@ -1,9 +1,10 @@
{-# LANGUAGE TemplateHaskell #-} {-# LANGUAGE TemplateHaskell, NegativeLiterals #-}
module Game where module Game where
import Lens.Micro.TH import Lens.Micro.TH
import Lens.Micro import Lens.Micro
import Linear.V2
import Dungeon import Dungeon
import Player import Player
@ -20,16 +21,16 @@ makeLenses ''Game
newGame :: IO Game newGame :: IO Game
newGame = do newGame = do
dun <- makeDungeonFromFile "maps/test.map" dun <- makeDungeonFromFile "maps/test.map"
return $ Game dun (Player (0,0)) return $ Game dun (Player $ V2 0 0)
runAction :: Action -> Game -> Maybe Game runAction :: Action -> Game -> Maybe Game
runAction (Walk N) game = Just $ game & player . pos . _2 -~ 1 runAction (Walk N) game = Just $ game & player . pos +~ V2 0 -1
runAction (Walk S) game = Just $ game & player . pos . _2 +~ 1 runAction (Walk S) game = Just $ game & player . pos +~ V2 0 1
runAction (Walk W) game = Just $ game & player . pos . _1 -~ 1 runAction (Walk W) game = Just $ game & player . pos +~ V2 -1 0
runAction (Walk E) game = Just $ game & player . pos . _1 +~ 1 runAction (Walk E) game = Just $ game & player . pos +~ V2 1 0
runAction (Walk NW) game = Just $ game & player . pos . both -~ 1 runAction (Walk NW) game = Just $ game & player . pos +~ V2 -1 -1
runAction (Walk NE) game = Just $ game & player . pos %~ (\(x,y) -> (x+1, y-1)) runAction (Walk NE) game = Just $ game & player . pos +~ V2 1 -1
runAction (Walk SW) game = Just $ game & player . pos %~ (\(x,y) -> (x-1, y+1)) runAction (Walk SW) game = Just $ game & player . pos +~ V2 -1 1
runAction (Walk SE) game = Just $ game & player . pos . both +~ 1 runAction (Walk SE) game = Just $ game & player . pos +~ V2 1 1
runAction None g = Just g runAction None g = Just g
runAction ExitGame _ = Nothing runAction ExitGame _ = Nothing

View File

@ -3,9 +3,10 @@
module Player where module Player where
import Lens.Micro.TH (makeLenses) import Lens.Micro.TH (makeLenses)
import Linear.V2
data Player = Player data Player = Player
{ _pos :: (Int, Int) { _pos :: V2 Int
} }
makeLenses ''Player makeLenses ''Player

View File

@ -2,6 +2,7 @@ module Rendering where
import Lens.Micro import Lens.Micro
import Graphics.Vty import Graphics.Vty
import Linear.V2
import Dungeon import Dungeon
import Game import Game
@ -18,5 +19,5 @@ dungeonToImg = vertCat . map (string defAttr . concatMap show) . dungeonToLists
playerToImg :: Player -> Image playerToImg :: Player -> Image
playerToImg p = translateX px . translateY py $ char defAttr '@' playerToImg p = translateX px . translateY py $ char defAttr '@'
where px = p ^. pos . _1 where px = p ^. pos . _x
py = p ^. pos . _2 py = p ^. pos . _y