import { doRectanglesOverlap } from "./util.js" export default class RoomObject { constructor(game) { this.game = game } static fromJson(game, json) { const roomObject = new RoomObject(game) Object.entries(json).forEach(([key, value]) => roomObject[key] = value) return roomObject } getProperty(name) { const property = this.properties.find(p => p.name == name) if (!property) { // console.error(`Unknown property ${name} on ${this.name}`) return null } return property.value } setProperty(name, value) { const p = this.properties.find(p => p.name == name) if (p) { p.value = value } else { this.properties[name] = value } } tick(dt) { const { player } = this.game if (doRectanglesOverlap(player, this)) { const eventName = this.getProperty.call(this, "event") if (eventName) this.game.triggerEvent(eventName, this) } if (player.interactHitbox && doRectanglesOverlap(player.interactHitbox, this)) { const eventName = this.getProperty("interactEvent") if (eventName) this.game.triggerEvent(eventName, this) } } draw(ctx) { ctx.fillStyle = this.getProperty("color") || "#00000000" ctx.fillRect(this.x, this.y, this.width, this.height) } }