/
BattleStage.ts
646 lines (543 loc) · 21.5 KB
/
BattleStage.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
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
import * as numbers from "../magic_numbers";
import {range_360} from "../utils";
import {GoldenSun} from "../GoldenSun";
import {Button} from "../XGamepad";
import {PlayerInfo} from "./Battle";
import {battle_actions, battle_positions, PlayerSprite} from "./PlayerSprite";
import {BattleAnimation} from "./BattleAnimation";
import {BattleCursorManager} from "./BattleCursorManager";
const SCALE_FACTOR = 0.8334;
const BG_X = 0;
export const BG_Y = 17;
const BG_WIDTH = 256;
export const BG_HEIGHT = 120;
const CENTER_X = numbers.GAME_WIDTH >> 1;
const CENTER_Y = numbers.GAME_HEIGHT - 35;
const CAMERA_SPEED = 0.009 * Math.PI;
const BG_SPEED = 2.4;
const BG_SPIN_SPEED = 0.4;
const SPACE_BETWEEN_CHARS = 35;
export const SEMI_MAJOR_AXIS = numbers.GAME_WIDTH / 2 - 50;
export const SEMI_MINOR_AXIS = numbers.GAME_HEIGHT / 50;
export const DEFAULT_POS_ANGLE = 0.7551327;
const INITIAL_POS_ANGLE = -2.120575;
const INITIAL_SCALE = 1.2;
const BG_DEFAULT_SCALE = 1.0;
const ACTION_POS_BG_SCALE = 2;
const ACTION_POS_ALLY_X = 88;
const ACTION_POS_ENEMY_CENTER_X = 106;
const ACTION_ALLY_Y = 160;
const ACTION_ENEMY_Y = 98;
const ACTION_POS_SPACE_BETWEEN = 40;
const ACTION_POS_SCALE_ADD = 0.2;
export const CHOOSE_TARGET_ENEMY_SHIFT = 15;
export const CHOOSE_TARGET_ALLY_SHIFT = -3;
const INIT_TIME = 1500;
export type CameraAngle = {
rad: number;
update: Function;
};
export type Target = {
magnitude: number;
target: PlayerInfo;
};
export class BattleStage {
private game: Phaser.Game;
private data: GoldenSun;
private _cursor_manager: BattleCursorManager;
private _camera_angle: CameraAngle;
private background_key: string;
private old_camera_angle: number;
private _battle_group: Phaser.Group;
private crop_group: Phaser.Group;
private _group_enemies: Phaser.Group;
private _group_allies: Phaser.Group;
private _allies_info: PlayerInfo[];
private _enemies_info: PlayerInfo[];
private _allies_count: number;
private _enemies_count: number;
private shift_from_middle_enemy: number;
private shift_from_middle_ally: number;
private _sprites: PlayerSprite[];
private x: number;
private y: number;
public choosing_actions: boolean;
public pause_update: boolean;
public pause_players_update: boolean;
private black_bg: Phaser.Graphics;
private _battle_bg: Phaser.TileSprite;
private _battle_bg2: Phaser.TileSprite;
private upper_rect: Phaser.Graphics;
private lower_rect: Phaser.Graphics;
private first_ally_char: Phaser.Sprite;
private last_ally_char: Phaser.Sprite;
private first_enemy_char: Phaser.Sprite;
private last_enemy_char: Phaser.Sprite;
private bg_height: number;
private pos_update_factor: {
factor: number;
};
constructor(
game: Phaser.Game,
data: GoldenSun,
background_key: string,
allies_info: PlayerInfo[],
enemies_info: PlayerInfo[]
) {
this.game = game;
this.data = data;
this._cursor_manager = new BattleCursorManager(this.game, this.data, this);
this._camera_angle = {
rad: INITIAL_POS_ANGLE,
update: this.update_sprite_properties.bind(this),
};
this.background_key = background_key;
this.old_camera_angle = this.camera_angle.rad;
this._battle_group = this.game.add.group();
this.crop_group = this.game.add.group();
this._group_enemies = this.game.add.group();
this._group_allies = this.game.add.group();
this._allies_info = allies_info;
this._enemies_info = enemies_info;
this._allies_count = allies_info.length;
this._enemies_count = enemies_info.length;
this.shift_from_middle_enemy = SPACE_BETWEEN_CHARS * this.enemies_count * 0.5;
this.shift_from_middle_ally = SPACE_BETWEEN_CHARS * this.allies_count * 0.5;
this._sprites = [];
this.x = this.game.camera.x;
this.y = this.game.camera.y;
this.battle_group.x = this.x;
this.battle_group.y = this.y;
this.battle_group.scale.setTo(INITIAL_SCALE, INITIAL_SCALE);
this.crop_group.x = this.x;
this.crop_group.y = this.y;
this.pause_update = false;
this.pause_players_update = false;
this.pos_update_factor = {factor: 0};
}
get cursor_manager() {
return this._cursor_manager;
}
get sprites() {
return this._sprites;
}
get group_allies() {
return this._group_allies;
}
get group_enemies() {
return this._group_enemies;
}
get allies_info() {
return this._allies_info;
}
get enemies_info() {
return this._enemies_info;
}
get allies_count() {
return this._allies_count;
}
get enemies_count() {
return this._enemies_count;
}
get battle_group() {
return this._battle_group;
}
get camera_angle() {
return this._camera_angle;
}
get battle_bg() {
return this._battle_bg;
}
get battle_bg2() {
return this._battle_bg2;
}
initialize_sprites() {
this.black_bg = this.game.add.graphics(0, 0);
this.battle_group.add(this.black_bg);
this.black_bg.beginFill(0x0, 1);
this.black_bg.drawRect(0, 0, numbers.GAME_WIDTH, numbers.GAME_HEIGHT);
this.black_bg.endFill();
this._battle_bg = this.game.add.tileSprite(
BG_X,
BG_Y,
BG_WIDTH,
BG_HEIGHT,
"battle_backgrounds",
this.background_key
);
this._battle_bg2 = this.game.add.tileSprite(
BG_X,
BG_Y,
BG_WIDTH,
BG_HEIGHT,
"battle_backgrounds",
this.background_key
);
const bg_filter = this.game.add.filter("ColorFilters");
this.battle_bg.filters = [bg_filter];
this.battle_bg2.filters = [bg_filter];
this.bg_height = this.battle_bg.height;
this.battle_bg.scale.setTo(BG_DEFAULT_SCALE, BG_DEFAULT_SCALE);
this.battle_bg2.scale.setTo(BG_DEFAULT_SCALE, BG_DEFAULT_SCALE);
this.allies_info.forEach(info => {
const sprite_base = this.data.info.main_char_list[info.instance.key_name].sprite_base;
const player_sprite = new PlayerSprite(
this.game,
this.data,
this.group_allies,
info,
sprite_base,
true,
battle_actions.IDLE,
battle_positions.BACK
);
player_sprite.initialize_player();
info.sprite = player_sprite;
this.sprites.push(player_sprite);
});
this.enemies_info.forEach(info => {
const sprite_base = this.data.info.enemies_list[info.instance.key_name].sprite_base;
const player_sprite = new PlayerSprite(
this.game,
this.data,
this.group_enemies,
info,
sprite_base,
false,
battle_actions.IDLE,
battle_positions.FRONT
);
player_sprite.initialize_player();
info.sprite = player_sprite;
this.sprites.push(player_sprite);
});
this.first_ally_char = this.group_allies.children[0] as Phaser.Sprite;
this.last_ally_char = this.group_allies.children[this.allies_count - 1] as Phaser.Sprite;
this.first_enemy_char = this.group_enemies.children[0] as Phaser.Sprite;
this.last_enemy_char = this.group_enemies.children[this.enemies_count - 1] as Phaser.Sprite;
}
intialize_crop_rectangles() {
const upper_x = 0;
const upper_y = 0;
this.upper_rect = this.game.add.graphics(upper_x, upper_y);
this.crop_group.add(this.upper_rect);
this.upper_rect.beginFill(0x0, 1);
this.upper_rect.drawRect(0, 0, numbers.GAME_WIDTH, numbers.GAME_HEIGHT >> 1);
this.upper_rect.endFill();
const lower_x = 0;
const lower_y = BG_Y + (this.bg_height >> 1) + 2;
this.lower_rect = this.game.add.graphics(lower_x, lower_y);
this.crop_group.add(this.lower_rect);
this.lower_rect.beginFill(0x0, 1);
this.lower_rect.drawRect(0, 0, numbers.GAME_WIDTH, (numbers.GAME_HEIGHT >> 1) + 2);
this.lower_rect.endFill();
}
initialize_stage(callback) {
this.choosing_actions = false;
this.initialize_sprites();
this.intialize_crop_rectangles();
this.data.game.camera.resetFX();
this.battle_group.add(this.battle_bg);
this.battle_group.add(this.battle_bg2);
this.battle_group.add(this.group_enemies);
this.battle_group.add(this.group_allies);
this.game.add.tween(this.upper_rect).to(
{
height: BG_Y,
},
INIT_TIME,
Phaser.Easing.Linear.None,
true
);
this.game.add.tween(this.lower_rect).to(
{
y: BG_Y + this.bg_height - 1,
height: numbers.GAME_HEIGHT - this.bg_height - BG_Y + 1,
},
INIT_TIME,
Phaser.Easing.Linear.None,
true
);
this.game.add
.tween(this.camera_angle)
.to(
{
rad: DEFAULT_POS_ANGLE,
},
INIT_TIME,
Phaser.Easing.Linear.None,
true
)
.onComplete.addOnce(() => {
if (callback) {
callback();
}
});
this.game.add.tween(this.battle_group.scale).to(
{
x: 1,
y: 1,
},
INIT_TIME,
Phaser.Easing.Linear.None,
true
);
}
set_update_factor(factor: number) {
this.pos_update_factor.factor = factor;
}
async reset_chars_position() {
for (let i = 0; i < this.sprites.length; ++i) {
const player = this.sprites[i];
if (player.player_instance.is_paralyzed(true, true)) {
player.set_action(battle_actions.DOWNED);
} else {
player.set_action(battle_actions.IDLE);
}
player.ellipses_semi_major = SEMI_MAJOR_AXIS;
player.ellipses_semi_minor = SEMI_MINOR_AXIS;
}
let promise_resolve;
const promise = new Promise(resolve => (promise_resolve = resolve));
this.game.add
.tween(this.pos_update_factor)
.to(
{
factor: 0,
},
300,
Phaser.Easing.Quadratic.Out,
true
)
.onComplete.addOnce(promise_resolve);
await promise;
}
async set_stage_default_position() {
let promise_resolve: Function;
const promise = new Promise(resolve => {
promise_resolve = resolve;
});
this.camera_angle.rad = range_360(this.camera_angle.rad);
const dest_angle = BattleAnimation.get_angle_by_direction(
this.camera_angle.rad,
DEFAULT_POS_ANGLE,
"closest",
true
);
this.game.add
.tween(this.camera_angle)
.to(
{
rad: dest_angle,
},
500,
Phaser.Easing.Quadratic.Out,
true
)
.onComplete.addOnce(promise_resolve);
await promise;
}
private get_player_position_in_stage(sprite_index: number, target_angle?: number) {
const player_sprite = this.sprites[sprite_index];
target_angle = target_angle ?? this.camera_angle.rad;
const relative_angle = player_sprite.is_ally ? target_angle : target_angle + Math.PI;
player_sprite.stage_angle = BattleStage.get_angle(relative_angle);
const ellipse_pos_x = BattleStage.ellipse_position(player_sprite, player_sprite.stage_angle, true);
const ellipse_pos_y = BattleStage.ellipse_position(player_sprite, player_sprite.stage_angle, false);
const shift_from_middle = player_sprite.is_ally ? this.shift_from_middle_ally : this.shift_from_middle_enemy;
const index_shifted = player_sprite.is_ally ? sprite_index : sprite_index - this.allies_count;
const pos_x =
ellipse_pos_x +
(SPACE_BETWEEN_CHARS * index_shifted - shift_from_middle + (SPACE_BETWEEN_CHARS >> 1)) *
Math.sin(relative_angle); //shift party players from base point
const pos_y = ellipse_pos_y;
return {x: pos_x, y: pos_y};
}
set_choosing_action_position() {
this.choosing_actions = true;
this.battle_bg2.x = 0;
this.battle_bg2.scale.setTo(ACTION_POS_BG_SCALE, ACTION_POS_BG_SCALE);
this.battle_bg2.y = -this.battle_bg.height * (ACTION_POS_BG_SCALE - 1) + BG_Y - CHOOSE_TARGET_ALLY_SHIFT;
for (let i = 0; i < this.sprites.length; ++i) {
const player_prite = this.sprites[i];
const index_shifted = player_prite.is_ally ? i : this.enemies_count - 1 - (i - this.allies_count);
const x_shift = player_prite.is_ally
? ACTION_POS_ALLY_X
: ACTION_POS_ENEMY_CENTER_X - (this.enemies_count >> 1) * ACTION_POS_SPACE_BETWEEN;
const pos_x = x_shift + index_shifted * ACTION_POS_SPACE_BETWEEN;
const pos_y = player_prite.is_ally ? ACTION_ALLY_Y : ACTION_ENEMY_Y;
player_prite.x = pos_x;
player_prite.y = pos_y;
const this_scale_x = player_prite.scale.x + Math.sign(player_prite.scale.x) * ACTION_POS_SCALE_ADD;
const this_scale_y = player_prite.scale.y + Math.sign(player_prite.scale.y) * ACTION_POS_SCALE_ADD;
player_prite.scale.setTo(this_scale_x, this_scale_y);
}
}
reset_positions() {
this.battle_bg2.scale.setTo(BG_DEFAULT_SCALE, BG_DEFAULT_SCALE);
this.battle_bg2.y = BG_Y;
for (let i = 0; i < this.sprites.length; ++i) {
const sprite = this.sprites[i];
const this_scale_x = sprite.scale.x - Math.sign(sprite.scale.x) * ACTION_POS_SCALE_ADD;
const this_scale_y = sprite.scale.y - Math.sign(sprite.scale.y) * ACTION_POS_SCALE_ADD;
sprite.scale.setTo(this_scale_x, this_scale_y);
}
}
prevent_camera_angle_overflow() {
this.camera_angle.rad = range_360(this.camera_angle.rad);
}
update_stage() {
if (this.choosing_actions || this.pause_update) return;
this.update_stage_rotation();
this.update_sprite_properties();
}
update_stage_rotation() {
if (this.data.gamepad.is_down(Button.DEBUG_CAM_MINUS) && !this.data.gamepad.is_down(Button.DEBUG_CAM_PLUS)) {
this.camera_angle.rad -= CAMERA_SPEED;
this.battle_bg.x -= BG_SPEED;
} else if (
this.data.gamepad.is_down(Button.DEBUG_CAM_PLUS) &&
!this.data.gamepad.is_down(Button.DEBUG_CAM_MINUS)
) {
this.camera_angle.rad += CAMERA_SPEED;
this.battle_bg.x += BG_SPEED;
} else {
const delta = range_360(this.camera_angle.rad) - range_360(this.old_camera_angle);
this.battle_bg.x += BG_SPIN_SPEED * this.battle_bg.width * delta; //tie bg x position with camera angle when spining
}
if (this.battle_bg.x > this.battle_bg.width || this.battle_bg.x < -this.battle_bg.width) {
//check bg x position surplus
this.battle_bg.x = this.battle_bg2.x;
}
if (this.battle_bg.x > 0) {
//make bg2 follow default bg
this.battle_bg2.x = this.battle_bg.x - this.battle_bg.width;
} else if (this.battle_bg.x < 0) {
this.battle_bg2.x = this.battle_bg.x + this.battle_bg.width;
}
if (this.old_camera_angle === this.camera_angle.rad) return;
this.old_camera_angle = this.camera_angle.rad;
if (
Math.sin(this.camera_angle.rad) > 0 &&
this.battle_group.getChildIndex(this.group_allies) < this.battle_group.getChildIndex(this.group_enemies)
) {
//check party and enemy z index
this.battle_group.swapChildren(this.group_enemies, this.group_allies);
} else if (
Math.sin(this.camera_angle.rad) < 0 &&
this.battle_group.getChildIndex(this.group_allies) > this.battle_group.getChildIndex(this.group_enemies)
) {
this.battle_group.swapChildren(this.group_enemies, this.group_allies);
}
if (Math.cos(this.camera_angle.rad) < 0 && this.first_ally_char.z > this.last_ally_char.z) {
//check ally z index order
this.group_allies.reverse();
} else if (Math.cos(this.camera_angle.rad) > 0 && this.first_ally_char.z < this.last_ally_char.z) {
this.group_allies.reverse();
}
if (Math.cos(this.camera_angle.rad) < 0 && this.first_enemy_char.z < this.last_enemy_char.z) {
//check enemy z index order
this.group_enemies.reverse();
} else if (Math.cos(this.camera_angle.rad) > 0 && this.first_enemy_char.z > this.last_enemy_char.z) {
this.group_enemies.reverse();
}
}
update_sprite_properties() {
for (let i = 0; i < this.sprites.length; ++i) {
const player_sprite = this.sprites[i];
if (this.pause_players_update && !player_sprite.force_stage_update) continue;
const pos = this.get_player_position_in_stage(i);
player_sprite.x =
this.pos_update_factor.factor * player_sprite.x + (1 - this.pos_update_factor.factor) * pos.x;
player_sprite.y =
this.pos_update_factor.factor * player_sprite.y + (1 - this.pos_update_factor.factor) * pos.y;
const relative_angle = player_sprite.is_ally ? this.camera_angle.rad : this.camera_angle.rad + Math.PI;
const scale = BattleStage.get_scale(relative_angle);
player_sprite.scale.setTo(scale, scale);
if (Math.sin(relative_angle) > 0 && player_sprite.position !== battle_positions.BACK) {
//change texture in function of position
player_sprite.set_position(battle_positions.BACK);
} else if (Math.sin(relative_angle) <= 0 && player_sprite.position !== battle_positions.FRONT) {
player_sprite.set_position(battle_positions.FRONT);
}
if (Math.cos(relative_angle) > 0 && player_sprite.scale.x < 0) {
//change side in function of position
player_sprite.scale.setTo(player_sprite.scale.x, player_sprite.scale.y);
} else if (Math.cos(relative_angle) <= 0 && player_sprite.scale.x > 0) {
player_sprite.scale.setTo(-player_sprite.scale.x, player_sprite.scale.y);
}
}
}
unset_stage(on_fade_complete: Function, on_flash_complete: Function) {
this.game.camera.fade();
this.game.camera.onFadeComplete.addOnce(() => {
if (on_fade_complete) {
on_fade_complete();
}
this.sprites.forEach(sprite => {
sprite.destroy();
});
this.group_allies.destroy(true);
this.group_enemies.destroy(true);
this.battle_group.destroy(true);
this.upper_rect.height = this.lower_rect.height = numbers.GAME_HEIGHT >> 1;
this.upper_rect.y = 0;
this.lower_rect.y = numbers.GAME_HEIGHT >> 1;
const fade_time = 300;
this.game.camera.resetFX();
this.game.add
.tween(this.upper_rect)
.to(
{
height: 0,
},
fade_time,
Phaser.Easing.Linear.None,
true
)
.onComplete.addOnce(() => {
if (on_flash_complete) {
on_flash_complete();
}
this.crop_group.destroy();
});
this.game.add.tween(this.lower_rect).to(
{
height: 0,
y: numbers.GAME_HEIGHT,
},
fade_time,
Phaser.Easing.Linear.None,
true
);
}, this);
}
static ellipse(angle: number, a: number, b: number) {
//ellipse formula
a = a ?? SEMI_MAJOR_AXIS;
b = b ?? SEMI_MINOR_AXIS;
return (a * b) / Math.sqrt(Math.pow(b * Math.cos(angle), 2) + Math.pow(a * Math.sin(angle), 2));
}
static ellipse_position(player_sprite: PlayerSprite, angle: number, is_x: boolean) {
if (is_x) {
const a = player_sprite.ellipses_semi_major;
return CENTER_X + BattleStage.ellipse(angle, a, SEMI_MINOR_AXIS) * Math.cos(angle);
} else {
const b = player_sprite.ellipses_semi_minor;
return CENTER_Y + BattleStage.ellipse(angle, SEMI_MAJOR_AXIS, b) * Math.sin(angle);
}
}
static get_angle(angle: number) {
//equidistant ellipse angle formula: https://math.stackexchange.com/a/1123448/202435
return (
angle +
Math.atan(
((SEMI_MINOR_AXIS - SEMI_MAJOR_AXIS) * Math.tan(angle)) /
(SEMI_MAJOR_AXIS + SEMI_MINOR_AXIS * Math.pow(Math.tan(angle), 2))
)
);
}
static get_scale(angle: number, default_scale: number = 1.0) {
return (Math.sin(angle) / 7 + SCALE_FACTOR) * default_scale;
}
}