-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameObject.js
More file actions
62 lines (47 loc) · 2.29 KB
/
GameObject.js
File metadata and controls
62 lines (47 loc) · 2.29 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
class GameObject {
constructor(config) { //config can be anything, i think its just an arbitrary data type
this.id = null;
this.isMounted = false;
this.x = config.x || 0;
this.y = config.y || 0;
this.direction = config.direction || "down";
this.isPlayerControlled = config.isPlayerControlled || false;
this.sprite = new Sprite({
gameObject: this,
src: config.src || window.playerState.playerSrc || "images/characters/average cute blob.png",
useShadow: config.useShadow,
frameX: config.frameX || 32,
frameY: config.frameY || 32,
Yoffset: config.Yoffset,
});
this.behaviorLoop = config.behaviorLoop || [];
this.behaviorLoopIndex = 0; // an index to track which part of the behavior we are at
this.talking = config.talking || [];
}
mount(map) { // when the object mounts (is blitted), we want it to add a wall at its location
this.isMounted = true;
map.addWall(this.x, this.y);
// if assigned a behavior, kick off after a short delay
setTimeout(() => {
this.doBehaviorEvent(map)
}, 10);
}
update() {
}
async doBehaviorEvent(map) {
// makes sure to short circuit if there is a bigger cutscene going - always want to honor that first
if (map.isCutscenePlaying || this.behaviorLoop.length === 0 || this.isStanding) {
return;
}
// identify which event to execute rn
let eventConfig = this.behaviorLoop[this.behaviorLoopIndex];
eventConfig.who = this.id; // whatever the id of the gameObject is that is calling this method (because each one does)
const eventHandler = new OverworldEvent({ map, event: eventConfig }); // OverworldEvent is going to instruct the blobs to do the blob thing, kinda like the puppetmaster
await eventHandler.init(); // await tells the code : yo, this line is gonna take some time, dont do anything until we're done; need to mark the function as async
this.behaviorLoopIndex += 1;
if (this.behaviorLoopIndex === this.behaviorLoop.length) {
this.behaviorLoopIndex = 0;
}
this.doBehaviorEvent(map) // once we reach the end, re-call again
}
}