aboutsummaryrefslogtreecommitdiff
path: root/level_map.wren
diff options
context:
space:
mode:
Diffstat (limited to 'level_map.wren')
-rw-r--r--level_map.wren58
1 files changed, 58 insertions, 0 deletions
diff --git a/level_map.wren b/level_map.wren
new file mode 100644
index 0000000..2bfb941
--- /dev/null
+++ b/level_map.wren
@@ -0,0 +1,58 @@
+import "json" for Json
+import "graphics" for ImageData
+import "math" for Math
+import "./layer_type" for LayerType
+
+class LevelMap {
+ construct new(mapName) {
+ _mapData = Json.load("assets/%(mapName).json")
+ var tileSheetData = _mapData["tilesets"][0] // Assuming there is only a single tilesheet/level
+ _tileSheet = ImageData.loadFromFile("assets/%(tileSheetData["image"])")
+ _tileSheetH = tileSheetData["imageheight"]
+ _tileSheetW = tileSheetData["imagewidth"]
+ _tileH = tileSheetData["tileheight"]
+ _tileW = tileSheetData["tilewidth"]
+ _mapHeight = _mapData["height"]
+ _mapWidth = _mapData["width"]
+ _layers = _mapData["layers"]
+ _rows = Math.floor(_tileSheetW/_tileW)
+ _cols = Math.floor(_tileSheetH/_tileH)
+ }
+
+ draw(x, y) {
+ var startX = x
+ var startY = y
+ for(layer in _layers) {
+ var index = 0
+ for(mapYV in 0...(_mapHeight)) {
+ for(mapXV in 0...(_mapWidth)) {
+ if((layer["type"] == LayerType.Tile) && (layer["visible"] == true)) {
+ var tileIndex = layer["data"][index]
+ if (tileIndex > 0) {
+ drawTile(tileIndex-1, x, y)
+ }
+ }
+ index = index + 1
+ x = x + 16
+ }
+ y = y + 16
+ x = startX
+ }
+ x = startX
+ y = startY
+ }
+ }
+
+ drawTile(tileIndex, mapX, mapY) {
+ var spriteX = (tileIndex % _rows)
+ var spriteY = Math.floor ((tileIndex - spriteX) % _cols)
+ _tileSheet.transform({
+ "srcX": spriteX * 16,
+ "srcY": spriteY * 16,
+ "srcW": _tileW,
+ "srcH": _tileH,
+ "scaleX": 1,
+ "scaleY": 1
+ }).draw(mapX,mapY)
+ }
+} \ No newline at end of file