[Refactor] Reduce nesting in doEvolution() (#4786)

Co-authored-by: Tempoanon <163687446+Tempo-anon@users.noreply.github.com>
This commit is contained in:
Adrian T. 2024-11-04 11:40:24 +08:00 committed by GitHub
parent 3a767ed38a
commit 618dfbd324
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,12 +1,12 @@
import SoundFade from "phaser3-rex-plugins/plugins/soundfade"; import SoundFade from "phaser3-rex-plugins/plugins/soundfade";
import { Phase } from "#app/phase"; import { Phase } from "#app/phase";
import BattleScene from "#app/battle-scene"; import BattleScene, { AnySound } from "#app/battle-scene";
import { SpeciesFormEvolution } from "#app/data/balance/pokemon-evolutions"; import { SpeciesFormEvolution } from "#app/data/balance/pokemon-evolutions";
import EvolutionSceneHandler from "#app/ui/evolution-scene-handler"; import EvolutionSceneHandler from "#app/ui/evolution-scene-handler";
import * as Utils from "#app/utils"; import * as Utils from "#app/utils";
import { Mode } from "#app/ui/ui"; import { Mode } from "#app/ui/ui";
import { cos, sin } from "#app/field/anims"; import { cos, sin } from "#app/field/anims";
import { PlayerPokemon } from "#app/field/pokemon"; import Pokemon, { PlayerPokemon } from "#app/field/pokemon";
import { getTypeRgb } from "#app/data/type"; import { getTypeRgb } from "#app/data/type";
import i18next from "i18next"; import i18next from "i18next";
import { getPokemonNameWithAffix } from "#app/messages"; import { getPokemonNameWithAffix } from "#app/messages";
@ -17,7 +17,11 @@ export class EvolutionPhase extends Phase {
protected pokemon: PlayerPokemon; protected pokemon: PlayerPokemon;
protected lastLevel: integer; protected lastLevel: integer;
private preEvolvedPokemonName: string;
private evolution: SpeciesFormEvolution | null; private evolution: SpeciesFormEvolution | null;
private evolutionBgm: AnySound;
private evolutionHandler: EvolutionSceneHandler;
protected evolutionContainer: Phaser.GameObjects.Container; protected evolutionContainer: Phaser.GameObjects.Container;
protected evolutionBaseBg: Phaser.GameObjects.Image; protected evolutionBaseBg: Phaser.GameObjects.Image;
@ -35,6 +39,8 @@ export class EvolutionPhase extends Phase {
this.pokemon = pokemon; this.pokemon = pokemon;
this.evolution = evolution; this.evolution = evolution;
this.lastLevel = lastLevel; this.lastLevel = lastLevel;
this.evolutionBgm = this.scene.playSoundWithoutBgm("evolution");
this.preEvolvedPokemonName = getPokemonNameWithAffix(this.pokemon);
} }
validate(): boolean { validate(): boolean {
@ -117,10 +123,9 @@ export class EvolutionPhase extends Phase {
} }
doEvolution(): void { doEvolution(): void {
const evolutionHandler = this.scene.ui.getHandler() as EvolutionSceneHandler; this.evolutionHandler = this.scene.ui.getHandler() as EvolutionSceneHandler;
const preName = getPokemonNameWithAffix(this.pokemon);
this.scene.ui.showText(i18next.t("menu:evolving", { pokemonName: preName }), null, () => { this.scene.ui.showText(i18next.t("menu:evolving", { pokemonName: this.preEvolvedPokemonName }), null, () => {
this.pokemon.cry(); this.pokemon.cry();
this.pokemon.getPossibleEvolution(this.evolution).then(evolvedPokemon => { this.pokemon.getPossibleEvolution(this.evolution).then(evolvedPokemon => {
@ -140,7 +145,6 @@ export class EvolutionPhase extends Phase {
}); });
this.scene.time.delayedCall(1000, () => { this.scene.time.delayedCall(1000, () => {
const evolutionBgm = this.scene.playSoundWithoutBgm("evolution");
this.scene.tweens.add({ this.scene.tweens.add({
targets: this.evolutionBgOverlay, targets: this.evolutionBgOverlay,
alpha: 1, alpha: 1,
@ -174,10 +178,30 @@ export class EvolutionPhase extends Phase {
this.scene.time.delayedCall(1500, () => { this.scene.time.delayedCall(1500, () => {
this.pokemonEvoTintSprite.setScale(0.25); this.pokemonEvoTintSprite.setScale(0.25);
this.pokemonEvoTintSprite.setVisible(true); this.pokemonEvoTintSprite.setVisible(true);
evolutionHandler.canCancel = true; this.evolutionHandler.canCancel = true;
this.doCycle(1).then(success => { this.doCycle(1).then(success => {
if (!success) { if (success) {
this.handleSuccessEvolution(evolvedPokemon);
} else {
this.handleFailedEvolution(evolvedPokemon);
}
});
});
});
}
});
}
});
});
});
}, 1000);
}
/**
* Handles a failed/stopped evolution
* @param evolvedPokemon - The evolved Pokemon
*/
private handleFailedEvolution(evolvedPokemon: Pokemon): void {
this.pokemonSprite.setVisible(true); this.pokemonSprite.setVisible(true);
this.pokemonTintSprite.setScale(1); this.pokemonTintSprite.setScale(1);
this.scene.tweens.add({ this.scene.tweens.add({
@ -189,12 +213,12 @@ export class EvolutionPhase extends Phase {
} }
}); });
SoundFade.fadeOut(this.scene, evolutionBgm, 100); SoundFade.fadeOut(this.scene, this.evolutionBgm, 100);
this.scene.unshiftPhase(new EndEvolutionPhase(this.scene)); this.scene.unshiftPhase(new EndEvolutionPhase(this.scene));
this.scene.ui.showText(i18next.t("menu:stoppedEvolving", { pokemonName: preName }), null, () => { this.scene.ui.showText(i18next.t("menu:stoppedEvolving", { pokemonName: this.preEvolvedPokemonName }), null, () => {
this.scene.ui.showText(i18next.t("menu:pauseEvolutionsQuestion", { pokemonName: preName }), null, () => { this.scene.ui.showText(i18next.t("menu:pauseEvolutionsQuestion", { pokemonName: this.preEvolvedPokemonName }), null, () => {
const end = () => { const end = () => {
this.scene.ui.showText("", 0); this.scene.ui.showText("", 0);
this.scene.playBgm(); this.scene.playBgm();
@ -204,21 +228,40 @@ export class EvolutionPhase extends Phase {
this.scene.ui.setOverlayMode(Mode.CONFIRM, () => { this.scene.ui.setOverlayMode(Mode.CONFIRM, () => {
this.scene.ui.revertMode(); this.scene.ui.revertMode();
this.pokemon.pauseEvolutions = true; this.pokemon.pauseEvolutions = true;
this.scene.ui.showText(i18next.t("menu:evolutionsPaused", { pokemonName: preName }), null, end, 3000); this.scene.ui.showText(i18next.t("menu:evolutionsPaused", { pokemonName: this.preEvolvedPokemonName }), null, end, 3000);
}, () => { }, () => {
this.scene.ui.revertMode(); this.scene.ui.revertMode();
this.scene.time.delayedCall(3000, end); this.scene.time.delayedCall(3000, end);
}); });
}); });
}, null, true); }, null, true);
return;
} }
/**
* Handles a successful evolution
* @param evolvedPokemon - The evolved Pokemon
*/
private handleSuccessEvolution(evolvedPokemon: Pokemon): void {
this.scene.playSound("se/sparkle"); this.scene.playSound("se/sparkle");
this.pokemonEvoSprite.setVisible(true); this.pokemonEvoSprite.setVisible(true);
this.doCircleInward(); this.doCircleInward();
const onEvolutionComplete = () => {
SoundFade.fadeOut(this.scene, this.evolutionBgm, 100);
this.scene.time.delayedCall(250, () => {
this.pokemon.cry();
this.scene.time.delayedCall(1250, () => {
this.scene.playSoundWithoutBgm("evolution_fanfare");
evolvedPokemon.destroy();
this.scene.ui.showText(i18next.t("menu:evolutionDone", { pokemonName: this.preEvolvedPokemonName, evolvedPokemonName: this.pokemon.name }), null, () => this.end(), null, true, Utils.fixedInt(4000));
this.scene.time.delayedCall(Utils.fixedInt(4250), () => this.scene.playBgm());
});
});
};
this.scene.time.delayedCall(900, () => { this.scene.time.delayedCall(900, () => {
evolutionHandler.canCancel = false; this.evolutionHandler.canCancel = false;
this.pokemon.evolve(this.evolution, this.pokemon.species).then(() => { this.pokemon.evolve(this.evolution, this.pokemon.species).then(() => {
const levelMoves = this.pokemon.getLevelMoves(this.lastLevel + 1, true); const levelMoves = this.pokemon.getLevelMoves(this.lastLevel + 1, true);
@ -248,28 +291,7 @@ export class EvolutionPhase extends Phase {
targets: this.evolutionBgOverlay, targets: this.evolutionBgOverlay,
alpha: 0, alpha: 0,
duration: 250, duration: 250,
onComplete: () => { onComplete: onEvolutionComplete
SoundFade.fadeOut(this.scene, evolutionBgm, 100);
this.scene.time.delayedCall(250, () => {
this.pokemon.cry();
this.scene.time.delayedCall(1250, () => {
this.scene.playSoundWithoutBgm("evolution_fanfare");
evolvedPokemon.destroy();
this.scene.ui.showText(i18next.t("menu:evolutionDone", { pokemonName: preName, evolvedPokemonName: this.pokemon.name }), null, () => this.end(), null, true, Utils.fixedInt(4000));
this.scene.time.delayedCall(Utils.fixedInt(4250), () => this.scene.playBgm());
});
});
}
});
}
});
}
});
});
});
});
});
}); });
} }
}); });
@ -277,7 +299,6 @@ export class EvolutionPhase extends Phase {
}); });
}); });
}); });
}, 1000);
} }
doSpiralUpward() { doSpiralUpward() {
@ -320,7 +341,6 @@ export class EvolutionPhase extends Phase {
doCycle(l: number, lastCycle: integer = 15): Promise<boolean> { doCycle(l: number, lastCycle: integer = 15): Promise<boolean> {
return new Promise(resolve => { return new Promise(resolve => {
const evolutionHandler = this.scene.ui.getHandler() as EvolutionSceneHandler;
const isLastCycle = l === lastCycle; const isLastCycle = l === lastCycle;
this.scene.tweens.add({ this.scene.tweens.add({
targets: this.pokemonTintSprite, targets: this.pokemonTintSprite,
@ -336,7 +356,7 @@ export class EvolutionPhase extends Phase {
duration: 500 / l, duration: 500 / l,
yoyo: !isLastCycle, yoyo: !isLastCycle,
onComplete: () => { onComplete: () => {
if (evolutionHandler.cancelled) { if (this.evolutionHandler.cancelled) {
return resolve(false); return resolve(false);
} }
if (l < lastCycle) { if (l < lastCycle) {