diff --git a/src/battle-scene.ts b/src/battle-scene.ts index 1712cf9236f..455b440ce56 100644 --- a/src/battle-scene.ts +++ b/src/battle-scene.ts @@ -2879,6 +2879,27 @@ export default class BattleScene extends SceneBase { 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, + 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 * @param message string for MessagePhase diff --git a/src/phases/summon-phase.ts b/src/phases/summon-phase.ts index 31cd2645e68..6a82665d9d1 100644 --- a/src/phases/summon-phase.ts +++ b/src/phases/summon-phase.ts @@ -14,6 +14,7 @@ import { GameOverPhase } from "./game-over-phase"; import { ShinySparklePhase } from "./shiny-sparkle-phase"; import { MysteryEncounterMode } from "#enums/mystery-encounter-mode"; import { globalScene } from "#app/global-scene"; +import { Stat } from "#enums/stat"; export class SummonPhase extends PartyMemberPokemonPhase { private loaded: boolean; @@ -283,7 +284,13 @@ export class SummonPhase extends PartyMemberPokemonPhase { } 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() {