From 5f4daa2ef4fccf59565781812c0c4621cb4226f1 Mon Sep 17 00:00:00 2001 From: Dean Date: Wed, 12 Mar 2025 12:47:51 -0700 Subject: [PATCH] Move sorting to BattleScene --- src/battle-scene.ts | 19 +++++++++++++++++++ src/phases/post-summon-phase.ts | 3 ++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/battle-scene.ts b/src/battle-scene.ts index 1712cf9236f..3048c22d9d8 100644 --- a/src/battle-scene.ts +++ b/src/battle-scene.ts @@ -2879,6 +2879,25 @@ export default class BattleScene extends SceneBase { return false; } + /** + * Sorts the first consecutive set of occurences of {@linkcode targetPhase} in {@linkcode phaseQueue} + * @param targetPhase The type of phase to search for and sort + * @param by A function to compare the phases with + * @see {@linkcode Array.sort} for the comparison function + */ + sortPhaseType(targetPhase: Constructor, by: (a: Phase, b: Phase) => number): void { + const startIndex = this.phaseQueue.findIndex(phase => phase instanceof targetPhase); + if (startIndex === -1) { + return; + } + const endIndex = this.phaseQueue.findIndex((phase, index) => index > startIndex && !(phase instanceof targetPhase)); + + const sortedSubset = this.phaseQueue + .slice(startIndex, endIndex !== -1 ? endIndex + 1 : this.phaseQueue.length) + .sort(by); + this.phaseQueue.splice(startIndex, sortedSubset.length, ...sortedSubset); + } + /** * Adds a MessagePhase, either to PhaseQueuePrepend or nextCommandPhaseQueue * @param message string for MessagePhase diff --git a/src/phases/post-summon-phase.ts b/src/phases/post-summon-phase.ts index 4154593022d..7d830c99ae3 100644 --- a/src/phases/post-summon-phase.ts +++ b/src/phases/post-summon-phase.ts @@ -64,7 +64,8 @@ export class PostSummonPhase extends PokemonPhase { } private orderPostSummonPhases() { - globalScene.phaseQueue.sort( + globalScene.sortPhaseType( + PostSummonPhase, (phaseA: PostSummonPhase, phaseB: PostSummonPhase) => phaseB.getPokemon().getEffectiveStat(Stat.SPD) - phaseA.getPokemon().getEffectiveStat(Stat.SPD), );