pokerogue/src/system/modifier-data.ts
Sirz Benjie 408b66f913
[Misc][Refactor][GitHub] Ditch eslint for biome, and add a formatter (#5495)
Co-authored-by: NightKev <34855794+DayKev@users.noreply.github.com>
2025-03-09 14:13:25 -07:00

64 lines
2.1 KiB
TypeScript

import { globalScene } from "#app/global-scene";
import { PersistentModifier } from "#app/modifier/modifier";
import type { GeneratedPersistentModifierType, ModifierType } from "#app/modifier/modifier-type";
import { ModifierTypeGenerator, getModifierTypeFuncById } from "#app/modifier/modifier-type";
export default class ModifierData {
public player: boolean;
public typeId: string;
public typePregenArgs: any[];
public args: any[];
public stackCount: number;
public className: string;
constructor(source: PersistentModifier | any, player: boolean) {
const sourceModifier = source instanceof PersistentModifier ? (source as PersistentModifier) : null;
this.player = player;
this.typeId = sourceModifier ? sourceModifier.type.id : source.typeId;
if (sourceModifier) {
if ("getPregenArgs" in source.type) {
this.typePregenArgs = (source.type as GeneratedPersistentModifierType).getPregenArgs();
}
} else if (source.typePregenArgs) {
this.typePregenArgs = source.typePregenArgs;
}
this.args = sourceModifier ? sourceModifier.getArgs() : source.args || [];
this.stackCount = source.stackCount;
this.className = sourceModifier ? sourceModifier.constructor.name : source.className;
}
toModifier(_constructor: any): PersistentModifier | null {
const typeFunc = getModifierTypeFuncById(this.typeId);
if (!typeFunc) {
return null;
}
try {
let type: ModifierType | null = typeFunc();
type.id = this.typeId;
if (type instanceof ModifierTypeGenerator) {
type = (type as ModifierTypeGenerator).generateType(
this.player ? globalScene.getPlayerParty() : globalScene.getEnemyField(),
this.typePregenArgs,
);
}
const ret = Reflect.construct(
_constructor,
([type] as any[]).concat(this.args).concat(this.stackCount),
) as PersistentModifier;
if (ret.stackCount > ret.getMaxStackCount()) {
ret.stackCount = ret.getMaxStackCount();
}
return ret;
} catch (err) {
console.error(err);
return null;
}
}
}