witchday/OverworldEvent.js
2024-08-05 19:36:41 +02:00

72 lines
1.6 KiB
JavaScript

class OverworldEvent {
constructor({map, event}) {
this.map = map;
this.event = event;
}
stand(resolve) {
const who = this.map.gameObjects[ this.event.who ];
who.startBehavior({
map: this.map
}, {
type: "stand",
direction: this.event.direction,
time: this.event.time
})
// handler to complete when correct person is done walking
const completeHandler = e => {
if (e.detail.whoId === this.event.who) {
document.removeEventListener("PersonStandComplete", completeHandler);
resolve();
}
}
document.addEventListener("PersonStandComplete", completeHandler)
}
walk(resolve) {
const who = this.map.gameObjects[ this.event.who ];
who.startBehavior({
map: this.map
}, {
type: "walk",
direction: this.event.direction,
retry: true
})
// handler to complete when correct person is done walking
const completeHandler = e => {
if (e.detail.whoId === this.event.who) {
document.removeEventListener("PersonWalkingComplete", completeHandler);
resolve();
}
}
document.addEventListener("PersonWalkingComplete", completeHandler)
}
textMessage(resolve) {
if (this.event.faceHero) {
const obj = this.map.gameObjects[this.event.faceHero];
obj.direction = utils.oppositeDirection(this.map.gameObjects["hero"].direction);
}
const message = new TextMessage({
text: this.event.text,
onComplete: () => resolve()
})
message.init( document.querySelector(".game-container") )
}
changeMap(resolve) {
this.map.overworld.startMap(window.OverworldMaps[this.event.map]);
resolve();
}
init() {
return new Promise(resolve => {
this[this.event.type](resolve)
})
}
}