[Refactor] Optimize trick room ordering code (#4468)

This commit is contained in:
Leo Kim 2024-09-30 10:18:07 +09:00 committed by GitHub
parent 3976d115d8
commit b43620b8c5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -43,20 +43,17 @@ export class TurnStartPhase extends FieldPhase {
orderedTargets = Utils.randSeedShuffle(orderedTargets); orderedTargets = Utils.randSeedShuffle(orderedTargets);
}, this.scene.currentBattle.turn, this.scene.waveSeed); }, this.scene.currentBattle.turn, this.scene.waveSeed);
orderedTargets.sort((a: Pokemon, b: Pokemon) => { // Next, a check for Trick Room is applied to determine sort order.
const aSpeed = a?.getEffectiveStat(Stat.SPD) || 0;
const bSpeed = b?.getEffectiveStat(Stat.SPD) || 0;
return bSpeed - aSpeed;
});
// Next, a check for Trick Room is applied. If Trick Room is present, the order is reversed.
const speedReversed = new Utils.BooleanHolder(false); const speedReversed = new Utils.BooleanHolder(false);
this.scene.arena.applyTags(TrickRoomTag, speedReversed); this.scene.arena.applyTags(TrickRoomTag, speedReversed);
if (speedReversed.value) { // Adjust the sort function based on whether Trick Room is active.
orderedTargets = orderedTargets.reverse(); orderedTargets.sort((a: Pokemon, b: Pokemon) => {
} const aSpeed = a?.getEffectiveStat(Stat.SPD) ?? 0;
const bSpeed = b?.getEffectiveStat(Stat.SPD) ?? 0;
return speedReversed.value ? aSpeed - bSpeed : bSpeed - aSpeed;
});
return orderedTargets.map(t => t.getFieldIndex() + (!t.isPlayer() ? BattlerIndex.ENEMY : BattlerIndex.PLAYER)); return orderedTargets.map(t => t.getFieldIndex() + (!t.isPlayer() ? BattlerIndex.ENEMY : BattlerIndex.PLAYER));
} }