pokerogue/src/ui/pokemon-icon-anim-handler.ts
Sirz Benjie 5854b21da0
[Refactor] Remove circular imports part 1 (#5663)
* Extract Mode enum out of UI and into its own file

Reduces circular imports from 909 to 773

* Move around utility files

Reduces cyclical dependencies from 773 to 765

* Remove starterColors and bypassLogin from battle-scene

Reduces cyclical dependencies from 765 to 623

* Fix test runner error

* Update import for bypassLogin in test

* Update mocks for utils in tests

* Fix broken tests

* Update selectWithTera override

* Update path for utils in ab-attr.ts

* Update path for utils in ability-class.ts

* Fix utils import path in healer.test.ts
2025-04-19 11:57:03 +00:00

93 lines
2.3 KiB
TypeScript

import { globalScene } from "#app/global-scene";
import { fixedInt } from "#app/utils/common";
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: 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);
}
}
}