-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOverworldEvent.js
More file actions
143 lines (121 loc) · 6.03 KB
/
OverworldEvent.js
File metadata and controls
143 lines (121 loc) · 6.03 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
class OverworldEvent {
constructor({ map, event }) {
this.map = map;
this.event = event; // this essentially is A behavior IN behaviorLoop in OverworldMap, the eventConfigs in GameObject (that an id attribute has been added onto)
}
stand(resolve) {
const who = this.map.gameObjects[this.event.who]; // "who" is now the gameObject retrived from passing in the object's / sprite's id
who.startBehavior({ // every object will have a method startBehavior() like the on defined in Player, allows us to dynamically call and seperate these functions
map: this.map // first argument is the "state" variable
}, { // and this is the behavior that they do
type: "stand",
direction: this.event.direction,
time: this.event.time
}) // so the object hsa started the behavior now, and once they finish, they emitted an event signal. we need to listen to that event signal
const completeHandler = e => {
if (e.detail.whoId === this.event.who) { // making sure the object/sprite is correct
document.removeEventListener("BlobStandComplete", completeHandler); // remove the listener
resolve(); // then resolve the promise
}
}
document.addEventListener("BlobStandComplete", completeHandler)
}
walk(resolve) {
const who = this.map.gameObjects[this.event.who]; // "who" is now the gameObject retrived from passing in the object's / sprite's id
who.startBehavior({ // every object will have a method startBehavior() like the on defined in Player, allows us to dynamically call and seperate these functions
map: this.map // first argument is the "state" variable
}, { // and this is the behavior that they do
type: "walk",
direction: this.event.direction,
retry: true // if this event fails, retry after a short delay
}) // so the object hsa started the behavior now, and once they finish, they emitted an event signal. we need to listen to that event signal
const completeHandler = e => {
if (e.detail.whoId === this.event.who) { // making sure the object/sprite is correct
document.removeEventListener("BlobWalkingComplete", completeHandler); // remove the listener
resolve(); // then resolve the promise
}
}
document.addEventListener("BlobWalkingComplete", completeHandler) // once the event fires, we want to fire a "completeHandler"
}
textMessage(resolve){
if (this.event.facePlayer) {
const obj = this.map.gameObjects[this.event.facePlayer]; // gets the gameObject sprite thing
obj.direction = utils.oppositeDirection(this.map.gameObjects["player"].direction);
}
const message = new TextMessage({ // making a new TextMessage from the params passed in, then just showing it on screen, simple!
text: this.event.text, // events arent limited to just walking, but can be array of texts too
name: this.event.name ? this.event.name : "",
onComplete: () => resolve() // called when textMessage is done being acknowledged by player
// just takes a text and a thing that should happen when we see the text - decouples it from the overworld, can be used in other ways (ex. battle scenes)
})
message.init(document.querySelector(".game-container")); // limits to just the window of the game-container
}
changeMap(resolve){
const sceneTransition = new SceneTransition();
sceneTransition.init(document.querySelector(".game-container"), () => {
this.map.overworld.startMap( window.OverworldMaps[this.event.map] ); // evaluates to a map config object
resolve(); // want the animation to happen in the middle
sceneTransition.fadeOut();
});
}
prologue(resolve) {
const prologue = new Prologue({
scene: this.event.scene,
onNewEvent: event => {
return new Promise(resolve => {
const prologueEvent = new PrologueEvent(event, prologue);
prologueEvent.init(resolve);
})
},
onComplete: () => {
resolve();
}
})
prologue.init(document.querySelector(".game-container"))
}
selectCharacter(resolve) {
const selectCharacter = new CharacterSelect({
onComplete: () => {
Object.values(window.OverworldMaps).forEach(map => {
map.gameObjects[this.event.who].sprite.image.src = utils.user.src;
})
resolve();
}
})
selectCharacter.init(document.querySelector(".game-container"))
}
battle(resolve) {
// maybe call battle instrcutions here - either through dialogue or another function
const battle = new Battle({
onComplete: () => {
resolve();
}
})
battle.init(document.querySelector(".game-container"))
}
end() {
const end = new End();
end.init(document.querySelector(".game-container"))
}
addStoryFlag(resolve) {
window.playerState.storyFlags[this.event.flag] = true; // the event is the {} passed in the events[] in OverworldMap, and the .flag acceess the "flag" part
resolve();
}
removeStoryFlag(resolve) {
window.playerState.storyFlags[this.event.flag] = false;
resolve();
}
removeWall(resolve) {
this.map.removeWall(utils.withGrid(this.event.x), utils.withGrid(this.event.y));
resolve();
}
addWall(resolve) {
this.map.addWall(utils.withGrid(this.event.x), utils.withGrid(this.event.y));
resolve();
}
init() {
return new Promise(resolve => {
this[this.event.type](resolve) // event.type is the string "walk" or "stand", calls the cooresponding event type function on itself, passing in resolve
})
}
}