pokerogue/src/phases/turn-init-phase.ts
Sirz Benjie 1c4edabd1d
[Refactor] Ensure that new phases are created through the phase manager
https://github.com/pagefaultgames/pokerogue/pull/5955

* Add newPhase method to phase-manager

* Update calls to append/prepend phase to use string phase

* Replace instantiations of new phase with phase manager
2025-06-07 23:55:30 -07:00

80 lines
2.8 KiB
TypeScript

import { BattlerIndex } from "#app/battle";
import {
handleMysteryEncounterBattleStartEffects,
handleMysteryEncounterTurnStartEffects,
} from "#app/data/mystery-encounters/utils/encounter-phase-utils";
import { TurnInitEvent } from "#app/events/battle-scene";
import type { PlayerPokemon } from "#app/field/pokemon";
import i18next from "i18next";
import { FieldPhase } from "./field-phase";
import { globalScene } from "#app/global-scene";
export class TurnInitPhase extends FieldPhase {
public readonly phaseName = "TurnInitPhase";
start() {
super.start();
globalScene.getPlayerField().forEach(p => {
// If this pokemon is in play and evolved into something illegal under the current challenge, force a switch
if (p.isOnField() && !p.isAllowedInBattle()) {
globalScene.phaseManager.queueMessage(
i18next.t("challenges:illegalEvolution", { pokemon: p.name }),
null,
true,
);
const allowedPokemon = globalScene.getPokemonAllowedInBattle();
if (!allowedPokemon.length) {
// If there are no longer any legal pokemon in the party, game over.
globalScene.phaseManager.clearPhaseQueue();
globalScene.phaseManager.unshiftNew("GameOverPhase");
} else if (
allowedPokemon.length >= globalScene.currentBattle.getBattlerCount() ||
(globalScene.currentBattle.double && !allowedPokemon[0].isActive(true))
) {
// If there is at least one pokemon in the back that is legal to switch in, force a switch.
p.switchOut();
} else {
// If there are no pokemon in the back but we're not game overing, just hide the pokemon.
// This should only happen in double battles.
p.leaveField();
}
if (allowedPokemon.length === 1 && globalScene.currentBattle.double) {
globalScene.phaseManager.unshiftNew("ToggleDoublePositionPhase", true);
}
}
});
globalScene.eventTarget.dispatchEvent(new TurnInitEvent());
handleMysteryEncounterBattleStartEffects();
// If true, will skip remainder of current phase (and not queue CommandPhases etc.)
if (handleMysteryEncounterTurnStartEffects()) {
this.end();
return;
}
globalScene.getField().forEach((pokemon, i) => {
if (pokemon?.isActive()) {
if (pokemon.isPlayer()) {
globalScene.currentBattle.addParticipant(pokemon as PlayerPokemon);
}
pokemon.resetTurnData();
if (pokemon.isPlayer()) {
globalScene.phaseManager.pushNew("CommandPhase", i);
} else {
globalScene.phaseManager.pushNew("EnemyCommandPhase", i - BattlerIndex.ENEMY);
}
}
});
globalScene.phaseManager.pushNew("TurnStartPhase");
this.end();
}
}