From c3c5832587bfd36d12790537c5acb36c02574ddb Mon Sep 17 00:00:00 2001 From: Indrajith K L Date: Tue, 3 Aug 2021 23:06:26 +0530 Subject: Failed attempt to build collision detection --- assets/level1.json | 30 ++++++++++++++++++++++++++---- assets/level1.tmx | 7 +++++-- entities/player_entity.wren | 32 +++++++++++++++++++++++++++----- game_state.wren | 37 ++++++++++++++++++++++++++++++++++++- layer_type.wren | 1 + level_map.wren | 27 +++++++++++++++------------ levels/level1.wren | 8 ++++---- 7 files changed, 114 insertions(+), 28 deletions(-) diff --git a/assets/level1.json b/assets/level1.json index fe44270..c2e6e68 100644 --- a/assets/level1.json +++ b/assets/level1.json @@ -47,8 +47,30 @@ "type":"player", "visible":true, "width":16, - "x":16, - "y":32 + "x":0, + "y":0 + }], + "opacity":1, + "type":"objectgroup", + "visible":true, + "x":0, + "y":0 + }, + { + "draworder":"topdown", + "id":6, + "name":"colliders", + "objects":[ + { + "height":60.5, + "id":7, + "name":"", + "rotation":0, + "type":"collider", + "visible":true, + "width":47.75, + "x":95.75, + "y":55.5 }], "opacity":1, "type":"objectgroup", @@ -56,8 +78,8 @@ "x":0, "y":0 }], - "nextlayerid":6, - "nextobjectid":2, + "nextlayerid":7, + "nextobjectid":8, "orientation":"orthogonal", "renderorder":"right-down", "tiledversion":"1.5.0", diff --git a/assets/level1.tmx b/assets/level1.tmx index e767d39..581d91c 100644 --- a/assets/level1.tmx +++ b/assets/level1.tmx @@ -1,5 +1,5 @@ - + @@ -43,6 +43,9 @@ - + + + + diff --git a/entities/player_entity.wren b/entities/player_entity.wren index 194bb5f..c547e4e 100644 --- a/entities/player_entity.wren +++ b/entities/player_entity.wren @@ -2,26 +2,48 @@ import "graphics" for ImageData, Canvas import "./controls" for Controls import "./config" for Config class Player { - construct new(x,y) { + construct new(x,y, gameState) { + __gameState = gameState _x = x _y = y _playerSprite = ImageData.loadFromFile("assets/sprites/player.png") } + x { _x } + y { _y } + w { 16 } + h { 16 } + update() { - if(Controls.isKeyDown(Config.KeyboardConstants["UP"])) { + + var collided = __gameState.checkCollision(this) + + if(Controls.isKeyDown(Config.KeyboardConstants["UP"]) && !collided) { _y = _y - 1 + } else if(Controls.isKeyDown(Config.KeyboardConstants["UP"]) && collided){ + _y = _y + 1 } - if(Controls.isKeyDown(Config.KeyboardConstants["DOWN"])) { + + if(Controls.isKeyDown(Config.KeyboardConstants["DOWN"]) && !collided) { _y = _y + 1 + } else if(Controls.isKeyDown(Config.KeyboardConstants["DOWN"]) && collided){ + _y = _y - 1 } - if(Controls.isKeyDown(Config.KeyboardConstants["LEFT"])) { + + if(Controls.isKeyDown(Config.KeyboardConstants["LEFT"]) && !collided) { _x = _x - 1 + } else if(Controls.isKeyDown(Config.KeyboardConstants["LEFT"]) && collided){ + _x = _x + 1 } - if(Controls.isKeyDown(Config.KeyboardConstants["RIGHT"])) { + + if(Controls.isKeyDown(Config.KeyboardConstants["RIGHT"]) && !collided) { _x = _x + 1 + } else if(Controls.isKeyDown(Config.KeyboardConstants["RIGHT"]) && collided){ + _x = _x - 1 } + + } draw(dt) { diff --git a/game_state.wren b/game_state.wren index 42e1189..e3208b7 100644 --- a/game_state.wren +++ b/game_state.wren @@ -11,7 +11,8 @@ class GameState { AudioEngine.load("select","assets/sfx/select.wav") AudioEngine.load("credit","assets/musics/Juhani Junkala [Retro Game Music Pack] Ending.wav") _fullScreen = false - + _entities = [] + _collidables = [] setupStates() } @@ -31,15 +32,49 @@ class GameState { switch(level) { AudioEngine.stopAllChannels() + _entities.clear() + _collidables.clear() _currentLevel = __gameStates[level].new(this) System.print("Current Level : %(__gameStates[level].name)") } update() { _currentLevel.update() + for(entity in _entities) { + entity.update() + } } draw(dt) { _currentLevel.draw(dt) + for(entity in _entities) { + entity.draw(dt) + } + } + + addEntities(entity) { + _entities.add(entity) + } + + addCollidable(collidable) { + _collidables.add(collidable) + } + + checkCollision(entity) { + var collide = false + for(collidable in _collidables) { + System.print(entity.x) + var entityX = entity.x + var entityY = entity.y + if(entityX < (collidable["x"] + collidable["width"]) && + (entityX + entity.w) > collidable["x"] && + entityY < (collidable["y"] + collidable["height"]) && + (entityY + entity.h) > collidable["y"]) { + collide = true + break + } + } + + return collide } } \ No newline at end of file diff --git a/layer_type.wren b/layer_type.wren index fb1f5c5..3bbe0e6 100644 --- a/layer_type.wren +++ b/layer_type.wren @@ -8,4 +8,5 @@ class ObjectType { static PLAYER { "player" } static NPC { "npc" } static ENEMY { "enemy" } + static COLLIDER { "collider" } } \ No newline at end of file diff --git a/level_map.wren b/level_map.wren index 88ce9c6..4d46321 100644 --- a/level_map.wren +++ b/level_map.wren @@ -4,7 +4,8 @@ import "math" for Math import "./layer_type" for LayerType, ObjectType import "./entities/player_entity" for Player class LevelMap { - construct new(mapName) { + construct new(mapName, gameState) { + __gameState = gameState _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"])") @@ -21,8 +22,8 @@ class LevelMap { "x": 0, "y": 0 } + _levelEntities = [] placeObjects() - } playerPos = (position) { @@ -40,22 +41,24 @@ class LevelMap { "x": object["x"], "y": object["y"] } - _player = Player.new(playerPos["x"], playerPos["y"]) + _player = Player.new(playerPos["x"], playerPos["y"], __gameState) + __gameState.addEntities(_player) + } + + if(object["type"]==ObjectType.COLLIDER) { + __gameState.addCollidable({ + "x": Math.floor(object["x"]), + "y": Math.floor(object["y"]), + "width": Math.floor(object["width"]), + "height": Math.floor(object["height"]) + }) } } } } } - draw(dt) { - _player.draw(dt) - } - - update() { - _player.update() - } - - render(x, y) { + draw(x, y) { var startX = x var startY = y for(layer in _layers) { diff --git a/levels/level1.wren b/levels/level1.wren index d113a25..d3daaf8 100644 --- a/levels/level1.wren +++ b/levels/level1.wren @@ -5,20 +5,20 @@ import "./config" for Config class Level1 { construct new(gameState) { __gameState = gameState - _level1 = LevelMap.new("level1") + _level1 = LevelMap.new("level1", __gameState) var channel = AudioEngine.play("level1_bg") channel.volume = 0.2 } update() { + if(Controls.justPressed(Config.KeyboardConstants["ATTACK"])) { } - _level1.update() + } draw(dt) { - _level1.render(0,0) - _level1.draw(dt) + _level1.draw(0,0) } } \ No newline at end of file -- cgit v1.2.3