on master help sign in
Rogue: use a type alias where coordinates are used
commit30103d7c247e99c7b4ece44db2dfea7ea636ae55 authorJonathan Daugherty <cygnus@foobox.com> dateFri 1 Aug 2014, 15:47 (12 years ago) parentd4d4aa3
1 file changed+17−8
test/Rogue.hs+17−8
test/Rogue.hs+17−8
@@ -13,8 +13,7 @@ import Control.Monad.RWS
1313import System.Random
1414
1515data Player = Player
16− { playerX :: Int
17− , playerY :: Int
16+ { playerCoord :: Coord
1817 } deriving (Show,Eq)
1918
2019data World = World
@@ -24,8 +23,8 @@ data World = World
2423 deriving (Show,Eq)
2524
2625data Level = Level
27− { levelStart :: (Int, Int)
28− , levelEnd :: (Int, Int)
26+ { levelStart :: Coord
27+ , levelEnd :: Coord
2928 , levelGeo :: Geo
3029 -- building the geo image is expensive. Cache it. Though VTY should go
3130 -- through greater lengths to avoid the need to cache images.
@@ -39,13 +38,14 @@ data LevelPiece
3938 deriving (Show, Eq)
4039
4140type Game = RWST Vty () World IO
42−type Geo = Array (Int, Int) LevelPiece
41+type Geo = Array Coord LevelPiece
42+type Coord = (Int, Int)
4343
4444main :: IO ()
4545main = do
4646 vty <- mkVty def
4747 level0 <- mkLevel 1
48− let world0 = World (Player (fst $ levelStart level0) (snd $ levelStart level0)) level0
48+ let world0 = World (Player (levelStart level0)) level0
4949 (_finalWorld, ()) <- execRWST play vty world0
5050 shutdown vty
5151
@@ -104,13 +104,13 @@ processEvent = do
104104movePlayer :: Int -> Int -> Game ()
105105movePlayer dx dy = do
106106 world <- get
107− let Player x y = player world
107+ let Player (x, y) = player world
108108 let x' = x + dx
109109 y' = y + dy
110110 -- this is only valid because the level generation assures the border is
111111 -- always Rock
112112 case levelGeo (level world) ! (x',y') of
113− EmptySpace -> put $ world { player = Player x' y' }
113+ EmptySpace -> put $ world { player = Player (x',y') }
114114 _ -> return ()
115115
116116updateDisplay :: Game ()
@@ -155,3 +155,12 @@ buildGeoImage geo =
155155 , let i = imageForGeo (geo ! (x,y))
156156 ]
157157 ]
158+
159+--
160+-- Miscellaneous
161+--
162+playerX :: Player -> Int
163+playerX = fst . playerCoord
164+
165+playerY :: Player -> Int
166+playerY = snd . playerCoord