Skip to content

Commit

Permalink
[Sprite] Fix game not waiting for variant data to finish loading (#5130)
Browse files Browse the repository at this point in the history
Co-authored-by: Moka <[email protected]>
Co-authored-by: damocleas <[email protected]>
  • Loading branch information
3 people authored Jan 16, 2025
1 parent c3641a3 commit 188664f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 18 deletions.
30 changes: 16 additions & 14 deletions src/battle-scene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -363,28 +363,30 @@ export default class BattleScene extends SceneBase {
/**
* Load the variant assets for the given sprite and stores them in {@linkcode variantColorCache}
*/
loadPokemonVariantAssets(spriteKey: string, fileRoot: string, variant?: Variant) {
public async loadPokemonVariantAssets(spriteKey: string, fileRoot: string, variant?: Variant): Promise<void> {
const useExpSprite = this.experimentalSprites && this.hasExpSprite(spriteKey);
if (useExpSprite) {
fileRoot = `exp/${fileRoot}`;
}
let variantConfig = variantData;
fileRoot.split("/").map(p => variantConfig ? variantConfig = variantConfig[p] : null);
fileRoot.split("/").map((p) => (variantConfig ? (variantConfig = variantConfig[p]) : null));
const variantSet = variantConfig as VariantSet;
if (variantSet && (variant !== undefined && variantSet[variant] === 1)) {
const populateVariantColors = (key: string): Promise<void> => {
return new Promise(resolve => {
if (variantColorCache.hasOwnProperty(key)) {
return resolve();
}
this.cachedFetch(`./images/pokemon/variant/${fileRoot}.json`).then(res => res.json()).then(c => {
variantColorCache[key] = c;

return new Promise<void>((resolve) => {
if (variantSet && variant !== undefined && variantSet[variant] === 1) {
if (variantColorCache.hasOwnProperty(spriteKey)) {
return resolve();
}
this.cachedFetch(`./images/pokemon/variant/${fileRoot}.json`)
.then((res) => res.json())
.then((c) => {
variantColorCache[spriteKey] = c;
resolve();
});
});
};
populateVariantColors(spriteKey);
}
} else {
resolve();
}
});
}

async preload() {
Expand Down
3 changes: 1 addition & 2 deletions src/data/pokemon-species.ts
Original file line number Diff line number Diff line change
Expand Up @@ -516,8 +516,7 @@ export abstract class PokemonSpeciesForm {
globalScene.anims.get(spriteKey).frameRate = 10;
}
const spritePath = this.getSpriteAtlasPath(female, formIndex, shiny, variant).replace("variant/", "").replace(/_[1-3]$/, "");
globalScene.loadPokemonVariantAssets(spriteKey, spritePath, variant);
resolve();
globalScene.loadPokemonVariantAssets(spriteKey, spritePath, variant).then(() => resolve());
});
if (startLoad) {
if (!globalScene.load.isLoading()) {
Expand Down
5 changes: 3 additions & 2 deletions src/field/mystery-encounter-intro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,11 +215,12 @@ export default class MysteryEncounterIntroVisuals extends Phaser.GameObjects.Con
resolve();
}

const shinyPromises: Promise<void>[] = [];
this.spriteConfigs.forEach((config) => {
if (config.isPokemon) {
globalScene.loadPokemonAtlas(config.spriteKey, config.fileRoot);
if (config.isShiny) {
globalScene.loadPokemonVariantAssets(config.spriteKey, config.fileRoot, config.variant);
shinyPromises.push(globalScene.loadPokemonVariantAssets(config.spriteKey, config.fileRoot, config.variant));
}
} else if (config.isItem) {
globalScene.loadAtlas("items", "");
Expand Down Expand Up @@ -254,7 +255,7 @@ export default class MysteryEncounterIntroVisuals extends Phaser.GameObjects.Con
return true;
});

resolve();
Promise.all(shinyPromises).then(() => resolve());
});

if (!globalScene.load.isLoading()) {
Expand Down

0 comments on commit 188664f

Please sign in to comment.