-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameObject.js
More file actions
73 lines (57 loc) · 1.88 KB
/
Copy pathGameObject.js
File metadata and controls
73 lines (57 loc) · 1.88 KB
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
72
73
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,
});
this.behaviourLoop = config.behaviourLoop || [];
this.behaviourLoopIndex = 0;
this.talking = config.talking || [];
this.retryTimeout = null;
}
mount(map) {
this.isMounted = true;
//If there is a behaviour, then start the transition after a short delay
setTimeout(() => {
this.doBehaviourEvent(map);
}, 10)
}
update() {
}
async doBehaviourEvent(map) {
//Basically, don't do anything if there is a higher priority cutscene
//or there is no config on the sprite
if(this.behaviourLoop.length === 0) {
return;
}
if (map.isCutscenePlaying) {
if (this.retryTimeout) {
clearTimeout(this.retryTimeout);
}
this.retryTimeout = setTimeout(() => {
this.doBehaviourEvent(map);
console.log("check");
}, 1000)
return;
}
//Setting up event with relevant info
let eventConfig = this.behaviourLoop[this.behaviourLoopIndex];
eventConfig.who = this.id;
//Creat an event instance based on the next event config
const eventHandler = new WorldEvent({map, event: eventConfig});
//await keyword to prevent an infinite loop, and gives a timing gap
await eventHandler.init();
//Setting the next event to fire
this.behaviourLoopIndex += 1;
if (this.behaviourLoopIndex === this.behaviourLoop.length) {
this.behaviourLoopIndex = 0;
}
//Do it again
this.doBehaviourEvent(map);
}
}