top-down-action-adventure/src/roomObject.js

49 lines
1.3 KiB
JavaScript
Raw Normal View History

2025-06-07 08:28:54 -04:00
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
}
}
2025-06-07 08:28:54 -04:00
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)
}
2025-06-07 08:28:54 -04:00
}