witchday/GameObject.js

51 lines
1.1 KiB
JavaScript

class GameObject {
constructor(config) {
this.id = null;
this.isMounted = false;
this.x = config.x || 0;
this.y = config.y || 0;
this.direction = config.direction || "down";
this.sprite = new Sprite({
gameObject: this,
src: config.src || "/images/characters/people/hero.png",
});
this.behaviorLoop = config.behaviorLoop || [];
this.behaviorLoopIndex = 0;
}
mount(map) {
this.isMounted = true;
map.addWall(this.x, this.y);
// when behavior kick off after delay
setTimeout(() => {
this.doBehaviorEvent(map);
}, 10);
}
update() {
}
async doBehaviorEvent(map) {
// stop if cutscene is playing
if (map.isCutscenePlaying || this.behaviorLoop.length === 0) {
return;
}
let eventConfig = this.behaviorLoop[this.behaviorLoopIndex];
eventConfig.who = this.id;
const eventHandler = new OverworldEvent({ map, event: eventConfig });
await eventHandler.init();
this.behaviorLoopIndex += 1;
if (this.behaviorLoopIndex === this.behaviorLoop.length) {
this.behaviorLoopIndex = 0;
}
this.doBehaviorEvent(map);
}
}