Skip to content

Commit

Permalink
[Bug] Fixing crash that happens when failing to load experimental spr…
Browse files Browse the repository at this point in the history
…ites.
  • Loading branch information
podarsmarty committed Sep 18, 2024
1 parent 6030b78 commit 88203da
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions src/field/pokemon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -436,18 +436,33 @@ export default abstract class Pokemon extends Phaser.GameObjects.Container {
if (variantColorCache.hasOwnProperty(key)) {
return resolve();
}
this.scene.cachedFetch(`./images/pokemon/variant/${useExpSprite ? "exp/" : ""}${battleSpritePath}.json`).
then(res => {
const getSpritePath = (isExpSprite: boolean): string => {
return `./images/pokemon/variant/${isExpSprite ? "exp/" : ""}${battleSpritePath}.json`;
};
const handleError = (spritePath: string, isExpSprite: boolean, ...optionalParams: any[]) => {
console.warn(`Could not load ${spritePath}!`, ...optionalParams);
if (isExpSprite) {
// If failed to load experimental sprites, fall back on non-experimental before giving up
attemptProcessVariant(false);
}
return;
};
const attemptProcessVariant = (isExpSprite: boolean) => {
const spritePath = getSpritePath(useExpSprite);
this.scene.cachedFetch(spritePath).then(res => {
// Prevent the JSON from processing if it failed to load
if (!res.ok) {
console.error(`Could not load ${res.url}!`);
return;
handleError(res.url, isExpSprite);
}
return res.json();
}).catch(error => {
handleError(spritePath, isExpSprite, error);
}).then(c => {
variantColorCache[key] = c;
resolve();
});
};
attemptProcessVariant(useExpSprite);
} else {
resolve();
}
Expand Down

0 comments on commit 88203da

Please sign in to comment.