-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameObject.js
63 lines (49 loc) · 1.84 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
class GameObject {
constructor(config) {
this.id = null;
this.isMounted = false;
// Position
this.x = config.x || 0;
this.y = config.y || 0;
this.direction = config.direction || "down"; // Which direction Person is looking/moving towards
// Appearance
this.sprite = new Sprite({
gameObject: this,
src: config.src || "images/characters/people/hero.png",
});
//Behavior loop
this.behaviorLoop = config.behaviorLoop || [];
this.behaviorLoopIndex = 0; // which part are we at
this.talking = config.talking || [];
}
// Adds us to the scene
mount (map) {
this.isMounted = true;
map.addWall(this.x, this.y);
// If we have a behavior, kick off after a short delay
setTimeout(() => {
this.doBehaviorEvent(map);
}, 10);
}
update () {
}
async doBehaviorEvent(map) {
// If cutscene is playing, or object has no behaviors don't play anything
if(map.isCutscenePlaying || this.behaviorLoop.length === 0 || this.isStanding) {
return;
}
// Setting up event with relevant info
let eventConfig = this.behaviorLoop[this.behaviorLoopIndex];
eventConfig.who = this.id;
// Create event instance of our next even config
const eventHandler = new OverworldEvent({ map, event: eventConfig }) //OverworldEvents: movement, music change, text pop-up etc.
await eventHandler.init(); // await to fully wait the action to finish
// Move on to the next behavior
this.behaviorLoopIndex += 1;
if (this.behaviorLoopIndex === this.behaviorLoop.length) {
this.behaviorLoopIndex = 0;
}
// Do behavior again
this.doBehaviorEvent(map);
}
}