pokerogue/src/sprites/pokemon-sprite.ts
Sirz Benjie 787feceb14
[Refactor] Refactor variant sprite code part 1 (#5592)
* Move exp to its own masterlist, simplify initVariantData

* Update test/sprites/pokemonSprite.test.ts

* Extract loadPokemonVariantAssets out of BattleScene

* move variant.ts and update pokemon.loadAssets

* Add fuzzy matching for applying variant recolors

* Move glsl shaders to their own files

* Remove extra variants from shader masterlist

Their exp sprites have since been removed.

Co-authored-by: Unicorn_Power <189861924+Unicornpowerstar@users.noreply.github.com>

* Make exp sprite keys a set instead of an array

* Remove outdated exp sprite jsons

Co-authored-by: Unicorn_Power <189861924+Unicornpowerstar@users.noreply.github.com>

---------

Co-authored-by: Unicorn_Power <189861924+Unicornpowerstar@users.noreply.github.com>
2025-04-09 10:43:05 -05:00

80 lines
2.6 KiB
TypeScript

import { globalScene } from "#app/global-scene";
import { variantColorCache, variantData } from "#app/sprites/variant";
import { Gender } from "#app/data/gender";
import { hasExpSprite } from "./sprite-utils";
import type { Variant, VariantSet } from "#app/sprites/variant";
import type Pokemon from "#app/field/pokemon";
import type BattleScene from "#app/battle-scene";
// Regex patterns
/** Regex matching double underscores */
const DUNDER_REGEX = /\_{2}/g;
/**
* Calculate the sprite ID from a pokemon form.
*/
export function getSpriteId(pokemon: Pokemon, ignoreOverride?: boolean): string {
return pokemon
.getSpeciesForm(ignoreOverride)
.getSpriteId(
pokemon.getGender(ignoreOverride) === Gender.FEMALE,
pokemon.formIndex,
pokemon.shiny,
pokemon.variant,
);
}
export function getBattleSpriteId(pokemon: Pokemon, back?: boolean, ignoreOverride = false): string {
if (back === undefined) {
back = pokemon.isPlayer();
}
return pokemon
.getSpeciesForm(ignoreOverride)
.getSpriteId(
pokemon.getGender(ignoreOverride) === Gender.FEMALE,
pokemon.formIndex,
pokemon.shiny,
pokemon.variant,
back,
);
}
/** Compute the path to the sprite atlas by converting double underscores to path components (/)
*/
export function getSpriteAtlasPath(pokemon: Pokemon, ignoreOverride = false): string {
const spriteId = getSpriteId(pokemon, ignoreOverride).replace(DUNDER_REGEX, "/");
return `${/_[1-3]$/.test(spriteId) ? "variant/" : ""}${spriteId}`;
}
/**
* Load the variant assets for the given sprite and store it in {@linkcode variantColorCache}.
* @param spriteKey - The key of the sprite to load
* @param fileRoot - The root path of the sprite file
* @param variant - The variant to load
* @param scene - The scene to load the assets in (defaults to the global scene)
*/
export async function loadPokemonVariantAssets(
spriteKey: string,
fileRoot: string,
variant: Variant,
scene: BattleScene = globalScene,
): Promise<void> {
if (variantColorCache.hasOwnProperty(spriteKey)) {
return;
}
const useExpSprite = scene.experimentalSprites && hasExpSprite(spriteKey);
if (useExpSprite) {
fileRoot = `exp/${fileRoot}`;
}
let variantConfig = variantData;
fileRoot.split("/").map(p => (variantConfig ? (variantConfig = variantConfig[p]) : null));
const variantSet = variantConfig as VariantSet;
if (!variantConfig || variantSet[variant] !== 1) {
return;
}
variantColorCache[spriteKey] = await scene
.cachedFetch(`./images/pokemon/variant/${fileRoot}.json`)
.then(res => res.json());
}