pokerogue/src/ui/fight-ui-handler.ts
Bertie690 5efdb0dc0b
[Refactor] Fix issues with "last move selected" vs "used" (#5810)
* Added `MoveUseType` and refactored MEP

* Fixed Wimp out tests & ME code

finally i think all the booleans are gone
i hope

* Added version migration for last resort and co.

buh gumbug

* Fixed various bugs and added tests for previous bugfixes

* Reverted a couple doc changes

* WIP

* Update pokemon-species.ts

Co-authored-by: NightKev <34855794+DayKev@users.noreply.github.com>

* Update pokemon-phase.ts

Co-authored-by: NightKev <34855794+DayKev@users.noreply.github.com>

* Fixed remaining tests (I think)

* Reverted rollout test changes

* Fixed command phase bug causing metronome test timeout

* Revert early_bird.test.ts

* Fix biome.jsonc

* Made `MoveUseType` start at 1

As per @DayKev's request

* Fixed a thing

* Fixed bolt beak condition to be marginally less jank

* Applied some review suggestions

* Reverted move phase operations

* Added helper functions complete with markdown tables

* Fixed things

* Update battler-tags.ts

* Fixed random issues

* Fixed code

* Fixed comment

* Fixed import issues

* Fix disable.test.ts conflicts

* Update instruct.test.ts

* Update `biome.jsonc`

* Renamed `MoveUseType` to `MoveUseMode`; applied review comments

* Fixed space

* Fixed phasemanager bugs

* Fixed instruct test to not bork

* Fixed gorilla tactics bug

* Battler Tags doc fixes

* Fixed formatting and suttff

* Minor comment updates and remove unused imports in `move.ts`

* Re-add `public`, remove unnecessary default value in `battler-tags.ts`

* Restore `{}` in `turn-start-phase.ts`

Fixes `lint/correctness/noSwitchDeclarations`

* Remove extra space in TSDoc in `move-phase.ts`

* Use `game.field` instead of `game.scene` in `instruct.test.ts`

Also `game.toEndOfTurn()` instead of
`game.phaseInterceptor.to("BerryPhase")`

* Use `game.field` instead of `game.scene` in `metronome.test.ts`

* Use `toEndOfTurn()` instead of `to("BerryPhase")` in `powder.test.ts`

* Convert `MoveUseMode` enum to `const` object

* Update move-phase.ts

Co-authored-by: NightKev <34855794+DayKev@users.noreply.github.com>

* Add `enumValueToKey` utility function

* Apply Biome

---------

Co-authored-by: NightKev <34855794+DayKev@users.noreply.github.com>
2025-06-15 10:52:44 -07:00

419 lines
14 KiB
TypeScript

import type { InfoToggle } from "#app/battle-scene";
import { globalScene } from "#app/global-scene";
import { addTextObject, TextStyle } from "./text";
import { getTypeDamageMultiplierColor } from "#app/data/type";
import { PokemonType } from "#enums/pokemon-type";
import { Command } from "#enums/command";
import { UiMode } from "#enums/ui-mode";
import UiHandler from "./ui-handler";
import { getLocalizedSpriteKey, fixedInt, padInt } from "#app/utils/common";
import { MoveCategory } from "#enums/MoveCategory";
import i18next from "i18next";
import { Button } from "#enums/buttons";
import type { EnemyPokemon } from "#app/field/pokemon";
import type { PokemonMove } from "#app/data/moves/pokemon-move";
import type Pokemon from "#app/field/pokemon";
import type { CommandPhase } from "#app/phases/command-phase";
import MoveInfoOverlay from "./move-info-overlay";
import { BattleType } from "#enums/battle-type";
import { MoveUseMode } from "#enums/move-use-mode";
export default class FightUiHandler extends UiHandler implements InfoToggle {
public static readonly MOVES_CONTAINER_NAME = "moves";
private movesContainer: Phaser.GameObjects.Container;
private moveInfoContainer: Phaser.GameObjects.Container;
private typeIcon: Phaser.GameObjects.Sprite;
private ppLabel: Phaser.GameObjects.Text;
private ppText: Phaser.GameObjects.Text;
private powerLabel: Phaser.GameObjects.Text;
private powerText: Phaser.GameObjects.Text;
private accuracyLabel: Phaser.GameObjects.Text;
private accuracyText: Phaser.GameObjects.Text;
private cursorObj: Phaser.GameObjects.Image | null;
private moveCategoryIcon: Phaser.GameObjects.Sprite;
private moveInfoOverlay: MoveInfoOverlay;
protected fieldIndex = 0;
protected fromCommand: Command = Command.FIGHT;
protected cursor2 = 0;
constructor() {
super(UiMode.FIGHT);
}
setup() {
const ui = this.getUi();
this.movesContainer = globalScene.add.container(18, -38.7);
this.movesContainer.setName(FightUiHandler.MOVES_CONTAINER_NAME);
ui.add(this.movesContainer);
this.moveInfoContainer = globalScene.add.container(1, 0);
this.moveInfoContainer.setName("move-info");
ui.add(this.moveInfoContainer);
this.typeIcon = globalScene.add.sprite(
globalScene.scaledCanvas.width - 57,
-36,
getLocalizedSpriteKey("types"),
"unknown",
);
this.typeIcon.setVisible(false);
this.moveInfoContainer.add(this.typeIcon);
this.moveCategoryIcon = globalScene.add.sprite(globalScene.scaledCanvas.width - 25, -36, "categories", "physical");
this.moveCategoryIcon.setVisible(false);
this.moveInfoContainer.add(this.moveCategoryIcon);
this.ppLabel = addTextObject(globalScene.scaledCanvas.width - 70, -26, "PP", TextStyle.MOVE_INFO_CONTENT);
this.ppLabel.setOrigin(0.0, 0.5);
this.ppLabel.setVisible(false);
this.ppLabel.setText(i18next.t("fightUiHandler:pp"));
this.moveInfoContainer.add(this.ppLabel);
this.ppText = addTextObject(globalScene.scaledCanvas.width - 12, -26, "--/--", TextStyle.MOVE_INFO_CONTENT);
this.ppText.setOrigin(1, 0.5);
this.ppText.setVisible(false);
this.moveInfoContainer.add(this.ppText);
this.powerLabel = addTextObject(globalScene.scaledCanvas.width - 70, -18, "POWER", TextStyle.MOVE_INFO_CONTENT);
this.powerLabel.setOrigin(0.0, 0.5);
this.powerLabel.setVisible(false);
this.powerLabel.setText(i18next.t("fightUiHandler:power"));
this.moveInfoContainer.add(this.powerLabel);
this.powerText = addTextObject(globalScene.scaledCanvas.width - 12, -18, "---", TextStyle.MOVE_INFO_CONTENT);
this.powerText.setOrigin(1, 0.5);
this.powerText.setVisible(false);
this.moveInfoContainer.add(this.powerText);
this.accuracyLabel = addTextObject(globalScene.scaledCanvas.width - 70, -10, "ACC", TextStyle.MOVE_INFO_CONTENT);
this.accuracyLabel.setOrigin(0.0, 0.5);
this.accuracyLabel.setVisible(false);
this.accuracyLabel.setText(i18next.t("fightUiHandler:accuracy"));
this.moveInfoContainer.add(this.accuracyLabel);
this.accuracyText = addTextObject(globalScene.scaledCanvas.width - 12, -10, "---", TextStyle.MOVE_INFO_CONTENT);
this.accuracyText.setOrigin(1, 0.5);
this.accuracyText.setVisible(false);
this.moveInfoContainer.add(this.accuracyText);
// prepare move overlay
const overlayScale = 1;
this.moveInfoOverlay = new MoveInfoOverlay({
delayVisibility: true,
scale: overlayScale,
onSide: true,
right: true,
x: 0,
y: -MoveInfoOverlay.getHeight(overlayScale, true),
width: globalScene.game.canvas.width / 6 + 4,
hideEffectBox: true,
hideBg: true,
});
ui.add(this.moveInfoOverlay);
// register the overlay to receive toggle events
globalScene.addInfoToggle(this.moveInfoOverlay);
globalScene.addInfoToggle(this);
}
show(args: any[]): boolean {
super.show(args);
this.fieldIndex = args.length ? (args[0] as number) : 0;
this.fromCommand = args.length > 1 ? (args[1] as Command) : Command.FIGHT;
const messageHandler = this.getUi().getMessageHandler();
messageHandler.bg.setVisible(false);
messageHandler.commandWindow.setVisible(false);
messageHandler.movesWindowContainer.setVisible(true);
const pokemon = (globalScene.phaseManager.getCurrentPhase() as CommandPhase).getPokemon();
if (pokemon.tempSummonData.turnCount <= 1) {
this.setCursor(0);
} else {
this.setCursor(this.getCursor());
}
this.displayMoves();
this.toggleInfo(false); // in case cancel was pressed while info toggle is active
this.active = true;
return true;
}
/**
* Process the player inputting the selected {@linkcode Button}.
* @param button - The {@linkcode Button} being pressed
* @returns Whether the input was successful (ie did anything).
*/
processInput(button: Button): boolean {
const ui = this.getUi();
const cursor = this.getCursor();
let success = false;
switch (button) {
case Button.CANCEL:
{
// Attempts to back out of the move selection pane are blocked in certain MEs
// TODO: Should we allow showing the summary menu at least?
const { battleType, mysteryEncounter } = globalScene.currentBattle;
if (battleType !== BattleType.MYSTERY_ENCOUNTER || !mysteryEncounter?.skipToFightInput) {
ui.setMode(UiMode.COMMAND, this.fieldIndex);
success = true;
}
}
break;
case Button.ACTION:
if (
(globalScene.phaseManager.getCurrentPhase() as CommandPhase).handleCommand(
this.fromCommand,
cursor,
MoveUseMode.NORMAL,
)
) {
success = true;
} else {
ui.playError();
}
break;
case Button.UP:
if (cursor >= 2) {
success = this.setCursor(cursor - 2);
}
break;
case Button.DOWN:
if (cursor < 2) {
success = this.setCursor(cursor + 2);
}
break;
case Button.LEFT:
if (cursor % 2 === 1) {
success = this.setCursor(cursor - 1);
}
break;
case Button.RIGHT:
if (cursor % 2 === 0) {
success = this.setCursor(cursor + 1);
}
break;
default:
// other inputs do nothing while in fight menu
}
if (success) {
ui.playSelect();
}
return success;
}
toggleInfo(visible: boolean): void {
if (visible) {
this.movesContainer.setVisible(false);
this.cursorObj?.setVisible(false);
}
globalScene.tweens.add({
targets: [this.movesContainer, this.cursorObj],
duration: fixedInt(125),
ease: "Sine.easeInOut",
alpha: visible ? 0 : 1,
});
if (!visible) {
this.movesContainer.setVisible(true);
this.cursorObj?.setVisible(true);
}
}
isActive(): boolean {
return this.active;
}
getCursor(): number {
return !this.fieldIndex ? this.cursor : this.cursor2;
}
setCursor(cursor: number): boolean {
const ui = this.getUi();
this.moveInfoOverlay.clear();
const changed = this.getCursor() !== cursor;
if (changed) {
if (!this.fieldIndex) {
this.cursor = cursor;
} else {
this.cursor2 = cursor;
}
}
if (!this.cursorObj) {
const isTera = this.fromCommand === Command.TERA;
this.cursorObj = globalScene.add.image(0, 0, isTera ? "cursor_tera" : "cursor");
this.cursorObj.setScale(isTera ? 0.7 : 1);
ui.add(this.cursorObj);
}
const pokemon = (globalScene.phaseManager.getCurrentPhase() as CommandPhase).getPokemon();
const moveset = pokemon.getMoveset();
const hasMove = cursor < moveset.length;
if (hasMove) {
const pokemonMove = moveset[cursor];
const moveType = pokemon.getMoveType(pokemonMove.getMove());
const textureKey = getLocalizedSpriteKey("types");
this.typeIcon.setTexture(textureKey, PokemonType[moveType].toLowerCase()).setScale(0.8);
const moveCategory = pokemonMove.getMove().category;
this.moveCategoryIcon.setTexture("categories", MoveCategory[moveCategory].toLowerCase()).setScale(1.0);
const power = pokemonMove.getMove().power;
const accuracy = pokemonMove.getMove().accuracy;
const maxPP = pokemonMove.getMovePp();
const pp = maxPP - pokemonMove.ppUsed;
const ppLeftStr = padInt(pp, 2, " ");
const ppMaxStr = padInt(maxPP, 2, " ");
this.ppText.setText(`${ppLeftStr}/${ppMaxStr}`);
this.powerText.setText(`${power >= 0 ? power : "---"}`);
this.accuracyText.setText(`${accuracy >= 0 ? accuracy : "---"}`);
const ppPercentLeft = pp / maxPP;
//** Determines TextStyle according to percentage of PP remaining */
let ppColorStyle = TextStyle.MOVE_PP_FULL;
if (ppPercentLeft > 0.25 && ppPercentLeft <= 0.5) {
ppColorStyle = TextStyle.MOVE_PP_HALF_FULL;
} else if (ppPercentLeft > 0 && ppPercentLeft <= 0.25) {
ppColorStyle = TextStyle.MOVE_PP_NEAR_EMPTY;
} else if (ppPercentLeft === 0) {
ppColorStyle = TextStyle.MOVE_PP_EMPTY;
}
//** Changes the text color and shadow according to the determined TextStyle */
this.ppText.setColor(this.getTextColor(ppColorStyle, false));
this.ppText.setShadowColor(this.getTextColor(ppColorStyle, true));
this.moveInfoOverlay.show(pokemonMove.getMove());
pokemon.getOpponents().forEach(opponent => {
(opponent as EnemyPokemon).updateEffectiveness(this.getEffectivenessText(pokemon, opponent, pokemonMove));
});
}
this.typeIcon.setVisible(hasMove);
this.ppLabel.setVisible(hasMove);
this.ppText.setVisible(hasMove);
this.powerLabel.setVisible(hasMove);
this.powerText.setVisible(hasMove);
this.accuracyLabel.setVisible(hasMove);
this.accuracyText.setVisible(hasMove);
this.moveCategoryIcon.setVisible(hasMove);
this.cursorObj.setPosition(13 + (cursor % 2 === 1 ? 114 : 0), -31 + (cursor >= 2 ? 15 : 0));
return changed;
}
/**
* Gets multiplier text for a pokemon's move against a specific opponent
* Returns undefined if it's a status move
*/
private getEffectivenessText(pokemon: Pokemon, opponent: Pokemon, pokemonMove: PokemonMove): string | undefined {
const effectiveness = opponent.getMoveEffectiveness(
pokemon,
pokemonMove.getMove(),
!opponent.waveData.abilityRevealed,
undefined,
undefined,
true,
);
if (effectiveness === undefined) {
return undefined;
}
return `${effectiveness}x`;
}
displayMoves() {
const pokemon = (globalScene.phaseManager.getCurrentPhase() as CommandPhase).getPokemon();
const moveset = pokemon.getMoveset();
for (let moveIndex = 0; moveIndex < 4; moveIndex++) {
const moveText = addTextObject(moveIndex % 2 === 0 ? 0 : 114, moveIndex < 2 ? 0 : 16, "-", TextStyle.WINDOW);
moveText.setName("text-empty-move");
if (moveIndex < moveset.length) {
const pokemonMove = moveset[moveIndex]!; // TODO is the bang correct?
moveText.setText(pokemonMove.getName());
moveText.setName(pokemonMove.getName());
moveText.setColor(this.getMoveColor(pokemon, pokemonMove) ?? moveText.style.color);
}
this.movesContainer.add(moveText);
}
}
/**
* Returns a specific move's color based on its type effectiveness against opponents
* If there are multiple opponents, the highest effectiveness' color is returned
* @returns A color or undefined if the default color should be used
*/
private getMoveColor(pokemon: Pokemon, pokemonMove: PokemonMove): string | undefined {
if (!globalScene.typeHints) {
return undefined;
}
const opponents = pokemon.getOpponents();
if (opponents.length <= 0) {
return undefined;
}
const moveColors = opponents
.map(opponent =>
opponent.getMoveEffectiveness(
pokemon,
pokemonMove.getMove(),
!opponent.waveData.abilityRevealed,
undefined,
undefined,
true,
),
)
.sort((a, b) => b - a)
.map(effectiveness => getTypeDamageMultiplierColor(effectiveness ?? 0, "offense"));
return moveColors[0];
}
clear() {
super.clear();
const messageHandler = this.getUi().getMessageHandler();
this.clearMoves();
this.typeIcon.setVisible(false);
this.ppLabel.setVisible(false);
this.ppText.setVisible(false);
this.powerLabel.setVisible(false);
this.powerText.setVisible(false);
this.accuracyLabel.setVisible(false);
this.accuracyText.setVisible(false);
this.moveCategoryIcon.setVisible(false);
this.moveInfoOverlay.clear();
messageHandler.bg.setVisible(true);
this.eraseCursor();
this.active = false;
}
clearMoves() {
this.movesContainer.removeAll(true);
const opponents = (globalScene.phaseManager.getCurrentPhase() as CommandPhase).getPokemon().getOpponents();
opponents.forEach(opponent => {
(opponent as EnemyPokemon).updateEffectiveness();
});
}
eraseCursor() {
if (this.cursorObj) {
this.cursorObj.destroy();
}
this.cursorObj = null;
}
}