aboutsummaryrefslogtreecommitdiff
path: root/level.wren
diff options
context:
space:
mode:
authorIndrajith K L2021-08-02 13:23:30 +0530
committerIndrajith K L2021-08-02 13:23:30 +0530
commit2ff207a1d30830aeca2ea9e5d085a55ee3448ab0 (patch)
treebe75e1282d19563324cff6d4bfeac9cea053ee51 /level.wren
parentb73889da9f33b883894db6a989037030fee99f95 (diff)
downloadrebirth-wren-2ff207a1d30830aeca2ea9e5d085a55ee3448ab0.tar.gz
rebirth-wren-2ff207a1d30830aeca2ea9e5d085a55ee3448ab0.tar.bz2
rebirth-wren-2ff207a1d30830aeca2ea9e5d085a55ee3448ab0.zip
* Code Refactoring
* Bug Fixes in Map rendering
Diffstat (limited to 'level.wren')
-rw-r--r--level.wren62
1 files changed, 62 insertions, 0 deletions
diff --git a/level.wren b/level.wren
new file mode 100644
index 0000000..4aaacea
--- /dev/null
+++ b/level.wren
@@ -0,0 +1,62 @@
+import "json" for Json
+import "graphics" for ImageData
+import "math" for Math
+import "./layer_type" for LayerType
+
+class Level {
+ construct new(levelName) {
+ _levelData = Json.load("assets/%(levelName).json")
+ var tileSheetData = _levelData["tilesets"][0] // Assuming I only use single tilesheet
+ _tileSheet = ImageData.loadFromFile("assets/%(tileSheetData["image"])")
+ _tileSheetH = tileSheetData["imageheight"]
+ _tileSheetW = tileSheetData["imagewidth"]
+ _tileH = tileSheetData["tileheight"]
+ _tileW = tileSheetData["tilewidth"]
+ _levelHeight = _levelData["height"]
+ _levelWidth = _levelData["width"]
+ _layers = _levelData["layers"]
+ _rows = Math.floor(_tileSheetW/_tileW)
+ _cols = Math.floor(_tileSheetH/_tileH)
+ }
+
+ load(levelName) {
+
+ }
+
+ draw(x, y) {
+ var startX = x
+ var startY = y
+ for(layer in _layers) {
+ var index = 0
+ for(levelY in 0...(_levelHeight)) {
+ for(levelX in 0...(_levelWidth)) {
+ 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