diff options
Diffstat (limited to 'Fallen/Map.hs')
-rw-r--r-- | Fallen/Map.hs | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/Fallen/Map.hs b/Fallen/Map.hs new file mode 100644 index 0000000..96c6467 --- /dev/null +++ b/Fallen/Map.hs | |||
@@ -0,0 +1,67 @@ | |||
1 | module Fallen.Map | ||
2 | ( Map, | ||
3 | emptyMap, | ||
4 | dimension, | ||
5 | inBounds, | ||
6 | getTileAtPos, | ||
7 | findTileInMap, | ||
8 | updateMap, | ||
9 | legalMoves, | ||
10 | fillMapRect | ||
11 | ) where | ||
12 | import Fallen.Tiles | ||
13 | import Fallen.Point | ||
14 | import Data.List | ||
15 | import Fallen.Util | ||
16 | import Data.Maybe | ||
17 | |||
18 | data Map = Map { | ||
19 | dimension :: (Int, Int), | ||
20 | mapdata :: [(Point, Tile)], | ||
21 | background :: Tile | ||
22 | } | ||
23 | |||
24 | -- emptyMap :: Int -> Int -> Tile -> Map | ||
25 | emptyMap w h t = Map { dimension=(w,h), mapdata=[], background=t } | ||
26 | |||
27 | -- inBounds :: Map -> Point -> Bool | ||
28 | inBounds m (x,y) = let (w,h) = dimension m in (x >= 0) && (x < w) && (y >= 0) && (y < h) | ||
29 | |||
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 | ||
35 | |||
36 | -- 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 | ||
41 | |||
42 | -- updateMap updates the tiles at the given position in the map | ||
43 | -- if the passed point already exists in the internal array, it is overwritten with the new point | ||
44 | -- if the passed tile is the background tile, it removes the tile pair from the internal array | ||
45 | -- updateMap :: Point -> Tile -> Map -> Map | ||
46 | updateMap p t (Map d xs bg) = if t == bg | ||
47 | then Map d (filter ((/= p) . fst) xs) bg | ||
48 | else case lookup p xs of | ||
49 | 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 | ||
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 | |||
54 | -- legalMoves :: Map -> Point -> [Tile] -> [Direction] | ||
55 | legalMoves m p ts = map fst $ filter legal $ map tileDir directions where | ||
56 | tileDir d = (d, getTileAtPos m $ stepInDirection p d) | ||
57 | legal (_,t) = t `elem` ts | ||
58 | |||
59 | -- fillMapRect :: Int -> Int -> Int -> Int -> Tile -> Map -> Map | ||
60 | fillMapRect x y w h t (Map d xs bg) = if bg == t | ||
61 | 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 | ||
63 | 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 | ||