/
GameEvent.ts
366 lines (341 loc) · 12.3 KB
/
GameEvent.ts
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
import {StoragePosition} from "Storage";
import {GoldenSun} from "../GoldenSun";
import {NPC} from "../NPC";
import * as _ from "lodash";
import {msg_types} from "../Logger";
type BasicValues = string | number | boolean;
/** Event value detailed info. */
export type DetailedValues = {
/** Storage or MainChar key name. */
key_name: string;
/** The target object type to has its value retrieved. */
type: game_info_types;
/** The property name of the target that you want to retrieve the value of. */
property: string;
/** NPC, IO or TileEvent unique label. */
label: string;
/** NPC, IO or TileEvent index. */
index: number;
/** The value to be set or used. */
value: BasicValues | StoragePosition;
};
export enum game_info_types {
CHAR = "char",
HERO = "hero",
NPC = "npc",
EVENT = "event",
INTERACTABLE_OBJECT = "interactable_object",
}
export enum event_value_types {
VALUE = "value",
STORAGE = "storage",
GAME_INFO = "game_info",
}
export type EventValue = {
type: event_value_types;
value: BasicValues | DetailedValues;
};
export enum event_types {
BATTLE = "battle",
BRANCH = "branch",
SET_VALUE = "set_value",
MOVE = "move",
DIALOG = "dialog",
LOOK = "look",
CHEST = "chest",
PSYNERGY_STONE = "psynergy_stone",
TIMER = "timer",
PARTY_JOIN = "party_join",
SUMMON = "summon",
DJINN_GET = "djinn_get",
DJINN_SET_STATUS = "djinn_set_status",
JUMP = "jump",
FACE_DIRECTION = "face_direction",
EMOTICON = "emoticon",
TILE_EVENT_MANAGE = "tile_event_manage",
DESTROYER = "destroyer",
CHAR_LEVEL_CHANGE = "char_level_change",
MAP_OPACITY = "map_opacity",
MAP_BLEND_MODE = "map_blend_mode",
IO_ANIM_PLAY = "io_animation_play",
CHAR_ANIMATION_PLAY = "char_animation_play",
SET_CHAR_ACTIVATION = "set_char_activation",
AUDIO_PLAY = "audio_play",
CONTROL_BGM = "control_bgm",
SET_PARTY_COINS = "set_party_coins",
CHAR_EXP = "char_exp",
CHAR_ITEM_MANIPULATION = "char_item_manipulation",
CAMERA_SHAKE = "camera_shake",
CAMERA_MOVE = "camera_move",
CAMERA_FADE = "camera_fade",
COLORIZE_MAP = "colorize_map",
COLORIZE_CHAR = "colorize_char",
TINT_CHAR = "tint_char",
OUTLINE_CHAR = "outline_char",
CASTING_AURA = "casting_aura",
CUSTOM_COLLISION_BODY = "custom_collision_body",
SET_CHAR_COLLISION = "set_char_collision",
SET_IO_COLLISION = "set_io_collision",
GRANT_ABILITY = "grant_ability",
STORAGE_CHANGE = "storage_change",
CAMERA_FOLLOW = "camera_follow",
PERMANENT_STATUS = "permanent_status",
CHANGE_COLLISION_LAYER = "change_collision_layer",
CREATE_STORAGE_VAR = "create_storage_var",
ITEM_CHECKS = "item_checks",
ADD_ITEM_TO_PARTY = "add_item_to_party",
GENERIC_SPRITE = "generic_sprite",
PARTICLES = "particles",
CHAR_HUE = "char_hue",
FLAME_CHAR = "flame_char",
CHAR_BLEND_MODE = "char_blend_mode",
EVENT_CALLER = "event_caller",
EVENT_ACTIVATION = "event_activation",
SET_CHAR_VISIBILITY = "set_char_visibility",
EVENTS_HOLDER = "events_holder",
EVENTS_LOOP = "events_loop",
LAYER_TWEEN = "layer_tween",
LAYER_VISIBILITY = "layer_visibility",
MAIN_CHARS_JOIN_SPLIT = "main_chars_join_split",
SET_IO_VISIBILITY = "set_io_visibility",
SET_IO_ACTIVATION = "set_io_activation",
TELEPORT = "teleport",
SET_NPC_COLLISION = "set_npc_collision",
CHAR_ROTATION = "char_rotation",
CHAR_TWEEN_POSITION = "char_tween_position",
CHAR_SHADOW_VISIBILITY = "char_shadow_visibility",
IO_TWEEN_POSITION = "io_tween_position",
EXIT_SAND_MODE = "exit_sand_mode",
CHAR_FALL = "char_fall",
SET_CHAR_HP_PP = "set_char_hp_pp",
AUDIO_STOP = "audio_stop",
CHAR_SPLASH_SWEAT_DROPS = "char_splash_sweat_drops",
}
export enum game_event_misc_origin {
INTERACTABLE_OBJECT_PUSH = "interactable_object_push",
INTERACTABLE_OBJECT_BEFORE_PSYNERGY = "interactable_object_before_psynergy",
INTERACTABLE_OBJECT_AFTER_PSYNERGY = "interactable_object_after_psynergy",
INTERACTABLE_OBJECT_TOGGLE = "interactable_object_toggle",
TILE_EVENT = "tile_event",
NPC = "npc",
NPC_PSYNERGY = "npc_psynergy",
MAP = "map",
MISC = "misc",
}
export const game_event_origin = {...event_types, ...game_event_misc_origin};
export type GameEventOrigin = event_types | game_event_misc_origin;
/**
* This is the class reponsible for general events of the game.
* Every game event class must inherit from this class. Whenever a game event is instantiated,
* this event receives an unique id. These ids are reset whenever a map is destroyed. In order
* to fire a GameEvent, calls GameEvent.fire. In order to destroy a game event, calls GameEvent.destroy.
* Game events can be fired from TileEvents, NPC or Interactable Objects interaction, map changes or
* other game events. For easy reference/example, check base/game_events/EmoticonEvent.ts.
* Whenever an asynchronous game event is fired, increments the GameEventManager.events_running_count, so the
* engine knows that there's an event going on. When the event is finished, decrements the same variable.
* If your event has internal states, don't forget to reset them on finish.
* When creating a new GameEvent class, add its instantiation in GameEventManager.get_event_instance factory
* method.
*/
export abstract class GameEvent {
public game: Phaser.Game;
public data: GoldenSun;
private _type: event_types;
private _id: number;
private _active: boolean;
private _key_name: string;
private _keep_reveal: boolean;
private _origin_npc: NPC;
/** If true, custom psynergies won't be stopped on this event fire. */
private keep_custom_psynergy: boolean;
/** The GameEvents id incrementer. */
public static id_incrementer: number;
/** The events object where the keys are the ids. */
public static events: {[id: number]: GameEvent};
/** The events object where the keys are the events labels. */
public static labeled_events: {[key_name: number]: GameEvent};
constructor(
game: Phaser.Game,
data: GoldenSun,
type: event_types,
active: boolean,
key_name: string,
keep_reveal: boolean,
keep_custom_psynergy: boolean
) {
this.game = game;
this.data = data;
this._type = type;
this._active = active ?? true;
this._id = GameEvent.id_incrementer++;
GameEvent.events[this.id] = this;
this._key_name = key_name;
if (this.key_name) {
GameEvent.labeled_events[this.key_name] = this;
}
this._keep_reveal = keep_reveal ?? false;
this.keep_custom_psynergy = keep_custom_psynergy ?? false;
this._origin_npc = null;
}
/** The GameEvent type. */
get type() {
return this._type;
}
/** The GameEvent id number. */
get id() {
return this._id;
}
/** Whether this GameEvent is active or not. */
get active() {
return this._active;
}
/** An unique label that identifies this GameEvent. This is optional. */
get key_name() {
return this._key_name;
}
/** If true, Reveal Psynergy won't be stopped on this event fire. */
get keep_reveal() {
return this._keep_reveal;
}
/** If this GameEvent was originated by a NPC, this var holds this NPC reference. */
get origin_npc() {
return this._origin_npc;
}
/** Check if "Reveal" is currently active. If yes, then cancel it. */
private check_reveal() {
if (this.data.hero?.on_reveal && !this.keep_reveal) {
this.data.info.field_abilities_list.reveal.finish_psynergy(false, false);
}
}
/** Check if a custom psynergy is currently active. If yes, then cancel it. */
private check_custom_psynergy() {
if (this.data?.hero.on_custom_psynergy_effect && !this.keep_custom_psynergy) {
_.forEach(this.data.info.field_abilities_list, ability => {
if (ability.is_custom_psynergy) {
ability.finish_psynergy(false, false);
}
});
}
}
/**
* This function is the one that should be called to start a event.
* It should never be overriden.
* Always before this function is called, it's checked whether Reveal psynergy
* is being casted, if yes, it's stopped before this event start.
* @param origin_npc the NPC that originated this game event.
* @returns if the child class has an async fire function, returns its Promise.
*/
fire(origin_npc?: NPC) {
if (!this.active) {
return;
}
this.check_reveal();
this.check_custom_psynergy();
if (this.data.verbose_game_event_fire) {
this.data.logger.log_message(
`Game event of type '${this.type}' fired.${this.key_name ? ` Key name: '${this.key_name}'.` : ""}`,
msg_types.INFO
);
}
this._origin_npc = origin_npc;
this._fire();
}
/**
* This abstract function is the one that GameEvent child classes should override.
* It should never be called.
*/
protected abstract _fire(): void;
/**
* This function is the one that should be called to destroy an event.
* It should never be overriden. Call this function to destroy this event.
* This can be called whenever an associated entity is destroyed, like maps, NPCs etc.
*/
destroy() {
this._destroy();
this._active = false;
this._origin_npc = null;
}
/**
* This abstract function is the one that GameEvent child classes should override.
* It should never be called.
*/
protected abstract _destroy(): void;
/**
* Actives or deactives this event.
* @param activate whether to active or not.
*/
set_event_activation(activate: boolean) {
this._active = activate;
}
/**
* A helper function that defines the ControllableChar based on inputs.
* The controllable char can be a hero or a npc.
* @param data The GoldenSun instance.
* @param options Some options to help defining the char.
* @returns the defined char.
*/
static get_char(
data: GoldenSun,
options: {
/** Whether it's a npc or not. */
is_npc?: boolean;
/** The npc index number. */
npc_index?: number;
/** The npc unique label identifier. */
npc_label?: string;
}
) {
if (options.is_npc === undefined && options.npc_index === undefined && options.npc_label === undefined) {
return null;
} else if (options.is_npc) {
if (options.npc_index === undefined && options.npc_label === undefined) {
return null;
}
if (options.npc_label) {
if (options.npc_label in data.map.npcs_label_map) {
return data.map.npcs_label_map[options.npc_label];
} else {
data.logger.log_message(`There's no NPC with '${options.npc_label}' label.`);
return null;
}
} else {
if (options.npc_index in data.map.npcs) {
return data.map.npcs[options.npc_index];
} else {
data.logger.log_message(`There's no NPC with '${options.npc_index}' index.`);
return null;
}
}
} else {
return data.hero;
}
}
/**
* Get a specific event by its id.
* @param id The event id.
* @returns Returns the event.
*/
static get_event(id) {
return GameEvent.events[id];
}
/**
* Gets an event that was labeled.
* @param key_name The event key.
* @returns Returns the labeled event, if the given key wasn't found, returns null.
*/
static get_labeled_event(key_name: string): GameEvent {
return key_name in GameEvent.labeled_events ? GameEvent.labeled_events[key_name] : null;
}
/**
* Destroys all game events and resets the id counter.
*/
static reset() {
GameEvent.id_incrementer = 0;
for (let id in GameEvent.events) {
GameEvent.events[id].destroy();
}
GameEvent.events = {};
GameEvent.labeled_events = {};
}
}
GameEvent.reset();