-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathBattle.ts
More file actions
1996 lines (1780 loc) · 81.7 KB
/
Copy pathBattle.ts
File metadata and controls
1996 lines (1780 loc) · 81.7 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
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import {
permanent_status,
temporary_status,
on_catch_status_msg,
fighter_types,
Player,
main_stats,
ailment_recovery_base_chances,
} from "../Player";
import {BattleStage} from "./BattleStage";
import {BattleLog} from "./BattleLog";
import {MainBattleMenu, PlayerAbilities, PlayerAbility} from "../main_menus/MainBattleMenu";
import {Enemy, get_enemy_instance} from "../Enemy";
import {ability_types, Ability, ability_categories} from "../Ability";
import {ChoosingTargetWindow} from "../windows/battle/ChoosingTargetWindow";
import {EnemyAI} from "./EnemyAI";
import {BattleFormulas, EVASION_CHANCE, DELUSION_MISS_CHANCE} from "./BattleFormulas";
import {effect_types, Effect, effect_usages, effect_names} from "../Effect";
import {ordered_elements, element_names, base_actions, promised_wait} from "../utils";
import {djinn_status, Djinn} from "../Djinn";
import {ItemSlot, MainChar} from "../MainChar";
import {animation_availability, BattleAnimationManager} from "./BattleAnimationManager";
import {GoldenSun} from "../GoldenSun";
import * as _ from "lodash";
import {Target} from "../battle/BattleStage";
import {Item, use_types} from "../Item";
import {battle_actions, PlayerSprite} from "./PlayerSprite";
import {Map} from "../Map";
import {GAME_WIDTH} from "../magic_numbers";
import {SpriteBase} from "../SpriteBase";
/* ACTIONS:
- Attack
- Psynergy
- Djinni Use
- Djinni Recovery
- Item
- Enemy Action
- Defend
- Total Defense (yes, this is defined differently from Defend for some reason)
- Counterattack
- Daedalus
- Retreat
*/
enum battle_phases {
NONE, // (not in a battle)
START, // Start (camera pan, shows enemies, move to menu)
MENU, // (includes submenus, this phase doesn't end until the player has entered their final command)
ROUND_START, // Start (turn order is determined, enemies may commit to certain actions)
COMBAT, // (all actions are queued and take place here, you could further break up combat actions into subactions, which should be governed by a separate sub-state variable)
ROUND_END, // End (djinn recovery, status/buff/debuff timers decrement)
FLEE,
END, // End (the last enemy has fallen, exp/gold/drops are awarded)
}
export type PlayerInfo = {
sprite_key?: string;
instance?: Enemy | MainChar;
entered_in_battle?: boolean;
battle_key?: string;
sprite?: PlayerSprite;
hue_angle?: number;
};
export type EnemyPartyMember = {
key: string;
min: number;
max: number;
hue_angle: number;
};
export class Battle {
public static readonly MAX_CHARS_IN_BATTLE = 4;
public game: Phaser.Game;
public data: GoldenSun;
public allies_info: PlayerInfo[];
public enemies_party_name: string;
public enemies_info: PlayerInfo[];
public this_enemies_list: {[battle_key: string]: Enemy};
public battle_stage: BattleStage;
public battle_log: BattleLog;
public battle_menu: MainBattleMenu;
public target_window: ChoosingTargetWindow;
public animation_manager: BattleAnimationManager;
public battle_phase: number;
public on_going_effects: Effect[];
public allies_defeated: boolean;
public enemies_defeated: boolean;
public battle_finishing: boolean;
public can_escape: boolean;
public party_fled: boolean;
public flee_attemps: {
allies: number;
enemies: number;
};
public advance_log_resolve: Function;
public advance_log_control_key: number;
public allies_abilities: PlayerAbilities;
public enemies_abilities: PlayerAbilities;
public turns_actions: PlayerAbility[];
public round_end_effects: {
action: PlayerAbility;
ability: Ability;
effect: any;
}[];
public allies_map_sprite: {[player_key: string]: PlayerSprite};
public enemies_map_sprite: {[player_key: string]: PlayerSprite};
public previous_map_state: ReturnType<Map["pause"]>;
public before_fade_finish_callback: (victory: boolean, all_party_fled: boolean) => Promise<void>;
public finish_callback: (victory: boolean, all_party_fled?: boolean) => void;
public background_key: string;
public bgm: string;
public reset_previous_bgm: boolean;
public previous_bgm: string;
constructor(
game: Phaser.Game,
data: GoldenSun,
background_key: string,
enemy_party_key: string,
bgm: string,
reset_previous_bgm: boolean,
before_fade_finish_callback?: Battle["before_fade_finish_callback"],
finish_callback?: Battle["finish_callback"]
) {
this.game = game;
this.data = data;
this.before_fade_finish_callback = before_fade_finish_callback;
this.finish_callback = finish_callback;
this.background_key = background_key;
this.bgm = bgm ?? null;
this.reset_previous_bgm = reset_previous_bgm ?? true;
this.previous_bgm = null;
this.allies_info = this.data.info.party_data.members.slice(0, Battle.MAX_CHARS_IN_BATTLE).map(char => {
char.init_effect_turns_count();
return {
sprite_key: char.sprite_base.getSpriteKey(base_actions.BATTLE),
instance: char,
entered_in_battle: true,
} as PlayerInfo;
});
const enemies_party_data = this.data.dbs.enemies_parties_db[enemy_party_key];
this.can_escape = enemies_party_data.can_escape;
this.enemies_party_name = enemies_party_data.name;
this.enemies_info = [];
this.this_enemies_list = {};
let battle_keys_count = {};
let counter = 0;
enemies_party_data.members.forEach((member_info: EnemyPartyMember) => {
const qtd = _.random(member_info.min, member_info.max);
for (let i = 0; i < qtd; ++i) {
this.enemies_info.push({
sprite_key: member_info.key + SpriteBase.ACTION_ANIM_SEPARATOR + base_actions.BATTLE,
hue_angle: member_info.hue_angle ?? 0,
});
if (this.enemies_info[counter].sprite_key in battle_keys_count) {
battle_keys_count[this.enemies_info[counter].sprite_key] += 1;
} else {
battle_keys_count[this.enemies_info[counter].sprite_key] = 1;
}
let battle_key_suffix = "",
name_suffix = "";
if (battle_keys_count[this.enemies_info[counter].sprite_key] > 1) {
battle_key_suffix =
SpriteBase.ACTION_ANIM_SEPARATOR +
battle_keys_count[this.enemies_info[counter].sprite_key].toString();
name_suffix = " " + battle_keys_count[this.enemies_info[counter].sprite_key].toString();
}
this.enemies_info[counter].instance = get_enemy_instance(
this.data,
this.data.info.enemies_list[member_info.key].data,
name_suffix
);
this.enemies_info[counter].battle_key = this.enemies_info[counter].sprite_key + battle_key_suffix;
this.this_enemies_list[this.enemies_info[counter].battle_key] = this.enemies_info[counter]
.instance as Enemy;
++counter;
}
});
this.battle_phase = battle_phases.NONE;
this.on_going_effects = [];
this.round_end_effects = [];
this.allies_defeated = false;
this.enemies_defeated = false;
this.battle_finishing = false;
this.party_fled = false;
this.flee_attemps = {
allies: 0,
enemies: 0,
};
}
start_battle() {
this.check_phases();
}
on_abilities_choose(abilities: PlayerAbilities) {
this.allies_abilities = abilities;
this.battle_menu.close_menu();
this.battle_stage.reset_positions();
this.battle_stage.choosing_actions = false;
this.battle_phase = battle_phases.ROUND_START;
this.check_phases();
}
choose_targets(ability_key: string, action: string, callback: Function, caster: Player, item_obj?: ItemSlot) {
const this_ability = this.data.info.abilities_list[ability_key];
let quantities: number[];
if (action === "psynergy") {
quantities = [this_ability.pp_cost];
}
if (action !== "defend") {
this.target_window.open(action, this_ability.name, this_ability.element, ability_key, quantities, item_obj);
}
const status_to_be_healed: (permanent_status | temporary_status)[] = [];
this_ability.effects.forEach(effect => {
if (effect.type === effect_types.TEMPORARY_STATUS || effect.type === effect_types.PERMANENT_STATUS) {
if (!effect.add_status) {
status_to_be_healed.push(effect.status_key_name);
}
}
});
this.battle_stage.cursor_manager.choose_targets(
this_ability.range,
this_ability.battle_target,
caster,
(targets: Target[]) => {
if (this.target_window.window_open) {
this.target_window.close();
}
callback(targets);
},
this_ability.affects_downed,
status_to_be_healed
);
}
check_parties() {
this.allies_defeated = this.allies_info.every(player => player.instance.is_downed());
this.enemies_defeated = this.enemies_info.every(
player => player.instance.is_downed() || (player.instance as Enemy).fled
);
if (this.allies_defeated || this.enemies_defeated) {
this.battle_phase = battle_phases.END;
}
}
check_phases() {
this.check_parties();
switch (this.battle_phase) {
case battle_phases.NONE:
this.battle_phase_none();
break;
case battle_phases.START:
case battle_phases.MENU:
this.battle_phase_menu();
break;
case battle_phases.ROUND_START:
this.battle_phase_round_start();
break;
case battle_phases.COMBAT:
this.battle_phase_combat();
break;
case battle_phases.ROUND_END:
this.battle_phase_round_end();
break;
case battle_phases.FLEE:
this.battle_phase_flee();
break;
case battle_phases.END:
this.battle_phase_end();
break;
}
}
initialize_battle_objs() {
this.battle_stage = new BattleStage(
this.game,
this.data,
this.background_key,
this.allies_info,
this.enemies_info
);
this.battle_log = new BattleLog(this.game);
this.battle_menu = new MainBattleMenu(this.game, this.data, this);
this.target_window = new ChoosingTargetWindow(this.game, this.data);
this.animation_manager = new BattleAnimationManager(this.game, this.data);
}
async battle_fadein() {
this.data.audio.play_se("monster_defeat/monster_2");
const graphic = this.game.add.graphics(this.data.hero.sprite.x, this.data.hero.sprite.y);
graphic.clear();
graphic.beginFill(0xfffffff);
const circle = graphic.drawCircle(0, 0, 3 * (GAME_WIDTH >> 1));
circle.scale.setTo(0, 0);
let resolve_promise;
const promise = new Promise(resolve => (resolve_promise = resolve));
const tween = this.game.add.tween(circle.scale).to({x: 1, y: 1}, 400, Phaser.Easing.Linear.None, true);
tween.onUpdateCallback(() => {
circle.visible = !circle.visible;
});
tween.onComplete.addOnce(() => {
circle.visible = true;
const color_obj = {
r: 0xff,
g: 0xff,
b: 0xff,
};
const color_tween = this.game.add.tween(color_obj).to(
{
r: 0x0,
g: 0x0,
b: 0x0,
},
300,
Phaser.Easing.Linear.None,
true
);
color_tween.onUpdateCallback(() => {
circle.tint = (color_obj.r << 16) + (color_obj.g << 8) + color_obj.b;
});
color_tween.onComplete.addOnce(() => {
this.data.game.camera.fade(0x0, 0, true);
this.data.game.camera.fx.alpha = 1;
circle.destroy();
graphic.destroy();
resolve_promise();
});
});
await promise;
}
can_flee(ally_attempt: boolean) {
const front_pt_lvl =
_.mean(
this.data.info.party_data.members.slice(0, Battle.MAX_CHARS_IN_BATTLE).flatMap(char => {
return char.is_downed() ? [] : [char.level];
})
) | 0;
const enemies_lvl =
_.mean(
this.enemies_info.flatMap(info => {
return info.instance.is_downed() ? [] : [info.instance.level];
})
) | 0;
const diff = ally_attempt ? front_pt_lvl - enemies_lvl : enemies_lvl - front_pt_lvl;
const rate = 500 * (1 + diff + 4 * (ally_attempt ? this.flee_attemps.allies : this.flee_attemps.enemies));
if (_.random(9999) < rate) {
return true;
} else {
if (ally_attempt) {
++this.flee_attemps.allies;
} else {
++this.flee_attemps.enemies;
}
return false;
}
}
async flee(ally_attempt: boolean, enemy_battle_key?: string) {
const flee_succeed = this.can_flee(ally_attempt);
if (ally_attempt) {
this.battle_menu.close_menu();
this.battle_stage.reset_positions();
this.battle_stage.update_stage();
this.battle_stage.choosing_actions = false;
await this.battle_log.add(`${this.data.info.party_data.members[0].name} and friends run!`);
} else {
const enemy_name = this.this_enemies_list[enemy_battle_key].name;
await this.battle_log.add(`${enemy_name} runs!`);
}
await this.wait_for_key();
if (flee_succeed) {
this.battle_stage.pause_players_update = true;
const animation_recipe = this.data.info.misc_battle_animations_recipes["flee"];
const flee_animation = BattleAnimationManager.get_animation_instance(
this.game,
this.data,
animation_recipe,
false
);
const caster_sprite = this.allies_map_sprite[this.data.info.party_data.members[0].key_name];
let target_sprites: PlayerSprite[];
if (ally_attempt) {
this.battle_phase = battle_phases.FLEE;
target_sprites = this.data.info.party_data.members
.filter(member => {
return !member.is_downed();
})
.map(member => this.allies_map_sprite[member.key_name]);
} else {
this.this_enemies_list[enemy_battle_key].fled = true;
target_sprites = [this.enemies_map_sprite[enemy_battle_key]];
}
await this.animation_manager.play_animation(
flee_animation,
caster_sprite,
target_sprites,
[],
this.battle_stage.group_allies,
this.battle_stage.group_allies,
this.battle_stage
);
this.battle_stage.pause_players_update = false;
if (!ally_attempt) {
this.enemies_map_sprite[enemy_battle_key].visible = false;
}
} else {
await this.battle_log.add("But there's no escape!");
await this.wait_for_key();
if (ally_attempt) {
this.allies_abilities = {};
this.battle_phase = battle_phases.ROUND_START;
}
}
this.check_phases();
}
async setup_battle_sound() {
this.data.audio.stop_bgm();
if (!this.bgm) {
return;
}
this.previous_bgm = this.data.audio.current_bgm.key;
this.data.audio.set_bgm(this.bgm, false);
this.data.audio.play_battle_bgm();
}
async battle_phase_none() {
this.data.hero.stop_char(true);
this.game.physics.p2.pause();
await this.setup_battle_sound();
await this.battle_fadein();
this.initialize_battle_objs();
this.previous_map_state = this.data.map.pause();
this.battle_phase = battle_phases.START;
this.data.in_battle = true;
this.data.battle_instance = this;
this.advance_log_control_key = this.data.control_manager.add_simple_controls(
() => {
if (this.advance_log_resolve) {
this.advance_log_resolve();
this.advance_log_resolve = null;
}
},
{persist: true}
);
this.battle_log.add(this.enemies_party_name + " appeared!");
this.battle_stage.initialize_stage(() => {
this.allies_map_sprite = _.mapValues(
_.keyBy(this.allies_info, "instance.key_name"),
(info: PlayerInfo) => info.sprite
);
this.enemies_map_sprite = _.mapValues(
_.keyBy(this.enemies_info, "battle_key"),
(info: PlayerInfo) => info.sprite
);
this.data.control_manager.add_simple_controls(() => {
this.battle_log.clear();
this.battle_phase = battle_phases.MENU;
this.check_phases();
});
});
}
battle_phase_menu() {
this.battle_stage.set_choosing_action_position();
this.battle_menu.open_menu();
}
/*
At round start, is calculated the players and enemies speeds.
If a certain player speed is the same of a enemy, player goes first.
If another tie, the most left char has priority.
At a specific enemy turn start, I roll an action for that turn.
The only thing needed to check about enemies actions at round start is:
- Roll their actions for each turn and see if an ability with priority move is rolled.
- If yes, this ability is fixed for that corresponding turn.
For the other turns, an action is re-roll in the turn start to be used on it.
*/
async battle_phase_round_start() {
const enemy_members = this.enemies_info.map(info => {
return {
instance: info.instance as Enemy,
battle_key: info.battle_key,
};
});
this.enemies_abilities = Object.fromEntries(
enemy_members.map((enemy, index) => {
let abilities = new Array(enemy.instance.turns);
for (let i = 0; i < enemy.instance.turns; ++i) {
abilities[i] = EnemyAI.roll_action(
this.data,
{
instance: enemy.instance as Enemy,
battle_key: enemy.battle_key,
},
enemy_members,
this.data.info.party_data.members.map(member => ({
instance: member,
battle_key: member.key_name,
}))
);
}
return [this.enemies_info[index].battle_key, abilities];
})
);
for (let char_key in this.allies_abilities) {
const this_char = this.data.info.main_char_list[char_key];
for (let i = 0; i < this.allies_abilities[char_key].length; ++i) {
const this_ability = this.data.info.abilities_list[this.allies_abilities[char_key][i].key_name];
const priority_move = this_ability !== undefined ? this_ability.priority_move : false;
this.allies_abilities[char_key][i].speed = BattleFormulas.player_turn_speed(
this_char.agi,
priority_move,
i > 0
);
this.allies_abilities[char_key][i].caster = this_char;
this.allies_abilities[char_key][i].caster_battle_key = char_key;
}
}
for (let battle_key in this.enemies_abilities) {
const this_enemy = this.this_enemies_list[battle_key];
for (let i = 0; i < this.enemies_abilities[battle_key].length; ++i) {
const this_ability = this.data.info.abilities_list[this.enemies_abilities[battle_key][i].key_name];
const priority_move = this_ability !== undefined ? this_ability.priority_move : false;
this.enemies_abilities[battle_key][i].speed = BattleFormulas.enemy_turn_speed(
this_enemy.agi,
i + 1,
this_enemy.turns,
priority_move
);
this.enemies_abilities[battle_key][i].caster = this_enemy;
this.enemies_abilities[battle_key][i].caster_battle_key = battle_key;
}
}
//sort actions by players speed
this.turns_actions = _.sortBy(
Object.values(this.allies_abilities).flat().concat(Object.values(this.enemies_abilities).flat()),
action => {
return action.speed; //still need to add left most and player preference criterias
}
);
for (let i = 0; i < this.turns_actions.length; ++i) {
const action = this.turns_actions[i];
const ability = this.data.info.abilities_list[action.key_name];
await this.set_action_animation_settings(action, ability);
}
await promised_wait(this.game, 500);
this.battle_phase = battle_phases.COMBAT;
this.check_phases();
}
async set_action_animation_settings(action: PlayerAbility, ability: Ability) {
if (action.caster.is_paralyzed(true)) {
return;
}
//check whether the player of this action has a variation for this battle animation
let battle_animation_key = this.data.info.abilities_list[action.key_name].battle_animation_key;
if (ability.has_animation_variation && action.key_name in action.caster.battle_animations_variations) {
battle_animation_key = action.caster.battle_animations_variations[action.key_name];
}
action.battle_animation_key = battle_animation_key;
const mirrored_animation = ability.can_be_mirrored && action.caster.fighter_type === fighter_types.ENEMY;
//loads battle animation assets for this ability
const battle_animation = await this.animation_manager.load_animation(
battle_animation_key,
action.caster_battle_key,
mirrored_animation
);
action.cast_animation_type = battle_animation?.cast_type;
}
wait_for_key() {
return new Promise<void>(resolve => {
this.advance_log_resolve = resolve;
});
}
async check_downed(target: Enemy | MainChar) {
if (target.current_hp === 0) {
await this.down_a_char(target);
await this.battle_log.add(on_catch_status_msg[permanent_status.DOWNED](target));
await this.wait_for_key();
}
}
async down_a_char(target: Enemy | MainChar) {
this.on_going_effects = this.on_going_effects.filter(effect => {
if (effect.char === target) {
target.remove_effect(effect);
target.update_all();
return false;
}
return true;
});
target.add_permanent_status(permanent_status.DOWNED);
const player_sprite = _.find(this.battle_stage.sprites, {player_instance: target});
player_sprite.set_action(battle_actions.DOWNED);
if (target.fighter_type === fighter_types.ENEMY) {
if ((target as Enemy).defeat_voice) {
this.data.audio.play_se((target as Enemy).defeat_voice);
}
await player_sprite.unmount_by_dissolving();
}
}
/*
Standard attack:
1. Unleash check (followed by another check for unleash type if weapon has multiple unleashes)
2. Miss check
3. Crit check 1 (using brn % 32)
4. Crit check 2 (using total crit chance from equipment, ((equipment_chance/2)*rand(0,65535) >> 16)
5. Status effect check
6. Added 0-3 damage
If any of checks 1-4 succeed, it skips to 5
*/
async battle_phase_combat() {
if (!this.turns_actions.length) {
this.battle_phase = battle_phases.ROUND_END;
this.check_phases();
return;
}
const action = this.turns_actions.pop();
//check if enemy fled
if (action.caster.fighter_type === fighter_types.ENEMY && (action.caster as Enemy).fled) {
this.check_phases();
return;
}
//check whether this char is downed
if (action.caster.is_downed()) {
this.check_phases();
return;
}
//check whether this char is paralyzed
if (await this.check_if_char_is_paralyzed(action)) {
await this.wait_for_key();
//check for poison damage
await this.check_poison_damage(action);
this.check_phases();
return;
}
//check whether the char will skip this turn due to cursed item
if (await this.check_if_curse_will_take_effect(action)) {
await this.wait_for_key();
//check for poison damage
await this.check_poison_damage(action);
this.check_phases();
return;
}
//gets the ability of this phase
let ability = await this.get_phase_ability(action);
if (!ability) {
await this.wait_for_key();
//check for poison damage
await this.check_poison_damage(action);
this.check_phases();
return;
}
if (ability.key_name === "flee" && action.caster.fighter_type === fighter_types.ENEMY) {
await this.flee(false, action.caster_battle_key);
//check for poison damage
await this.check_poison_damage(action);
return;
}
//check whether all targets are downed and change ability to "defend" in the case it's true
if (!ability.affects_downed) {
if (this.check_if_all_targets_are_downed(action)) {
ability = this.data.info.abilities_list["defend"];
}
}
//gets item's name in the case this ability is related to an item
let item_name = action.item_slot ? this.data.info.items_list[action.item_slot.key_name].name : "";
//change the current ability to custom animation or unleash ability if necessary
const unleash_info = await this.check_weapon_ability(action, ability, item_name);
ability = unleash_info.ability;
item_name = unleash_info.item_name;
//check whether this ability exists
if (ability === undefined) {
await this.battle_log.add(`${action.key_name} ability key not registered.`);
await this.wait_for_key();
//check for poison damage
await this.check_poison_damage(action);
this.check_phases();
return;
}
//logs ability to be casted
const djinn_name = action.djinn_key_name ? this.data.info.djinni_list[action.djinn_key_name].name : undefined;
await this.battle_log.add_ability(
action.caster,
ability,
item_name,
djinn_name,
action.item_slot !== undefined
);
//check if is possible to cast ability due to seal
if (
action.caster.has_temporary_status(temporary_status.SEAL) &&
ability.ability_category === ability_categories.PSYNERGY
) {
await this.battle_log.add(`But the Psynergy was blocked!`);
await this.wait_for_key();
//check for poison damage
await this.check_poison_damage(action);
this.check_phases();
return;
}
//check if char has enough pp to cast ability
if (ability.pp_cost > action.caster.current_pp) {
await this.battle_log.add(`... But doesn't have enough PP!`);
await this.wait_for_key();
//check for poison damage
await this.check_poison_damage(action);
this.check_phases();
return;
} else {
action.caster.current_pp -= ability.pp_cost;
}
//deals with abilities related to djinn and summons
if (await this.manage_djinn_or_summon_ability(action, ability)) {
await this.wait_for_key();
//check for poison damage
await this.check_poison_damage(action);
this.check_phases();
return;
}
//check if item is broken
if (action.item_slot) {
if (action.item_slot.broken) {
await this.battle_log.add(`But ${item_name} is broken...`);
await this.wait_for_key();
//check for poison damage
await this.check_poison_damage(action);
this.check_phases();
return;
}
}
//updates chars status window
this.battle_menu.chars_status_window.update_chars_info();
if (ability.type === ability_types.UTILITY) {
await this.wait_for_key();
}
//checks if target will dodge attack and plays dodge animation
this.check_for_attack_dodge(action, ability);
//executes the animation of the current ability
await this.play_battle_animation(action, ability);
//apply ability damage
if (![ability_types.UTILITY, ability_types.EFFECT_ONLY].includes(ability.type)) {
await this.apply_damage(action, ability);
}
//check whether a party is defeated
this.check_parties();
if (this.battle_phase === battle_phases.END) {
this.check_phases();
return;
}
//apply ability effects
let end_turn_effect = false;
for (let i = 0; i < ability.effects.length; ++i) {
const effect = ability.effects[i];
if (effect.usage === effect_usages.ON_USE) {
end_turn_effect = await this.apply_effects(action, ability, effect);
} else if (effect.usage === effect_usages.BATTLE_ROUND_END) {
this.round_end_effects.push({
action: action,
ability: ability,
effect: effect,
});
}
}
//check whether a party is defeated
this.check_parties();
if (this.battle_phase === battle_phases.END) {
this.check_phases();
return;
}
//resets stage and chars position to default
this.battle_stage.pause_players_update = false;
this.battle_stage.set_update_factor(1);
await Promise.all([this.battle_stage.reset_chars_position(), this.battle_stage.set_stage_default_position()]);
//summon's power buff after cast
await this.apply_summon_power_buff(action, ability);
//some checks related to items
await this.apply_item_ability_side_effects(action);
//check for poison damage
await this.check_poison_damage(action);
//check for death curse end
await this.check_death_curse(action);
if (end_turn_effect) {
this.battle_phase = battle_phases.ROUND_END;
}
this.check_phases();
}
check_for_attack_dodge(action: PlayerAbility, ability: Ability) {
for (let i = 0; i < action.targets.length; ++i) {
const target_info = action.targets[i];
if (target_info.magnitude === null) {
continue;
}
const target_instance = target_info.target.instance;
if (target_instance.is_downed()) {
continue;
}
if (target_instance.fighter_type === fighter_types.ENEMY && (target_instance as Enemy).fled) {
continue;
}
if (ability.can_be_evaded) {
//check whether the target is going to evade the caster attack
if (
Math.random() < EVASION_CHANCE ||
(action.caster.temporary_status.has(temporary_status.DELUSION) &&
Math.random() < DELUSION_MISS_CHANCE)
) {
target_info.dodged = true;
const target_sprites =
action.caster.fighter_type === fighter_types.ALLY
? this.enemies_map_sprite
: this.allies_map_sprite;
const target_sprite = target_sprites[target_info.target.battle_key];
const animation_recipe = this.data.info.misc_battle_animations_recipes["dodge"];
const dodge_animation = BattleAnimationManager.get_animation_instance(
this.game,
this.data,
animation_recipe,
false
);
const caster_sprite = target_sprite;
//should not wait for this anim
this.animation_manager.play_animation(
dodge_animation,
caster_sprite,
[],
[],
this.battle_stage.group_allies,
this.battle_stage.group_allies,
this.battle_stage,
undefined
);
}
}
}
}
async play_battle_animation(action: PlayerAbility, ability: Ability) {
const anim_availability = this.animation_manager.animation_available(
action.battle_animation_key,
action.caster_battle_key
);
if (anim_availability === animation_availability.AVAILABLE) {
const caster_targets_sprites = {
caster:
action.caster.fighter_type === fighter_types.ALLY
? this.allies_map_sprite
: this.enemies_map_sprite,
targets:
action.caster.fighter_type === fighter_types.ALLY
? this.enemies_map_sprite
: this.allies_map_sprite,
allies:
action.caster.fighter_type === fighter_types.ALLY
? this.allies_map_sprite
: this.enemies_map_sprite,
};
const caster_sprite = caster_targets_sprites.caster[action.caster_battle_key];
const target_sprites = action.targets.flatMap(info => {
return info.magnitude ? [caster_targets_sprites.targets[info.target.battle_key]] : [];
});
const allies_sprites = Object.values(caster_targets_sprites.allies).filter(
ally => ally.battle_key !== action.caster_battle_key
);
const group_caster =
action.caster.fighter_type === fighter_types.ALLY
? this.battle_stage.group_allies
: this.battle_stage.group_enemies;
const group_taker =
action.caster.fighter_type === fighter_types.ALLY
? this.battle_stage.group_enemies
: this.battle_stage.group_allies;
this.battle_stage.pause_players_update = true;
if (action.cast_animation_type && action.caster_battle_key) {
const animation_recipe = this.data.info.abilities_cast_recipes[action.cast_animation_type];
const cast_animation = BattleAnimationManager.get_animation_instance(
this.game,
this.data,
animation_recipe,
action.caster.fighter_type !== fighter_types.ALLY,
ability.element
);
const main_animation = this.animation_manager.get_animation(
action.battle_animation_key,
action.caster_battle_key
);
const cast_promise = this.animation_manager.play_animation(
cast_animation,
caster_sprite,
target_sprites,
allies_sprites,
group_caster,
group_taker,
this.battle_stage
);
if (main_animation.wait_for_cast_animation) {
await cast_promise;
}