Move sorting to BattleScene

This commit is contained in:
Dean 2025-03-12 12:47:51 -07:00
parent 80ac3d964c
commit 5f4daa2ef4
2 changed files with 21 additions and 1 deletions

View File

@ -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<Phase>, 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

View File

@ -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),
);