-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameObject.js
59 lines (47 loc) · 1.64 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
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;
this.talking = config.talking || [];
}
mount(map) {
this.isMounted = true;
map.addWall(this.x, this.y);
//Si tenemos un behavior, inicia después de un breve retraso
setTimeout(() => {
this.doBehaviorEvent(map);
}, 10)
}
update() {
}
async doBehaviorEvent(map) {
//No hacer nada si hay cutscene importante o no tenemos una config para
//hacer nada
if (map.isCutscenePlaying || this.behaviorLoop.length === 0 || this.isStanding){
return;
}
//Configurando evento con info relevante
let eventConfig = this.behaviorLoop[this.behaviorLoopIndex];
eventConfig.who = this.id;
//Crear una instancia de evento a partir de la configuración
//del próximo evento
const eventHandler = new OverworldEvent({ map, event: eventConfig });
await eventHandler.init();
//Configura el proximo evento para dispararlo
this.behaviorLoopIndex += 1;
if (this.behaviorLoopIndex === this.behaviorLoop.length){
this.behaviorLoopIndex = 0;
}
//Do it again!
this.doBehaviorEvent(map);
}
}