aboutsummaryrefslogtreecommitdiff
path: root/src/Game/Map.hs
diff options
context:
space:
mode:
authorJuan J. Martinez <jjm@usebox.net>2023-02-07 16:08:13 +0000
committerJuan J. Martinez <jjm@usebox.net>2023-02-07 16:08:13 +0000
commit9534c490c5c318e6c29ebf881ac5350545f479e5 (patch)
treea1d3e04db1fbeae883502668e1444763c2a28a69 /src/Game/Map.hs
parent54d3f5a32758eb92606338d28a13652bcf51375c (diff)
downloadspace-plat-hs-9534c490c5c318e6c29ebf881ac5350545f479e5.tar.gz
space-plat-hs-9534c490c5c318e6c29ebf881ac5350545f479e5.zip
Using OverloadedRecordDot
Diffstat (limited to 'src/Game/Map.hs')
-rw-r--r--src/Game/Map.hs50
1 files changed, 22 insertions, 28 deletions
diff --git a/src/Game/Map.hs b/src/Game/Map.hs
index 7866f97..94478bd 100644
--- a/src/Game/Map.hs
+++ b/src/Game/Map.hs
@@ -13,24 +13,24 @@ import Text.JSON
import Text.JSON.Types
data Tileset = Tileset
- { tsCols :: Int,
- tsWidth :: Int,
- tsHeight :: Int,
- tsFirstGid :: Int
+ { cols :: Int,
+ width :: Int,
+ height :: Int,
+ firstGid :: Int
}
deriving (Show)
data TileLayer = TileLayer
- { tlName :: String,
- tlTiles :: [Int]
+ { name :: String,
+ tiles :: [Int]
}
deriving (Show)
data MapData = MapData
- { mWidth :: Int,
- mHeight :: Int,
- mTileset :: Tileset,
- mLayers :: [TileLayer]
+ { width :: Int,
+ height :: Int,
+ tileset :: Tileset,
+ layers :: [TileLayer]
}
deriving (Show)
@@ -78,13 +78,10 @@ load filename tex = do
-- | Check for collision detection vs "Blocked" TileLayer that MUST be last layer.
-- x and y in pixels.
isBlocked :: Map -> Int -> Int -> Bool
-isBlocked (Map (MapData width _ ts layers) _) x y =
- tiles !! ((x `div` tw) + (y `div` th) * width) >= firstgid
+isBlocked (Map (MapData mapWidth _ ts ls) _) x y =
+ blocked !! ((x `div` ts.width) + (y `div` ts.height) * mapWidth) >= ts.firstGid
where
- tiles = tlTiles (last layers)
- tw = tsWidth ts
- th = tsHeight ts
- firstgid = tsFirstGid ts
+ blocked = (last ls).tiles
-- | Renders a map.
render :: SDL.Renderer -> Map -> IO ()
@@ -93,28 +90,25 @@ render renderer (Map mapData tex) = do
( \layer ->
mapM_
( \(x, y) ->
- renderTile x y $ tlTiles layer !! (x + (y * mWidth mapData))
+ renderTile x y $ layer.tiles !! (x + (y * mapData.width))
)
index
)
- (init $ mLayers mapData)
+ (init $ mapData.layers)
where
- mw = mWidth mapData
- mh = mHeight mapData
- index = [(x, y) | x <- [0 .. mw - 1], y <- [0 .. mh - 1]]
+ index = [(x, y) | x <- [0 .. mapData.width - 1], y <- [0 .. mapData.height - 1]]
- ts = mTileset mapData
- firstgid = tsFirstGid ts
- cols = tsCols ts
- tileWidth = tsWidth ts
- tileHeight = tsHeight ts
+ columns = mapData.tileset.cols
+ firstgid = mapData.tileset.firstGid
+ tileWidth = mapData.tileset.width
+ tileHeight = mapData.tileset.height
renderTile :: Int -> Int -> Int -> IO ()
renderTile x y tile
| tile < firstgid = pure ()
| otherwise = do
- let tx = (tile - firstgid) `rem` cols
- ty = (tile - firstgid) `div` cols
+ let tx = (tile - firstgid) `rem` columns
+ ty = (tile - firstgid) `div` columns
src = U.rect (tx * tileWidth) (ty * tileHeight) tileWidth tileHeight
dst = U.rect (x * tileWidth) (y * tileHeight) tileWidth tileHeight
SDL.copy renderer tex (Just src) (Just dst)