From 2ff207a1d30830aeca2ea9e5d085a55ee3448ab0 Mon Sep 17 00:00:00 2001 From: Indrajith K L Date: Mon, 2 Aug 2021 13:23:30 +0530 Subject: * Code Refactoring * Bug Fixes in Map rendering --- config.wren | 39 +++++++++++++++++++++++++++++++++++ controls.wren | 11 ++++++++++ layerType.wren | 5 ----- layer_type.wren | 5 +++++ level.wren | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ main.wren | 63 +++++++++++---------------------------------------------- map.wren | 5 +++++ 7 files changed, 134 insertions(+), 56 deletions(-) create mode 100644 config.wren create mode 100644 controls.wren delete mode 100644 layerType.wren create mode 100644 layer_type.wren create mode 100644 level.wren create mode 100644 map.wren diff --git a/config.wren b/config.wren new file mode 100644 index 0000000..a1b8cec --- /dev/null +++ b/config.wren @@ -0,0 +1,39 @@ +import "graphics" for Canvas +import "dome" for Window +class Config { + construct new() { + + } + + static KeyboardConstants { + return { + "UP": "up", + "DOWN": "down", + "LEFT": "left", + "RIGHT": "right", + "PICK" : "c", + "SELECT": "z", + "ATTACK": "x", + "QUIT": "escape" + } + } + + static setup() { + Window.resize(800, 600) + Canvas.resize(800, 600) + Window.title = "REBIRTH" + } + + // static KeyboardConstants() { + // return { + // "UP": "up", + // "DOWN": "down", + // "LEFT": "left", + // "RIGHT": "right", + // "PICK" : "c", + // "SELECT": "z", + // "ATTACK": "x", + // "QUIT": "esc" + // } + // } +} \ No newline at end of file diff --git a/controls.wren b/controls.wren new file mode 100644 index 0000000..3804020 --- /dev/null +++ b/controls.wren @@ -0,0 +1,11 @@ +import "input" for Keyboard + +class Controls { + construct new() { + + } + + static detect(keyCode) { + return Keyboard.isKeyDown(keyCode) + } +} \ No newline at end of file diff --git a/layerType.wren b/layerType.wren deleted file mode 100644 index cee6b82..0000000 --- a/layerType.wren +++ /dev/null @@ -1,5 +0,0 @@ -// ENUM lol -class LayerType { - static Tile { "tilelayer" } - static Object { "objectgroup" } -} \ No newline at end of file diff --git a/layer_type.wren b/layer_type.wren new file mode 100644 index 0000000..cee6b82 --- /dev/null +++ b/layer_type.wren @@ -0,0 +1,5 @@ +// ENUM lol +class LayerType { + static Tile { "tilelayer" } + static Object { "objectgroup" } +} \ No newline at end of file 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 diff --git a/main.wren b/main.wren index 1d472b0..7e39844 100644 --- a/main.wren +++ b/main.wren @@ -1,25 +1,17 @@ import "graphics" for ImageData import "json" for Json import "graphics" for Canvas, Color -import "./layerType" for LayerType -import "dome" for Window +import "dome" for Window, Process import "math" for Math +import "config" for Config +import "input" for Keyboard +import "./controls" for Controls +import "./level" for Level + 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) - + Config.setup() + __level = Level.new("level1") } init() { @@ -27,45 +19,14 @@ class Main { } 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 - } - } + if(Controls.detect(Config.KeyboardConstants["QUIT"])) { + Process.exit(0) } } - getTile() { - - } - draw(alpha) { - drawLevel(0,0) + Canvas.cls(Color.pink) + __level.draw(0,0) } } diff --git a/map.wren b/map.wren new file mode 100644 index 0000000..e9a37ee --- /dev/null +++ b/map.wren @@ -0,0 +1,5 @@ +class Map { + construct new() { + + } +} \ No newline at end of file -- cgit v1.2.3