on master help sign in
Rogue: use a type alias where coordinates are used
30103d7Jonathan Daugherty
test/Rogue.hs+17−8
@@ -13,8 +13,7 @@ import Control.Monad.RWS
13import System.Random
13import System.Random
14
14
15data Player = Player
15data Player = Player
16− { playerX :: Int
16+ { playerCoord :: Coord
17− , playerY :: Int
18 } deriving (Show,Eq)
17 } deriving (Show,Eq)
19
18
20data World = World
19data World = World
@@ -24,8 +23,8 @@ data World = World
24 deriving (Show,Eq)
23 deriving (Show,Eq)
25
24
26data Level = Level
25data Level = Level
27− { levelStart :: (Int, Int)
26+ { levelStart :: Coord
28− , levelEnd :: (Int, Int)
27+ , levelEnd :: Coord
29 , levelGeo :: Geo
28 , levelGeo :: Geo
30 -- building the geo image is expensive. Cache it. Though VTY should go
29 -- building the geo image is expensive. Cache it. Though VTY should go
31 -- through greater lengths to avoid the need to cache images.
30 -- through greater lengths to avoid the need to cache images.
@@ -39,13 +38,14 @@ data LevelPiece
39 deriving (Show, Eq)
38 deriving (Show, Eq)
40
39
41type Game = RWST Vty () World IO
40type Game = RWST Vty () World IO
42−type Geo = Array (Int, Int) LevelPiece
41+type Geo = Array Coord LevelPiece
42+type Coord = (Int, Int)
43
43
44main :: IO ()
44main :: IO ()
45main = do
45main = do
46 vty <- mkVty def
46 vty <- mkVty def
47 level0 <- mkLevel 1
47 level0 <- mkLevel 1
48− let world0 = World (Player (fst $ levelStart level0) (snd $ levelStart level0)) level0
48+ let world0 = World (Player (levelStart level0)) level0
49 (_finalWorld, ()) <- execRWST play vty world0
49 (_finalWorld, ()) <- execRWST play vty world0
50 shutdown vty
50 shutdown vty
51
51
@@ -104,13 +104,13 @@ processEvent = do
104movePlayer :: Int -> Int -> Game ()
104movePlayer :: Int -> Int -> Game ()
105movePlayer dx dy = do
105movePlayer dx dy = do
106 world <- get
106 world <- get
107− let Player x y = player world
107+ let Player (x, y) = player world
108 let x' = x + dx
108 let x' = x + dx
109 y' = y + dy
109 y' = y + dy
110 -- this is only valid because the level generation assures the border is
110 -- this is only valid because the level generation assures the border is
111 -- always Rock
111 -- always Rock
112 case levelGeo (level world) ! (x',y') of
112 case levelGeo (level world) ! (x',y') of
113− EmptySpace -> put $ world { player = Player x' y' }
113+ EmptySpace -> put $ world { player = Player (x',y') }
114 _ -> return ()
114 _ -> return ()
115
115
116updateDisplay :: Game ()
116updateDisplay :: Game ()
@@ -155,3 +155,12 @@ buildGeoImage geo =
155 , let i = imageForGeo (geo ! (x,y))
155 , let i = imageForGeo (geo ! (x,y))
156 ]
156 ]
157 ]
157 ]
158+
159+--
160+-- Miscellaneous
161+--
162+playerX :: Player -> Int
163+playerX = fst . playerCoord
164+
165+playerY :: Player -> Int
166+playerY = snd . playerCoord