From 499814553f42a70e5e34af32b1b4b6aaa9e95418 Mon Sep 17 00:00:00 2001 From: Bill Rossi Date: Sat, 7 Jun 2025 11:07:53 -0400 Subject: [PATCH] Collide with all tile layers --- src/player.js | 2 +- src/room.js | 12 +++- tilesets/kenney_rpg.tsj | 135 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 146 insertions(+), 3 deletions(-) diff --git a/src/player.js b/src/player.js index 9b71ec8..bcc8bfe 100644 --- a/src/player.js +++ b/src/player.js @@ -27,7 +27,7 @@ export default class Player extends Actor { this.x += this.xVel this.y += this.yVel - const tur = this.game.currentRoom.tilesUnderRectangle(this.game.currentRoom.json.layers[0], this).filter(x => x) + const tur = this.game.currentRoom.tilesUnderRectangle(this).filter(x => x) const colliders = tur.filter(tile => tile.properties.find(prop => prop.name == "collides" && prop.value)) if (tur.length >= 1) { this.x -= this.xVel diff --git a/src/room.js b/src/room.js index c0057c6..4a49401 100644 --- a/src/room.js +++ b/src/room.js @@ -26,14 +26,22 @@ export default class Room { } draw(ctx) { - this.json.layers.forEach(this.drawLayer.bind(this, ctx)) + this.tileLayers.forEach(this.drawTileLayer.bind(this, ctx)) } drawLayer(ctx, layer) { if (layer.type == "tilelayer") this.drawTileLayer(ctx, layer) } - tilesUnderRectangle(layer, rect) { + get tileLayers() { + return this.json.layers.filter(layer => layer.type == "tilelayer") + } + + tilesUnderRectangle(rect) { + return this.tileLayers.map(layer => this.tilesUnderRectangleInLayer(layer, rect)).flat() + } + + tilesUnderRectangleInLayer(layer, rect) { return [{ x: rect.x, y: rect.y }, { x: rect.x + rect.width, y: rect.y }, { x: rect.x, y: rect.y + rect.height }, diff --git a/tilesets/kenney_rpg.tsj b/tilesets/kenney_rpg.tsj index 9dda02e..88cd864 100644 --- a/tilesets/kenney_rpg.tsj +++ b/tilesets/kenney_rpg.tsj @@ -224,6 +224,141 @@ "type":"bool", "value":true }] + }, + { + "id":180, + "properties":[ + { + "name":"collides", + "type":"bool", + "value":true + }] + }, + { + "id":182, + "properties":[ + { + "name":"collides", + "type":"bool", + "value":true + }] + }, + { + "id":184, + "properties":[ + { + "name":"collides", + "type":"bool", + "value":true + }] + }, + { + "id":200, + "properties":[ + { + "name":"collides", + "type":"bool", + "value":true + }] + }, + { + "id":201, + "properties":[ + { + "name":"collides", + "type":"bool", + "value":true + }] + }, + { + "id":202, + "properties":[ + { + "name":"collides", + "type":"bool", + "value":true + }] + }, + { + "id":203, + "properties":[ + { + "name":"collides", + "type":"bool", + "value":true + }] + }, + { + "id":204, + "properties":[ + { + "name":"collides", + "type":"bool", + "value":true + }] + }, + { + "id":205, + "properties":[ + { + "name":"collides", + "type":"bool", + "value":true + }] + }, + { + "id":220, + "properties":[ + { + "name":"collides", + "type":"bool", + "value":true + }] + }, + { + "id":221, + "properties":[ + { + "name":"collides", + "type":"bool", + "value":true + }] + }, + { + "id":222, + "properties":[ + { + "name":"collides", + "type":"bool", + "value":true + }] + }, + { + "id":223, + "properties":[ + { + "name":"collides", + "type":"bool", + "value":true + }] + }, + { + "id":224, + "properties":[ + { + "name":"collides", + "type":"bool", + "value":true + }] + }, + { + "id":225, + "properties":[ + { + "name":"collides", + "type":"bool", + "value":true + }] }], "tilewidth":64, "type":"tileset",