mirror of
https://github.com/pagefaultgames/pokerogue.git
synced 2025-06-30 21:42:20 +02:00
[Refactor] Create utility function makeArray
This replaces the `if(!Array.isArray(var)) { var = [var] }` pattern
This commit is contained in:
parent
425985a056
commit
5ae28da186
@ -17,6 +17,7 @@ import {
|
||||
isNullOrUndefined,
|
||||
BooleanHolder,
|
||||
type Constructor,
|
||||
makeArray,
|
||||
} from "#app/utils/common";
|
||||
import { deepMergeSpriteData } from "#app/utils/data";
|
||||
import type { Modifier, ModifierPredicate, TurnHeldItemTransferModifier } from "./modifier/modifier";
|
||||
|
@ -1,7 +1,7 @@
|
||||
import type Pokemon from "../field/pokemon";
|
||||
import { allMoves } from "./data-lists";
|
||||
import { MoveCategory } from "#enums/MoveCategory";
|
||||
import type { Constructor, nil } from "#app/utils/common";
|
||||
import { makeArray, type Constructor, type nil } from "#app/utils/common";
|
||||
import { AbilityId } from "#enums/ability-id";
|
||||
import { MoveId } from "#enums/move-id";
|
||||
import { SpeciesId } from "#enums/species-id";
|
||||
|
@ -1,7 +1,7 @@
|
||||
import { globalScene } from "#app/global-scene";
|
||||
import { modifierTypes } from "../data-lists";
|
||||
import { PokemonMove } from "../moves/pokemon-move";
|
||||
import { toReadableString, isNullOrUndefined, randSeedItem, randSeedInt, randSeedIntRange } from "#app/utils/common";
|
||||
import { toReadableString, isNullOrUndefined, randSeedItem, randSeedInt, makeArray, randSeedIntRange } from "#app/utils/common";
|
||||
import { pokemonEvolutions, pokemonPrevolutions } from "#app/data/balance/pokemon-evolutions";
|
||||
import { getPokemonSpecies } from "#app/data/pokemon-species";
|
||||
import { tmSpecies } from "#app/data/balance/tms";
|
||||
@ -554,10 +554,7 @@ export class TrainerConfig {
|
||||
this.speciesPools = evilAdminTrainerPools[poolName];
|
||||
|
||||
signatureSpecies.forEach((speciesPool, s) => {
|
||||
if (!Array.isArray(speciesPool)) {
|
||||
speciesPool = [speciesPool];
|
||||
}
|
||||
this.setPartyMemberFunc(-(s + 1), getRandomPartyMemberFunc(speciesPool));
|
||||
this.setPartyMemberFunc(-(s + 1), getRandomPartyMemberFunc(makeArray(speciesPool)));
|
||||
});
|
||||
|
||||
const nameForCall = this.name.toLowerCase().replace(/\s/g, "_");
|
||||
@ -620,10 +617,7 @@ export class TrainerConfig {
|
||||
this.setPartyTemplates(trainerPartyTemplates.RIVAL_5);
|
||||
}
|
||||
signatureSpecies.forEach((speciesPool, s) => {
|
||||
if (!Array.isArray(speciesPool)) {
|
||||
speciesPool = [speciesPool];
|
||||
}
|
||||
this.setPartyMemberFunc(-(s + 1), getRandomPartyMemberFunc(speciesPool));
|
||||
this.setPartyMemberFunc(-(s + 1), getRandomPartyMemberFunc(makeArray(speciesPool)));
|
||||
});
|
||||
if (!isNullOrUndefined(specialtyType)) {
|
||||
this.setSpeciesFilter(p => p.isOfType(specialtyType));
|
||||
@ -668,12 +662,8 @@ export class TrainerConfig {
|
||||
|
||||
// Set up party members with their corresponding species.
|
||||
signatureSpecies.forEach((speciesPool, s) => {
|
||||
// Ensure speciesPool is an array.
|
||||
if (!Array.isArray(speciesPool)) {
|
||||
speciesPool = [speciesPool];
|
||||
}
|
||||
// Set a function to get a random party member from the species pool.
|
||||
this.setPartyMemberFunc(-(s + 1), getRandomPartyMemberFunc(speciesPool));
|
||||
this.setPartyMemberFunc(-(s + 1), getRandomPartyMemberFunc(makeArray(speciesPool)));
|
||||
});
|
||||
|
||||
// If specialty type is provided, set species filter and specialty type.
|
||||
@ -729,12 +719,8 @@ export class TrainerConfig {
|
||||
|
||||
// Set up party members with their corresponding species.
|
||||
signatureSpecies.forEach((speciesPool, s) => {
|
||||
// Ensure speciesPool is an array.
|
||||
if (!Array.isArray(speciesPool)) {
|
||||
speciesPool = [speciesPool];
|
||||
}
|
||||
// Set a function to get a random party member from the species pool.
|
||||
this.setPartyMemberFunc(-(s + 1), getRandomPartyMemberFunc(speciesPool));
|
||||
this.setPartyMemberFunc(-(s + 1), getRandomPartyMemberFunc(makeArray(speciesPool)));
|
||||
});
|
||||
|
||||
// Set species filter and specialty type if provided, otherwise filter by base total.
|
||||
|
@ -1,6 +1,6 @@
|
||||
import { globalScene } from "#app/global-scene";
|
||||
import Pokemon from "./pokemon";
|
||||
import { fixedInt, randInt } from "#app/utils/common";
|
||||
import { fixedInt, makeArray, randInt } from "#app/utils/common";
|
||||
|
||||
export default class PokemonSpriteSparkleHandler {
|
||||
private sprites: Set<Phaser.GameObjects.Sprite>;
|
||||
@ -57,9 +57,7 @@ export default class PokemonSpriteSparkleHandler {
|
||||
}
|
||||
|
||||
add(sprites: Phaser.GameObjects.Sprite | Phaser.GameObjects.Sprite[]): void {
|
||||
if (!Array.isArray(sprites)) {
|
||||
sprites = [sprites];
|
||||
}
|
||||
sprites = makeArray(sprites);
|
||||
for (const s of sprites) {
|
||||
if (this.sprites.has(s)) {
|
||||
continue;
|
||||
@ -69,9 +67,7 @@ export default class PokemonSpriteSparkleHandler {
|
||||
}
|
||||
|
||||
remove(sprites: Phaser.GameObjects.Sprite | Phaser.GameObjects.Sprite[]): void {
|
||||
if (!Array.isArray(sprites)) {
|
||||
sprites = [sprites];
|
||||
}
|
||||
sprites = makeArray(sprites);
|
||||
for (const s of sprites) {
|
||||
this.sprites.delete(s);
|
||||
}
|
||||
|
@ -41,6 +41,7 @@ import {
|
||||
type nil,
|
||||
type Constructor,
|
||||
randSeedIntRange,
|
||||
makeArray,
|
||||
} from "#app/utils/common";
|
||||
import type { TypeDamageMultiplier } from "#app/data/type";
|
||||
import { getTypeDamageMultiplier, getTypeRgb } from "#app/data/type";
|
||||
@ -1774,9 +1775,7 @@ export default abstract class Pokemon extends Phaser.GameObjects.Container {
|
||||
let overrideArray: MoveId | Array<MoveId> = this.isPlayer()
|
||||
? Overrides.MOVESET_OVERRIDE
|
||||
: Overrides.OPP_MOVESET_OVERRIDE;
|
||||
if (!Array.isArray(overrideArray)) {
|
||||
overrideArray = [overrideArray];
|
||||
}
|
||||
overrideArray = makeArray(overrideArray);
|
||||
if (overrideArray.length > 0) {
|
||||
if (!this.isPlayer()) {
|
||||
this.moveset = [];
|
||||
|
@ -1,10 +1,8 @@
|
||||
import { makeArray } from "#app/utils/common";
|
||||
|
||||
let manifest: object;
|
||||
|
||||
export default class CacheBustedLoaderPlugin extends Phaser.Loader.LoaderPlugin {
|
||||
constructor(scene: Phaser.Scene) {
|
||||
super(scene);
|
||||
}
|
||||
|
||||
get manifest() {
|
||||
return manifest;
|
||||
}
|
||||
@ -14,9 +12,7 @@ export default class CacheBustedLoaderPlugin extends Phaser.Loader.LoaderPlugin
|
||||
}
|
||||
|
||||
addFile(file): void {
|
||||
if (!Array.isArray(file)) {
|
||||
file = [file];
|
||||
}
|
||||
file = makeArray(file);
|
||||
|
||||
file.forEach(item => {
|
||||
if (manifest) {
|
||||
|
@ -1,3 +1,5 @@
|
||||
import { makeArray } from "#app/utils/common";
|
||||
|
||||
export const legacyCompatibleImages: string[] = [];
|
||||
|
||||
export class SceneBase extends Phaser.Scene {
|
||||
@ -88,9 +90,7 @@ export class SceneBase extends Phaser.Scene {
|
||||
} else {
|
||||
folder += "/";
|
||||
}
|
||||
if (!Array.isArray(filenames)) {
|
||||
filenames = [filenames];
|
||||
}
|
||||
filenames = makeArray(filenames);
|
||||
for (const f of filenames as string[]) {
|
||||
this.load.audio(folder + key, this.getCachedUrl(`audio/${folder}${f}`));
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
import { globalScene } from "#app/global-scene";
|
||||
import { fixedInt } from "#app/utils/common";
|
||||
import { fixedInt, makeArray } from "#app/utils/common";
|
||||
|
||||
export enum PokemonIconAnimMode {
|
||||
NONE,
|
||||
@ -49,9 +49,7 @@ export default class PokemonIconAnimHandler {
|
||||
}
|
||||
|
||||
addOrUpdate(icons: PokemonIcon | PokemonIcon[], mode: PokemonIconAnimMode): void {
|
||||
if (!Array.isArray(icons)) {
|
||||
icons = [icons];
|
||||
}
|
||||
icons = makeArray(icons);
|
||||
for (const i of icons) {
|
||||
if (this.icons.has(i) && this.icons.get(i) === mode) {
|
||||
continue;
|
||||
@ -66,9 +64,7 @@ export default class PokemonIconAnimHandler {
|
||||
}
|
||||
|
||||
remove(icons: PokemonIcon | PokemonIcon[]): void {
|
||||
if (!Array.isArray(icons)) {
|
||||
icons = [icons];
|
||||
}
|
||||
icons = makeArray(icons);
|
||||
for (const i of icons) {
|
||||
if (this.toggled) {
|
||||
const icon = this.icons.get(i);
|
||||
|
@ -611,3 +611,10 @@ export function getShinyDescriptor(variant: Variant): string {
|
||||
return i18next.t("common:commonShiny");
|
||||
}
|
||||
}
|
||||
|
||||
export function makeArray<T>(input: T | T[]): T[] {
|
||||
if (!Array.isArray(input)) {
|
||||
return [input];
|
||||
}
|
||||
return input;
|
||||
}
|
||||
|
@ -12,6 +12,7 @@ import { UiMode } from "#enums/ui-mode";
|
||||
import { getMovePosition } from "#test/testUtils/gameManagerUtils";
|
||||
import { GameManagerHelper } from "#test/testUtils/helpers/gameManagerHelper";
|
||||
import { vi } from "vitest";
|
||||
import { makeArray } from "#app/utils/common";
|
||||
|
||||
/**
|
||||
* Helper to handle a Pokemon's move
|
||||
@ -157,9 +158,7 @@ export class MoveHelper extends GameManagerHelper {
|
||||
* @param moveset - The {@linkcode MoveId} (single or array) to change the Pokemon's moveset to.
|
||||
*/
|
||||
public changeMoveset(pokemon: Pokemon, moveset: MoveId | MoveId[]): void {
|
||||
if (!Array.isArray(moveset)) {
|
||||
moveset = [moveset];
|
||||
}
|
||||
moveset = makeArray(moveset);
|
||||
pokemon.moveset = [];
|
||||
moveset.forEach(move => {
|
||||
pokemon.moveset.push(new PokemonMove(move));
|
||||
|
@ -14,7 +14,7 @@ import { StatusEffect } from "#enums/status-effect";
|
||||
import type { WeatherType } from "#enums/weather-type";
|
||||
import { expect, vi } from "vitest";
|
||||
import { GameManagerHelper } from "./gameManagerHelper";
|
||||
import { shiftCharCodes } from "#app/utils/common";
|
||||
import { makeArray, shiftCharCodes } from "#app/utils/common";
|
||||
import type { RandomTrainerOverride } from "#app/overrides";
|
||||
import type { BattleType } from "#enums/battle-type";
|
||||
|
||||
@ -202,9 +202,7 @@ export class OverridesHelper extends GameManagerHelper {
|
||||
*/
|
||||
public moveset(moveset: MoveId | MoveId[]): this {
|
||||
vi.spyOn(Overrides, "MOVESET_OVERRIDE", "get").mockReturnValue(moveset);
|
||||
if (!Array.isArray(moveset)) {
|
||||
moveset = [moveset];
|
||||
}
|
||||
moveset = makeArray(moveset);
|
||||
const movesetStr = moveset.map(moveId => MoveId[moveId]).join(", ");
|
||||
this.log(`Player Pokemon moveset set to ${movesetStr} (=[${moveset.join(", ")}])!`);
|
||||
return this;
|
||||
@ -382,9 +380,7 @@ export class OverridesHelper extends GameManagerHelper {
|
||||
*/
|
||||
public enemyMoveset(moveset: MoveId | MoveId[]): this {
|
||||
vi.spyOn(Overrides, "OPP_MOVESET_OVERRIDE", "get").mockReturnValue(moveset);
|
||||
if (!Array.isArray(moveset)) {
|
||||
moveset = [moveset];
|
||||
}
|
||||
moveset = makeArray(moveset);
|
||||
const movesetStr = moveset.map(moveId => MoveId[moveId]).join(", ");
|
||||
this.log(`Enemy Pokemon moveset set to ${movesetStr} (=[${moveset.join(", ")}])!`);
|
||||
return this;
|
||||
|
Loading…
Reference in New Issue
Block a user