diff options
-rw-r--r-- | Fallen/FunMap.hs | 56 | ||||
-rw-r--r-- | Fallen/Map.hs | 12 |
2 files changed, 56 insertions, 12 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 | |||
3 | module 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 | ||
diff --git a/Fallen/Map.hs b/Fallen/Map.hs index 96c6467..a33b696 100644 --- a/Fallen/Map.hs +++ b/Fallen/Map.hs | |||
@@ -28,15 +28,9 @@ module Fallen.Map | |||
28 | inBounds m (x,y) = let (w,h) = dimension m in (x >= 0) && (x < w) && (y >= 0) && (y < h) | 28 | inBounds m (x,y) = let (w,h) = dimension m in (x >= 0) && (x < w) && (y >= 0) && (y < h) |
29 | 29 | ||
30 | -- getTileAtPos :: Map -> Point -> Tile | 30 | -- getTileAtPos :: Map -> Point -> Tile |
31 | --getTileAtPos m p = if (inBounds m p) | ||
32 | -- then mapdata m p | ||
33 | -- else background m | ||
34 | getTileAtPos m p = fromMaybe (background m) $ lookup p $ mapdata m | 31 | getTileAtPos m p = fromMaybe (background m) $ lookup p $ mapdata m |
35 | 32 | ||
36 | -- findTileInMap :: Map -> Tile -> [Point] | 33 | -- findTileInMap :: Map -> Tile -> [Point] |
37 | -- findTileInMap m t = filter (\p -> t == mapdata m p) rawPoints where | ||
38 | -- (w,h) = dimension m | ||
39 | -- rawPoints = [(x,y) | x <- [0..w-1], y <- [0..h-1]] | ||
40 | findTileInMap m t = map fst $ filter ((== t) . snd) $ mapdata m | 34 | findTileInMap m t = map fst $ filter ((== t) . snd) $ mapdata m |
41 | 35 | ||
42 | -- updateMap updates the tiles at the given position in the map | 36 | -- updateMap updates the tiles at the given position in the map |
@@ -48,8 +42,6 @@ module Fallen.Map | |||
48 | else case lookup p xs of | 42 | else case lookup p xs of |
49 | Just _ -> Map d (map (\(p1,t1) -> if p1 == p then (p,t) else (p1,t1)) xs) bg | 43 | Just _ -> Map d (map (\(p1,t1) -> if p1 == p then (p,t) else (p1,t1)) xs) bg |
50 | Nothing -> Map d ((p,t):xs) bg | 44 | Nothing -> Map d ((p,t):xs) bg |
51 | -- updateMap p t (Map d xs bg) = Map d (redirect p t xs) bg where | ||
52 | -- redirect p t xs = (\p2 -> if p == p2 then t else xs p2) | ||
53 | 45 | ||
54 | -- legalMoves :: Map -> Point -> [Tile] -> [Direction] | 46 | -- legalMoves :: Map -> Point -> [Tile] -> [Direction] |
55 | legalMoves m p ts = map fst $ filter legal $ map tileDir directions where | 47 | legalMoves m p ts = map fst $ filter legal $ map tileDir directions where |
@@ -61,7 +53,3 @@ module Fallen.Map | |||
61 | then Map d (filter outBounds xs) bg | 53 | then Map d (filter outBounds xs) bg |
62 | else Map d (filter outBounds xs ++ [((px,py), t) | px <- [x..x+w-1], py <- [y..y+h-1]]) bg where | 54 | else Map d (filter outBounds xs ++ [((px,py), t) | px <- [x..x+w-1], py <- [y..y+h-1]]) bg where |
63 | outBounds ((px,py),_) = (px < x) || (px >= (x+w)) || (py < y) || (py >= (y+h)) | 55 | outBounds ((px,py),_) = (px < x) || (px >= (x+w)) || (py < y) || (py >= (y+h)) |
64 | -- fillMapRect x y w h t (Map d xs bg) = Map d redirectInBounds bg where | ||
65 | -- redirectInBounds = (\p -> if (inBounds p) then t else xs p) | ||
66 | -- inBounds (px,py) = (px >= x) && (px < (x+w)) && (py >= y) && (py < (y+h)) | ||
67 | \ No newline at end of file | ||