Add prependToPhaseWithCondition and use it in SummonPhase to determine speed order

This commit is contained in:
Dean 2025-03-11 17:46:46 -07:00
parent b298138157
commit 3ab6f15e49
2 changed files with 29 additions and 1 deletions

View File

@ -2879,6 +2879,27 @@ export default class BattleScene extends SceneBase {
return false; return false;
} }
/**
* Prepends a phase to the first occurence of {@linkcode targetPhase} which satisfies {@linkcode condition}
*
* Pushes the phase to the end of the queue if no occurrence is found
* @param newPhase The phase to be added
* @param targetPhase The type of phase to search for
* @param condition The condition that the target phase must meet
*/
prependToPhaseWithCondition(
newPhase: Phase,
targetPhase: Constructor<Phase>,
condition: (newPhase: Phase, prependPhase: Phase) => boolean,
) {
const prependPhase = this.findPhase(phase => phase instanceof targetPhase && condition(newPhase, phase));
if (prependPhase) {
this.phaseQueue.splice(this.phaseQueue.indexOf(prependPhase), 0, newPhase);
} else {
this.pushPhase(newPhase);
}
}
/** /**
* Adds a MessagePhase, either to PhaseQueuePrepend or nextCommandPhaseQueue * Adds a MessagePhase, either to PhaseQueuePrepend or nextCommandPhaseQueue
* @param message string for MessagePhase * @param message string for MessagePhase

View File

@ -14,6 +14,7 @@ import { GameOverPhase } from "./game-over-phase";
import { ShinySparklePhase } from "./shiny-sparkle-phase"; import { ShinySparklePhase } from "./shiny-sparkle-phase";
import { MysteryEncounterMode } from "#enums/mystery-encounter-mode"; import { MysteryEncounterMode } from "#enums/mystery-encounter-mode";
import { globalScene } from "#app/global-scene"; import { globalScene } from "#app/global-scene";
import { Stat } from "#enums/stat";
export class SummonPhase extends PartyMemberPokemonPhase { export class SummonPhase extends PartyMemberPokemonPhase {
private loaded: boolean; private loaded: boolean;
@ -283,7 +284,13 @@ export class SummonPhase extends PartyMemberPokemonPhase {
} }
queuePostSummon(): void { queuePostSummon(): void {
globalScene.pushPhase(new PostSummonPhase(this.getPokemon().getBattlerIndex())); // Insert PostSummonPhase in speed order
globalScene.prependToPhaseWithCondition(
new PostSummonPhase(this.getPokemon().getBattlerIndex()),
PostSummonPhase,
(newPhase: PostSummonPhase, prependPhase: PostSummonPhase) =>
prependPhase.getPokemon().getEffectiveStat(Stat.SPD) < newPhase.getPokemon().getEffectiveStat(Stat.SPD),
);
} }
end() { end() {