35 lines
975 B
JavaScript
35 lines
975 B
JavaScript
|
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
|
||
|
}
|
||
|
|
||
|
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)
|
||
|
}
|
||
|
}
|
||
|
}
|