pokerogue/src/ui/pokemon-icon-anim-handler.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

95 lines
2.3 KiB
TypeScript

import { globalScene } from "#app/global-scene";
import * as Utils from "../utils";
export enum PokemonIconAnimMode {
NONE,
PASSIVE,
ACTIVE
}
type PokemonIcon = Phaser.GameObjects.Container | Phaser.GameObjects.Sprite;
export default class PokemonIconAnimHandler {
private icons: Map<PokemonIcon, PokemonIconAnimMode>;
private toggled: boolean;
setup(): void {
this.icons = new Map();
this.toggled = false;
const onAlternate = (tween: Phaser.Tweens.Tween) => {
const value = tween.getValue();
this.toggled = !!value;
for (const i of this.icons.keys()) {
const icon = this.icons.get(i);
const delta = icon ? this.getModeYDelta(icon) : 0;
i.y += delta * (this.toggled ? 1 : -1);
}
};
globalScene.tweens.addCounter({
duration: Utils.fixedInt(200),
from: 0,
to: 1,
yoyo: true,
repeat: -1,
onRepeat: onAlternate,
onYoyo: onAlternate
});
}
getModeYDelta(mode: PokemonIconAnimMode): number {
switch (mode) {
case PokemonIconAnimMode.NONE:
return 0;
case PokemonIconAnimMode.PASSIVE:
return -1;
case PokemonIconAnimMode.ACTIVE:
return -2;
}
}
addOrUpdate(icons: PokemonIcon | PokemonIcon[], mode: PokemonIconAnimMode): void {
if (!Array.isArray(icons)) {
icons = [ icons ];
}
for (const i of icons) {
if (this.icons.has(i) && this.icons.get(i) === mode) {
continue;
}
if (this.toggled) {
const lastYDelta = this.icons.has(i)
? this.icons.get(i)!
: 0;
const yDelta = this.getModeYDelta(mode);
i.y += yDelta + lastYDelta;
}
this.icons.set(i, mode);
}
}
remove(icons: PokemonIcon | PokemonIcon[]): void {
if (!Array.isArray(icons)) {
icons = [ icons ];
}
for (const i of icons) {
if (this.toggled) {
const icon = this.icons.get(i);
const delta = icon ? this.getModeYDelta(icon) : 0;
i.y -= delta;
}
this.icons.delete(i);
}
}
removeAll(): void {
for (const i of this.icons.keys()) {
if (this.toggled) {
const icon = this.icons.get(i);
const delta = icon ? this.getModeYDelta(icon) : 0;
i.y -= delta;
}
this.icons.delete(i);
}
}
}