-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameObject.js
71 lines (56 loc) · 1.63 KB
/
GameObject.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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",
});
//These happen once on map startup.
this.behaviorLoop = config.behaviorLoop || [];
this.behaviorLoopIndex = 0;
this.talking = config.talking || [];
this.retryTimeout = null;
}
mount(map) {
this.isMounted = true;
//If we have a behavior, kick off after a short delay
setTimeout(() => {
this.doBehaviorEvent(map);
}, 10)
}
update() {
}
async doBehaviorEvent(map) {
//I don't have config to do anything
if (this.behaviorLoop.length === 0 ) {
return;
}
//Retry later if a cutscene is playing
if (map.isCutscenePlaying) {
if (this.retryTimeout) {
clearTimeout(this.retryTimeout);
}
this.retryTimeout = setTimeout(() => {
this.doBehaviorEvent(map)
}, 1000)
return;
}
//Setting up our event with relevant info
let eventConfig = this.behaviorLoop[this.behaviorLoopIndex];
eventConfig.who = this.id;
//Create an event instance out of our next event config
const eventHandler = new OverworldEvent({ map, event: eventConfig });
await eventHandler.init();
//Setting the next event to fire
this.behaviorLoopIndex += 1;
if (this.behaviorLoopIndex === this.behaviorLoop.length) {
this.behaviorLoopIndex = 0;
}
//Do it again!
this.doBehaviorEvent(map);
}
}