From 7d988900e2f73992fea504419d61d4cdf759fedf Mon Sep 17 00:00:00 2001 From: Indrajith K L Date: Mon, 2 Aug 2021 13:48:25 +0530 Subject: * Basic Game State Implementation * Code Refactoring --- game_state.wren | 17 +++++++++++++++ level.wren | 62 ------------------------------------------------------ level_map.wren | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++ levels/level1.wren | 14 ++++++++++++ levels/level2.wren | 5 +++++ main.wren | 11 ++++++---- map.wren | 5 ----- 7 files changed, 101 insertions(+), 71 deletions(-) create mode 100644 game_state.wren delete mode 100644 level.wren create mode 100644 level_map.wren create mode 100644 levels/level1.wren create mode 100644 levels/level2.wren delete mode 100644 map.wren diff --git a/game_state.wren b/game_state.wren new file mode 100644 index 0000000..94e8a99 --- /dev/null +++ b/game_state.wren @@ -0,0 +1,17 @@ +class GameState { + construct new() { + + } + + switch(level) { + _currentLevel = level.new() + } + + update() { + _currentLevel.update() + } + + draw(dt) { + _currentLevel.draw(dt) + } +} \ No newline at end of file diff --git a/level.wren b/level.wren deleted file mode 100644 index 4aaacea..0000000 --- a/level.wren +++ /dev/null @@ -1,62 +0,0 @@ -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/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 diff --git a/levels/level1.wren b/levels/level1.wren new file mode 100644 index 0000000..c485924 --- /dev/null +++ b/levels/level1.wren @@ -0,0 +1,14 @@ +import "./level_map" for LevelMap +class Level1 { + construct new() { + _level1 = LevelMap.new("level1") + } + + update() { + + } + + draw(dt) { + _level1.draw(0,0) + } +} \ No newline at end of file diff --git a/levels/level2.wren b/levels/level2.wren new file mode 100644 index 0000000..6323ad5 --- /dev/null +++ b/levels/level2.wren @@ -0,0 +1,5 @@ +class Level2 { + construct new() { + + } +} \ No newline at end of file diff --git a/main.wren b/main.wren index 7e39844..370860e 100644 --- a/main.wren +++ b/main.wren @@ -6,12 +6,14 @@ import "math" for Math import "config" for Config import "input" for Keyboard import "./controls" for Controls -import "./level" for Level +import "./game_state" for GameState +import "./levels/level1" for Level1 class Main { construct new() { Config.setup() - __level = Level.new("level1") + __gameState = GameState.new() + __gameState.switch(Level1) } init() { @@ -22,11 +24,12 @@ class Main { if(Controls.detect(Config.KeyboardConstants["QUIT"])) { Process.exit(0) } + __gameState.update() } - draw(alpha) { + draw(dt) { Canvas.cls(Color.pink) - __level.draw(0,0) + __gameState.draw(dt) } } diff --git a/map.wren b/map.wren deleted file mode 100644 index e9a37ee..0000000 --- a/map.wren +++ /dev/null @@ -1,5 +0,0 @@ -class Map { - construct new() { - - } -} \ No newline at end of file -- cgit v1.2.3