Skip to content

Commit

Permalink
[P1 Bug] Prevent crash from missing pokemon sprite (#4821)
Browse files Browse the repository at this point in the history
* catch Sprite.play() errors in egg hatch phase

* catch more Sprite.play() errors causing crashes if sprite missing

* Add changes suggested by PigeonBar

---------

Co-authored-by: NightKev <[email protected]>
Co-authored-by: Moka <[email protected]>
  • Loading branch information
3 people authored Dec 1, 2024
1 parent 37b8c33 commit fad29ff
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,13 @@ function doPokemonTradeSequence(scene: BattleScene, tradedPokemon: PlayerPokemon
receivedPokemonTintSprite.setTintFill(getPokeballTintColor(receivedPokemon.pokeball));

[ tradedPokemonSprite, tradedPokemonTintSprite ].map(sprite => {
sprite.play(tradedPokemon.getSpriteKey(true));
const spriteKey = tradedPokemon.getSpriteKey(true);
try {
sprite.play(spriteKey);
} catch (err: unknown) {
console.error(`Failed to play animation for ${spriteKey}`, err);
}

sprite.setPipeline(scene.spritePipeline, { tone: [ 0.0, 0.0, 0.0, 0.0 ], hasShadow: false, teraColor: getTypeRgb(tradedPokemon.getTeraType()) });
sprite.setPipelineData("ignoreTimeTint", true);
sprite.setPipelineData("spriteKey", tradedPokemon.getSpriteKey());
Expand All @@ -597,7 +603,13 @@ function doPokemonTradeSequence(scene: BattleScene, tradedPokemon: PlayerPokemon
});

[ receivedPokemonSprite, receivedPokemonTintSprite ].map(sprite => {
sprite.play(receivedPokemon.getSpriteKey(true));
const spriteKey = receivedPokemon.getSpriteKey(true);
try {
sprite.play(spriteKey);
} catch (err: unknown) {
console.error(`Failed to play animation for ${spriteKey}`, err);
}

sprite.setPipeline(scene.spritePipeline, { tone: [ 0.0, 0.0, 0.0, 0.0 ], hasShadow: false, teraColor: getTypeRgb(tradedPokemon.getTeraType()) });
sprite.setPipelineData("ignoreTimeTint", true);
sprite.setPipelineData("spriteKey", receivedPokemon.getSpriteKey());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,13 @@ export function doPokemonTransformationSequence(scene: BattleScene, previousPoke
pokemonEvoTintSprite.setTintFill(0xFFFFFF);

[ pokemonSprite, pokemonTintSprite, pokemonEvoSprite, pokemonEvoTintSprite ].map(sprite => {
sprite.play(previousPokemon.getSpriteKey(true));
const spriteKey = previousPokemon.getSpriteKey(true);
try {
sprite.play(spriteKey);
} catch (err: unknown) {
console.error(`Failed to play animation for ${spriteKey}`, err);
}

sprite.setPipeline(scene.spritePipeline, { tone: [ 0.0, 0.0, 0.0, 0.0 ], hasShadow: false, teraColor: getTypeRgb(previousPokemon.getTeraType()) });
sprite.setPipelineData("ignoreTimeTint", true);
sprite.setPipelineData("spriteKey", previousPokemon.getSpriteKey());
Expand All @@ -69,7 +75,13 @@ export function doPokemonTransformationSequence(scene: BattleScene, previousPoke
});

[ pokemonEvoSprite, pokemonEvoTintSprite ].map(sprite => {
sprite.play(transformPokemon.getSpriteKey(true));
const spriteKey = transformPokemon.getSpriteKey(true);
try {
sprite.play(spriteKey);
} catch (err: unknown) {
console.error(`Failed to play animation for ${spriteKey}`, err);
}

sprite.setPipelineData("ignoreTimeTint", true);
sprite.setPipelineData("spriteKey", transformPokemon.getSpriteKey());
sprite.setPipelineData("shiny", transformPokemon.shiny);
Expand Down
12 changes: 10 additions & 2 deletions src/field/pokemon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3741,8 +3741,16 @@ export default abstract class Pokemon extends Phaser.GameObjects.Container {

setFrameRate(frameRate: integer) {
this.scene.anims.get(this.getBattleSpriteKey()).frameRate = frameRate;
this.getSprite().play(this.getBattleSpriteKey());
this.getTintSprite()?.play(this.getBattleSpriteKey());
try {
this.getSprite().play(this.getBattleSpriteKey());
} catch (err: unknown) {
console.error(`Failed to play animation for ${this.getBattleSpriteKey()}`, err);
}
try {
this.getTintSprite()?.play(this.getBattleSpriteKey());
} catch (err: unknown) {
console.error(`Failed to play animation for ${this.getBattleSpriteKey()}`, err);
}
}

tint(color: number, alpha?: number, duration?: integer, ease?: string) {
Expand Down
7 changes: 6 additions & 1 deletion src/phases/egg-hatch-phase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,12 @@ export class EggHatchPhase extends Phase {
this.scene.validateAchv(achvs.HATCH_SHINY);
}
this.eggContainer.setVisible(false);
this.pokemonSprite.play(this.pokemon.getSpriteKey(true));
const spriteKey = this.pokemon.getSpriteKey(true);
try {
this.pokemonSprite.play(spriteKey);
} catch (err: unknown) {
console.error(`Failed to play animation for ${spriteKey}`, err);
}
this.pokemonSprite.setPipelineData("ignoreTimeTint", true);
this.pokemonSprite.setPipelineData("spriteKey", this.pokemon.getSpriteKey());
this.pokemonSprite.setPipelineData("shiny", this.pokemon.shiny);
Expand Down
16 changes: 14 additions & 2 deletions src/phases/evolution-phase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,13 @@ export class EvolutionPhase extends Phase {
this.scene.ui.add(this.evolutionOverlay);

[ this.pokemonSprite, this.pokemonTintSprite, this.pokemonEvoSprite, this.pokemonEvoTintSprite ].map(sprite => {
sprite.play(this.pokemon.getSpriteKey(true));
const spriteKey = this.pokemon.getSpriteKey(true);
try {
sprite.play(spriteKey);
} catch (err: unknown) {
console.error(`Failed to play animation for ${spriteKey}`, err);
}

sprite.setPipeline(this.scene.spritePipeline, { tone: [ 0.0, 0.0, 0.0, 0.0 ], hasShadow: false, teraColor: getTypeRgb(this.pokemon.getTeraType()) });
sprite.setPipelineData("ignoreTimeTint", true);
sprite.setPipelineData("spriteKey", this.pokemon.getSpriteKey());
Expand All @@ -130,7 +136,13 @@ export class EvolutionPhase extends Phase {
this.pokemon.getPossibleEvolution(this.evolution).then(evolvedPokemon => {

[ this.pokemonEvoSprite, this.pokemonEvoTintSprite ].map(sprite => {
sprite.play(evolvedPokemon.getSpriteKey(true));
const spriteKey = evolvedPokemon.getSpriteKey(true);
try {
sprite.play(spriteKey);
} catch (err: unknown) {
console.error(`Failed to play animation for ${spriteKey}`, err);
}

sprite.setPipelineData("ignoreTimeTint", true);
sprite.setPipelineData("spriteKey", evolvedPokemon.getSpriteKey());
sprite.setPipelineData("shiny", evolvedPokemon.shiny);
Expand Down
8 changes: 7 additions & 1 deletion src/phases/form-change-phase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,13 @@ export class FormChangePhase extends EvolutionPhase {
this.pokemon.getPossibleForm(this.formChange).then(transformedPokemon => {

[ this.pokemonEvoSprite, this.pokemonEvoTintSprite ].map(sprite => {
sprite.play(transformedPokemon.getSpriteKey(true));
const spriteKey = transformedPokemon.getSpriteKey(true);
try {
sprite.play(spriteKey);
} catch (err: unknown) {
console.error(`Failed to play animation for ${spriteKey}`, err);
}

sprite.setPipelineData("ignoreTimeTint", true);
sprite.setPipelineData("spriteKey", transformedPokemon.getSpriteKey());
sprite.setPipelineData("shiny", transformedPokemon.shiny);
Expand Down
14 changes: 12 additions & 2 deletions src/phases/quiet-form-change-phase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,12 @@ export class QuietFormChangePhase extends BattlePhase {
const getPokemonSprite = () => {
const sprite = this.scene.addPokemonSprite(this.pokemon, this.pokemon.x + this.pokemon.getSprite().x, this.pokemon.y + this.pokemon.getSprite().y, "pkmn__sub");
sprite.setOrigin(0.5, 1);
sprite.play(this.pokemon.getBattleSpriteKey()).stop();
const spriteKey = this.pokemon.getBattleSpriteKey();
try {
sprite.play(spriteKey).stop();
} catch (err: unknown) {
console.error(`Failed to play animation for ${spriteKey}`, err);
}
sprite.setPipeline(this.scene.spritePipeline, { tone: [ 0.0, 0.0, 0.0, 0.0 ], hasShadow: false, teraColor: getTypeRgb(this.pokemon.getTeraType()) });
[ "spriteColors", "fusionSpriteColors" ].map(k => {
if (this.pokemon.summonData?.speciesForm) {
Expand Down Expand Up @@ -81,7 +86,12 @@ export class QuietFormChangePhase extends BattlePhase {
this.pokemon.setVisible(false);
this.pokemon.changeForm(this.formChange).then(() => {
pokemonFormTintSprite.setScale(0.01);
pokemonFormTintSprite.play(this.pokemon.getBattleSpriteKey()).stop();
const spriteKey = this.pokemon.getBattleSpriteKey();
try {
pokemonFormTintSprite.play(spriteKey).stop();
} catch (err: unknown) {
console.error(`Failed to play animation for ${spriteKey}`, err);
}
pokemonFormTintSprite.setVisible(true);
this.scene.tweens.add({
targets: pokemonTintSprite,
Expand Down
8 changes: 6 additions & 2 deletions src/ui/summary-ui-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,8 +321,12 @@ export default class SummaryUiHandler extends UiHandler {
this.numberText.setText(Utils.padInt(this.pokemon.species.speciesId, 4));
this.numberText.setColor(this.getTextColor(!this.pokemon.isShiny() ? TextStyle.SUMMARY : TextStyle.SUMMARY_GOLD));
this.numberText.setShadowColor(this.getTextColor(!this.pokemon.isShiny() ? TextStyle.SUMMARY : TextStyle.SUMMARY_GOLD, true));

this.pokemonSprite.play(this.pokemon.getSpriteKey(true));
const spriteKey = this.pokemon.getSpriteKey(true);
try {
this.pokemonSprite.play(spriteKey);
} catch (err: unknown) {
console.error(`Failed to play animation for ${spriteKey}`, err);
}
this.pokemonSprite.setPipelineData("teraColor", getTypeRgb(this.pokemon.getTeraType()));
this.pokemonSprite.setPipelineData("ignoreTimeTint", true);
this.pokemonSprite.setPipelineData("spriteKey", this.pokemon.getSpriteKey());
Expand Down

0 comments on commit fad29ff

Please sign in to comment.