From b73889da9f33b883894db6a989037030fee99f95 Mon Sep 17 00:00:00 2001 From: Indrajith K L Date: Sun, 1 Aug 2021 02:26:00 +0530 Subject: Basic Renderer * Adds Tiled map JSON rendering * TODO Cleanup & Organization --- .gitignore | 1 + assets/level1.json | 73 ++++++++++++++++++++++++++++++++++++++++++++++ assets/level1.tmx | 67 ++++++++++++++++++++++++++++++++++++++++++ assets/main_tileset.tsx | 4 +++ assets/tilemap_packed.png | Bin 0 -> 4501 bytes layerType.wren | 5 ++++ main.wren | 72 +++++++++++++++++++++++++++++++++++++++++++++ scratch.md | 22 ++++++++++++++ 8 files changed, 244 insertions(+) create mode 100644 .gitignore create mode 100644 assets/level1.json create mode 100644 assets/level1.tmx create mode 100644 assets/main_tileset.tsx create mode 100644 assets/tilemap_packed.png create mode 100644 layerType.wren create mode 100644 main.wren create mode 100644 scratch.md diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..bf0824e --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.log \ No newline at end of file diff --git a/assets/level1.json b/assets/level1.json new file mode 100644 index 0000000..ea69794 --- /dev/null +++ b/assets/level1.json @@ -0,0 +1,73 @@ +{ "compressionlevel":-1, + "editorsettings": + { + "export": + { + "format":"json", + "target":"level1.json" + } + }, + "height":16, + "infinite":false, + "layers":[ + { + "data":[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + "height":16, + "id":4, + "name":"Tile Layer 3", + "opacity":1, + "type":"tilelayer", + "visible":true, + "width":16, + "x":0, + "y":0 + }], + "nextlayerid":5, + "nextobjectid":1, + "orientation":"orthogonal", + "renderorder":"right-down", + "tiledversion":"1.5.0", + "tileheight":16, + "tilesets":[ + { + "columns":17, + "firstgid":1, + "image":"tilemap_packed.png", + "imageheight":128, + "imagewidth":272, + "margin":0, + "name":"main_tileset", + "spacing":0, + "tilecount":136, + "tileheight":16, + "tilewidth":16 + }], + "tilewidth":16, + "type":"map", + "version":1.5, + "width":16 +} \ No newline at end of file diff --git a/assets/level1.tmx b/assets/level1.tmx new file mode 100644 index 0000000..2fe128f --- /dev/null +++ b/assets/level1.tmx @@ -0,0 +1,67 @@ + + + + + + + + +1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, +1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, +1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, +1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, +1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, +1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, +1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, +1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, +1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, +1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, +1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, +1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, +1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, +1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, +1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, +1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 + + + + +0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, +0,0,0,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ff --git a/assets/main_tileset.tsx b/assets/main_tileset.tsx new file mode 100644 index 0000000..0b0b1d6 --- /dev/null +++ b/assets/main_tileset.tsx @@ -0,0 +1,4 @@ + + + + diff --git a/assets/tilemap_packed.png b/assets/tilemap_packed.png new file mode 100644 index 0000000..6f47609 Binary files /dev/null and b/assets/tilemap_packed.png differ diff --git a/layerType.wren b/layerType.wren new file mode 100644 index 0000000..cee6b82 --- /dev/null +++ b/layerType.wren @@ -0,0 +1,5 @@ +// ENUM lol +class LayerType { + static Tile { "tilelayer" } + static Object { "objectgroup" } +} \ No newline at end of file diff --git a/main.wren b/main.wren new file mode 100644 index 0000000..1d472b0 --- /dev/null +++ b/main.wren @@ -0,0 +1,72 @@ +import "graphics" for ImageData +import "json" for Json +import "graphics" for Canvas, Color +import "./layerType" for LayerType +import "dome" for Window +import "math" for Math +class Main { + construct new() { + _tileSheet = ImageData.loadFromFile("assets/tilemap_packed.png") + _tileSheetH = 128 + _tileSheetW = 272 + _tileH = 16 + _tileW = 16 + _rows = _tileSheetW/_tileW + _cols = _tileSheetH/_tileH + _levelData = Json.load("assets/level1.json") + _levelHeight = _levelData["tileheight"] + _levelWidth = _levelData["tilewidth"] + _layers = _levelData["layers"] + Window.resize(256, 256) + Canvas.resize(256, 256) + + } + + init() { + + } + + update() { + + } + + 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*16,mapY*16) + } + + drawLevel(x, 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, levelX, levelY) + } + } + index = index + 1 + } + } + } + } + + getTile() { + + } + + draw(alpha) { + drawLevel(0,0) + } +} + +var Game = Main.new() \ No newline at end of file diff --git a/scratch.md b/scratch.md new file mode 100644 index 0000000..0eb82bf --- /dev/null +++ b/scratch.md @@ -0,0 +1,22 @@ + +## Wren Scratch +### Matrix +```wren +var matrix = [[1, 2, 3], [4, 5, 6]] +System.print(matrix[0][0]) +for(layer in _layerData) { + System.print(layer) +} +``` + +### Sprite Sheet Transform +```wren +_spriteSheet.transform({ + "srcX": 16*14, + "srcY": 0, + "srcW": 16, + "srcH": 16, + "scaleX": 2, + "scaleY": 2 + }).draw(0,0) +``` \ No newline at end of file -- cgit v1.2.3