about summary refs log tree commit diff stats
path: root/Fallen/FunMap.hs
diff options
context:
space:
mode:
Diffstat (limited to 'Fallen/FunMap.hs')
-rw-r--r--Fallen/FunMap.hs56
1 files changed, 56 insertions, 0 deletions
diff --git a/Fallen/FunMap.hs b/Fallen/FunMap.hs new file mode 100644 index 0000000..809bd4c --- /dev/null +++ b/Fallen/FunMap.hs
@@ -0,0 +1,56 @@
1-- Alternate implementation of Map with functions
2
3module Fallen.FunMap
4( Map,
5 emptyMap,
6 dimension,
7 inBounds,
8 getTileAtPos,
9 findTileInMap,
10 updateMap,
11 legalMoves,
12 fillMapRect
13) where
14 import Fallen.Tiles
15 import Fallen.Point
16 import Data.List
17 import Fallen.Util
18 import Data.Maybe
19
20 data Map = Map {
21 dimension :: (Int, Int),
22 mapdata :: Point -> Tile,
23 background :: Tile
24 }
25
26 -- emptyMap :: Int -> Int -> Tile -> Map
27 emptyMap w h t = Map { dimension=(w,h), mapdata=(\p -> t), background=t }
28
29 -- inBounds :: Map -> Point -> Bool
30 inBounds m (x,y) = let (w,h) = dimension m in (x >= 0) && (x < w) && (y >= 0) && (y < h)
31
32 -- getTileAtPos :: Map -> Point -> Tile
33 getTileAtPos m p = if (inBounds m p)
34 then mapdata m p
35 else background m
36
37 -- findTileInMap :: Map -> Tile -> [Point]
38 -- REALLY inefficient
39 findTileInMap m t = filter (\p -> t == mapdata m p) rawPoints where
40 (w,h) = dimension m
41 rawPoints = [(x,y) | x <- [0..w-1], y <- [0..h-1]]
42
43 -- updateMap :: Point -> Tile -> Map -> Map
44 updateMap p t (Map d xs bg) = Map d (redirect p t xs) bg where
45 redirect p t xs = (\p2 -> if p == p2 then t else xs p2)
46
47 -- legalMoves :: Map -> Point -> [Tile] -> [Direction]
48 legalMoves m p ts = map fst $ filter legal $ map tileDir directions where
49 tileDir d = (d, getTileAtPos m $ stepInDirection p d)
50 legal (_,t) = t `elem` ts
51
52 -- fillMapRect :: Int -> Int -> Int -> Int -> Tile -> Map -> Map
53 fillMapRect x y w h t (Map d xs bg) = Map d redirectInBounds bg where
54 redirectInBounds = (\p -> if (inBounds p) then t else xs p)
55 inBounds (px,py) = (px >= x) && (px < (x+w)) && (py >= y) && (py < (y+h))
56 \ No newline at end of file