pokerogue/src/ui/command-ui-handler.ts

189 lines
5.3 KiB
TypeScript
Raw Normal View History

import BattleScene from "../battle-scene";
2023-04-20 15:46:05 -04:00
import { addTextObject, TextStyle } from "./text";
import PartyUiHandler, { PartyUiMode } from "./party-ui-handler";
import { Mode } from "./ui";
import UiHandler from "./ui-handler";
import i18next from "i18next";
2024-10-04 13:08:31 +08:00
import { Button } from "#enums/buttons";
import { getPokemonNameWithAffix } from "#app/messages";
import { CommandPhase } from "#app/phases/command-phase";
2023-03-28 14:54:52 -04:00
export enum Command {
FIGHT = 0,
BALL,
POKEMON,
RUN
}
2023-03-28 14:54:52 -04:00
export default class CommandUiHandler extends UiHandler {
private commandsContainer: Phaser.GameObjects.Container;
[Refactor] use typescript `strict-null` (#3259) * TS: enable strict-null * fix battle-scene.ts * fix voucher.ts * adapt more files to strict-null * adapt more files to strict-null ( 2) * adapt ability.ts to strict-null * adapt `arena.ts` to strict-null * adapt TagAddedEvent constructor to strict-null * adapt phases.ts.to strict-null * adapt status-effect.ts to strict-null * adapt `account.ts` to strict-null * adapt `configHandler.ts` to strict-null * adapt `ability.ts` to strict-null * adapt `biomes.ts` to strict-null * adapt `challenge.ts` to strict-null * adapt `daily-run.ts` to strict-null * adapt `nature.ts` to strict-null * adapt `pokemon-forms.ts` to strict-null * adapt `tainer-names.ts` to strict-null * adapt `types.ts` to strict-null * adapt `weather.ts` to strict-null * adapt `egg-hatch-phase.ts` to strict-null * adapt `evolution-phase.ts` to strict-null * adapt `pokemon-sprite-sparkle-handler.ts` to strict-null * adapt `evolution-phase.ts` to strict-null * adapt `game-mode.ts` to strict-null * adapt `utils.ts` to strict-null * adapt `voucher-ui-handler.ts` to strict-null * adapt `src/ui/unavailable-modal-ui-handler.ts` to strict-null * adapt `src/ui/ui.ts` to strict-null * adapt `src/ui/ui-theme.ts` to strict-null * adapt `src/ui/title-ui-handler.ts` to strict-null * adapt `src/ui/time-of-day-widget.ts` to strict-null * adapt `src/ui/text.ts` to strict-null * adapt `src/ui/target-select-ui-handler.ts` to strict-null * adapt `src/ui/settings/settings-keyboard-ui-handler.ts` to strict-null * adapt more files to strict-null (3) * adapt more files to strict-null (4) * adapt more files (mostly tests) to strict-null (5) * adapt more files to strict-null (6) * adapt more files to strict-null (7) * Update `src/data/pokemon-evolutions.ts` for strict-null Partial update `src/data/pokemon-species.ts` for strict-null * adapt more files to strict-null (8) * adapt more files to strict-null (9) * Strict some more nulls (still a few errors remaining) * adapt rest of the files to strict-null (9) * fix tests (check for null instead of undefined) * repalce a lot of `??` with bangs And added TODO notice as usual * fix more tests * all tests pass now * fix broken game-loop after trainer battle add some console.warn for missing cases and falling back to default * remove guessed fallback from utils.rgbHexToRgba * add TODO for this.currentBattle = null * adjust getPokemonById() return to include `null` * fix compilation errors * add test for pokemon.trySetStatus * `chanceMultiplier` shouldn't be optional * allow `null` for currentPhase * adjust hasExpSprite logic for no keymatch found * reduce bang usage in account.updateUserInfo() * fix new strict-null issues after merge * fix `strict-null` issues in dropdown.ts and sand_spit.test.ts * fix egg-gacha * adapt gul_missile.test.ts to strict-null * fix move.ts strict-null * fix i18n.ts strict-null * fix strict-null issues * fix baton_pass test after accidentially breaking it * chore: fix compiler errors * revert accidential changes in baton_pass.test.ts --------- Co-authored-by: NightKev <34855794+DayKev@users.noreply.github.com>
2024-08-07 09:23:12 -07:00
private cursorObj: Phaser.GameObjects.Image | null;
2023-03-28 14:54:52 -04:00
protected fieldIndex: integer = 0;
protected cursor2: integer = 0;
2023-03-28 14:54:52 -04:00
constructor(scene: BattleScene) {
super(scene, Mode.COMMAND);
}
setup() {
const ui = this.getUi();
2024-05-24 01:45:04 +02:00
const commands = [
i18next.t("commandUiHandler:fight"),
i18next.t("commandUiHandler:ball"),
i18next.t("commandUiHandler:pokemon"),
i18next.t("commandUiHandler:run")
];
2023-03-28 14:54:52 -04:00
this.commandsContainer = this.scene.add.container(217, -38.7);
this.commandsContainer.setName("commands");
2023-03-28 14:54:52 -04:00
this.commandsContainer.setVisible(false);
ui.add(this.commandsContainer);
for (let c = 0; c < commands.length; c++) {
const commandText = addTextObject(this.scene, c % 2 === 0 ? 0 : 55.8, c < 2 ? 0 : 16, commands[c], TextStyle.WINDOW);
commandText.setName(commands[c]);
2023-03-28 14:54:52 -04:00
this.commandsContainer.add(commandText);
}
}
2023-12-30 18:41:25 -05:00
show(args: any[]): boolean {
2023-03-28 14:54:52 -04:00
super.show(args);
this.fieldIndex = args.length ? args[0] as integer : 0;
2023-03-28 14:54:52 -04:00
this.commandsContainer.setVisible(true);
2024-01-09 23:34:43 -05:00
let commandPhase: CommandPhase;
const currentPhase = this.scene.getCurrentPhase();
if (currentPhase instanceof CommandPhase) {
2024-01-09 23:34:43 -05:00
commandPhase = currentPhase;
} else {
2024-01-09 23:34:43 -05:00
commandPhase = this.scene.getStandbyPhase() as CommandPhase;
}
2024-01-09 23:34:43 -05:00
2023-03-28 14:54:52 -04:00
const messageHandler = this.getUi().getMessageHandler();
messageHandler.bg.setVisible(true);
messageHandler.commandWindow.setVisible(true);
messageHandler.movesWindowContainer.setVisible(false);
2023-03-28 14:54:52 -04:00
messageHandler.message.setWordWrapWidth(1110);
2024-10-04 13:08:31 +08:00
messageHandler.showText(i18next.t("commandUiHandler:actionMessage", { pokemonName: getPokemonNameWithAffix(commandPhase.getPokemon()) }), 0);
if (this.getCursor() === Command.POKEMON) {
this.setCursor(Command.FIGHT);
} else {
this.setCursor(this.getCursor());
}
2023-12-30 18:41:25 -05:00
return true;
2023-03-28 14:54:52 -04:00
}
processInput(button: Button): boolean {
2023-03-28 14:54:52 -04:00
const ui = this.getUi();
let success = false;
const cursor = this.getCursor();
if (button === Button.CANCEL || button === Button.ACTION) {
2024-05-24 01:45:04 +02:00
if (button === Button.ACTION) {
switch (cursor) {
// Fight
case Command.FIGHT:
if ((this.scene.getCurrentPhase() as CommandPhase).checkFightOverride()) {
return true;
}
ui.setMode(Mode.FIGHT, (this.scene.getCurrentPhase() as CommandPhase).getFieldIndex());
success = true;
break;
2024-03-30 15:34:00 -04:00
// Ball
case Command.BALL:
ui.setModeWithoutClear(Mode.BALL);
success = true;
break;
2024-03-30 15:34:00 -04:00
// Pokemon
case Command.POKEMON:
ui.setMode(Mode.PARTY, PartyUiMode.SWITCH, (this.scene.getCurrentPhase() as CommandPhase).getPokemon().getFieldIndex(), null, PartyUiHandler.FilterNonFainted);
success = true;
break;
2024-03-30 15:34:00 -04:00
// Run
case Command.RUN:
(this.scene.getCurrentPhase() as CommandPhase).handleCommand(Command.RUN, 0);
success = true;
break;
2023-03-28 14:54:52 -04:00
}
} else {
(this.scene.getCurrentPhase() as CommandPhase).cancel();
}
2023-03-28 14:54:52 -04:00
} else {
switch (button) {
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;
2023-03-28 14:54:52 -04:00
}
}
if (success) {
2023-03-28 14:54:52 -04:00
ui.playSelect();
}
return success;
2023-03-28 14:54:52 -04:00
}
getCursor(): integer {
return !this.fieldIndex ? this.cursor : this.cursor2;
}
2023-03-28 14:54:52 -04:00
setCursor(cursor: integer): boolean {
const changed = this.getCursor() !== cursor;
if (changed) {
if (!this.fieldIndex) {
this.cursor = cursor;
} else {
this.cursor2 = cursor;
}
}
2023-03-28 14:54:52 -04:00
if (!this.cursorObj) {
this.cursorObj = this.scene.add.image(0, 0, "cursor");
this.commandsContainer.add(this.cursorObj);
2023-03-28 14:54:52 -04:00
}
this.cursorObj.setPosition(-5 + (cursor % 2 === 1 ? 56 : 0), 8 + (cursor >= 2 ? 16 : 0));
2023-03-28 14:54:52 -04:00
return changed;
2023-03-28 14:54:52 -04:00
}
2023-04-30 00:51:33 -04:00
clear(): void {
2023-03-28 14:54:52 -04:00
super.clear();
2024-01-09 23:34:43 -05:00
this.getUi().getMessageHandler().commandWindow.setVisible(false);
2023-03-28 14:54:52 -04:00
this.commandsContainer.setVisible(false);
this.getUi().getMessageHandler().clearText();
this.eraseCursor();
}
2023-04-30 00:51:33 -04:00
eraseCursor(): void {
if (this.cursorObj) {
2023-03-28 14:54:52 -04:00
this.cursorObj.destroy();
}
2023-03-28 14:54:52 -04:00
this.cursorObj = null;
}
}