pokerogue/src/ui/pokeball-tray.ts
NightKev 0107b1d47e
[Refactor] Create global scene variable (#4766)
* Replace various `scene` pass-arounds with global scene variable

* Modify tests

* Add scene back to `fade[in|out]()` calls

Co-authored-by: Moka <54149968+MokaStitcher@users.noreply.github.com>

* Fix Bug Superfan ME test

Co-authored-by: Moka <54149968+MokaStitcher@users.noreply.github.com>

* Re-enable fixed test

Co-authored-by: Moka <54149968+MokaStitcher@users.noreply.github.com>

* Rename `gScene` to `globalScene`

* Move `globalScene` to its own file to fix import/async issues

* Fix `SelectModifierPhase` tests

* Fix ME tests by removing `scene` from `expect()`s

* Resolve merge issues

* Remove tsdocs referencing `scene` params

Remove missed instances of `.scene`

* Remove unnecessary `globalScene` usage in `loading-scene.ts`

* Fix merge conflicts

* Attempt to fix circular import issue

* Found the source of the import issue

* Fix merge issues

---------

Co-authored-by: Moka <54149968+MokaStitcher@users.noreply.github.com>
2025-01-12 15:33:05 -08:00

120 lines
3.2 KiB
TypeScript

import { globalScene } from "#app/global-scene";
import type Pokemon from "../field/pokemon";
export default class PokeballTray extends Phaser.GameObjects.Container {
private player: boolean;
private bg: Phaser.GameObjects.NineSlice;
private balls: Phaser.GameObjects.Sprite[];
public shown: boolean;
constructor(player: boolean) {
super(globalScene, player ? (globalScene.game.canvas.width / 6) : 0, player ? -72 : -144);
this.player = player;
}
setup(): void {
this.bg = globalScene.add.nineslice(0, 0, `pb_tray_overlay_${this.player ? "player" : "enemy"}`, undefined, 104, 4, 48, 8, 0, 0);
this.bg.setOrigin(this.player ? 1 : 0, 0);
this.add(this.bg);
this.balls = new Array(6).fill(null).map((_, i) => globalScene.add.sprite((this.player ? -83 : 76) + (globalScene.game.canvas.width / 6) * (this.player ? -1 : 1) + 10 * i * (this.player ? 1 : -1), -8, "pb_tray_ball", "empty"));
for (const ball of this.balls) {
ball.setOrigin(0, 0);
this.add(ball);
}
this.setVisible(false);
this.shown = false;
}
showPbTray(party: Pokemon[]): Promise<void> {
return new Promise(resolve => {
if (this.shown) {
return resolve();
}
globalScene.fieldUI.bringToTop(this);
this.x += 104 * (this.player ? 1 : -1);
this.bg.width = 104;
this.bg.alpha = 1;
this.balls.forEach((ball, b) => {
ball.x += (globalScene.game.canvas.width / 6 + 104) * (this.player ? 1 : -1);
let ballFrame = "ball";
if (b >= party.length) {
ballFrame = "empty";
} else if (!party[b].hp) {
ballFrame = "faint";
} else if (party[b].status) {
ballFrame = "status";
}
ball.setFrame(ballFrame);
});
globalScene.playSound("se/pb_tray_enter");
globalScene.tweens.add({
targets: this,
x: `${this.player ? "-" : "+"}=104`,
duration: 500,
ease: "Sine.easeIn",
onComplete: () => {
this.balls.forEach((ball, b) => {
globalScene.tweens.add({
targets: ball,
x: `${this.player ? "-" : "+"}=104`,
duration: b * 100,
ease: "Sine.easeIn",
onComplete: () => globalScene.playSound(`se/${(b < party.length ? "pb_tray_ball" : "pb_tray_empty")}`)
});
});
}
});
this.setVisible(true);
this.shown = true;
globalScene.time.delayedCall(1100, () => resolve());
});
}
hide(): Promise<void> {
return new Promise(resolve => {
if (!this.shown) {
return resolve();
}
this.balls.forEach((ball, b) => {
globalScene.tweens.add({
targets: ball,
x: `${this.player ? "-" : "+"}=${globalScene.game.canvas.width / 6}`,
duration: 250,
delay: b * 100,
ease: "Sine.easeIn"
});
});
globalScene.tweens.add({
targets: this.bg,
width: 144,
alpha: 0,
duration: 500,
ease: "Sine.easeIn"
});
globalScene.time.delayedCall(850, () => {
this.setVisible(false);
resolve();
});
this.shown = false;
});
}
}