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

140 lines
4.4 KiB
TypeScript
Raw Normal View History

import { CommandPhase } from "../phases";
import BattleScene from "../battle-scene";
import { getPokeballName } from "../data/pokeball";
import { addTextObject, getTextStyleOptions, TextStyle } from "./text";
2023-04-01 22:59:07 -04:00
import { Command } from "./command-ui-handler";
import { Mode } from "./ui";
import UiHandler from "./ui-handler";
import { addWindow } from "./ui-theme";
import {Button} from "#enums/buttons";
2023-04-01 22:59:07 -04:00
export default class BallUiHandler extends UiHandler {
private pokeballSelectContainer: Phaser.GameObjects.Container;
private pokeballSelectBg: Phaser.GameObjects.NineSlice;
2023-04-01 22:59:07 -04:00
private countsText: Phaser.GameObjects.Text;
private cursorObj: Phaser.GameObjects.Image;
private scale: number = 0.1666666667;
2023-04-01 22:59:07 -04:00
constructor(scene: BattleScene) {
super(scene, Mode.BALL);
}
setup() {
const ui = this.getUi();
2024-05-24 01:45:04 +02:00
this.scale = getTextStyleOptions(TextStyle.WINDOW, this.scene.uiTheme).scale;
2023-04-01 22:59:07 -04:00
let optionsTextContent = "";
2023-04-01 22:59:07 -04:00
for (let pb = 0; pb < Object.keys(this.scene.pokeballCounts).length; pb++) {
2023-04-01 22:59:07 -04:00
optionsTextContent += `${getPokeballName(pb)}\n`;
}
optionsTextContent += "Cancel";
const optionsText = addTextObject(this.scene, 0, 0, optionsTextContent, TextStyle.WINDOW, { align: "right", maxLines: 6 });
const optionsTextWidth = optionsText.displayWidth;
this.pokeballSelectContainer = this.scene.add.container((this.scene.game.canvas.width / 6) - 51 - Math.max(64, optionsTextWidth), -49);
this.pokeballSelectContainer.setVisible(false);
ui.add(this.pokeballSelectContainer);
this.pokeballSelectBg = addWindow(this.scene, 0, 0, 50 + Math.max(64, optionsTextWidth), 32 + 480 * this.scale);
this.pokeballSelectBg.setOrigin(0, 1);
this.pokeballSelectContainer.add(this.pokeballSelectBg);
this.pokeballSelectContainer.add(optionsText);
2023-04-01 22:59:07 -04:00
optionsText.setOrigin(0, 0);
optionsText.setPositionRelative(this.pokeballSelectBg, 42, 9);
optionsText.setLineSpacing(this.scale * 72);
2023-04-01 22:59:07 -04:00
this.countsText = addTextObject(this.scene, 0, 0, "", TextStyle.WINDOW, { maxLines: 5 });
2023-04-01 22:59:07 -04:00
this.countsText.setPositionRelative(this.pokeballSelectBg, 18, 9);
this.countsText.setLineSpacing(this.scale * 72);
2023-04-01 22:59:07 -04:00
this.pokeballSelectContainer.add(this.countsText);
this.setCursor(0);
}
2023-12-30 18:41:25 -05:00
show(args: any[]): boolean {
2023-04-01 22:59:07 -04:00
super.show(args);
this.updateCounts();
this.pokeballSelectContainer.setVisible(true);
this.setCursor(this.cursor);
2023-12-30 18:41:25 -05:00
return true;
2023-04-01 22:59:07 -04:00
}
processInput(button: Button): boolean {
2023-04-01 22:59:07 -04:00
const ui = this.getUi();
let success = false;
const pokeballTypeCount = Object.keys(this.scene.pokeballCounts).length;
2023-04-01 22:59:07 -04:00
if (button === Button.ACTION || button === Button.CANCEL) {
const commandPhase = this.scene.getCurrentPhase() as CommandPhase;
2023-04-01 22:59:07 -04:00
success = true;
if (button === Button.ACTION && this.cursor < pokeballTypeCount) {
if (this.scene.pokeballCounts[this.cursor]) {
if (commandPhase.handleCommand(Command.BALL, this.cursor)) {
this.scene.ui.setMode(Mode.COMMAND, commandPhase.getFieldIndex());
2023-04-30 00:51:33 -04:00
this.scene.ui.setMode(Mode.MESSAGE);
success = true;
}
} else {
2023-04-01 22:59:07 -04:00
ui.playError();
}
2023-04-01 22:59:07 -04:00
} else {
ui.setMode(Mode.COMMAND, commandPhase.getFieldIndex());
2023-04-01 22:59:07 -04:00
success = true;
}
} else {
switch (button) {
case Button.UP:
success = this.setCursor(this.cursor ? this.cursor - 1 : pokeballTypeCount);
break;
case Button.DOWN:
success = this.setCursor(this.cursor < pokeballTypeCount ? this.cursor + 1 : 0);
break;
2023-04-01 22:59:07 -04:00
}
}
if (success) {
2023-04-01 22:59:07 -04:00
ui.playSelect();
}
return success;
2023-04-01 22:59:07 -04:00
}
updateCounts() {
this.countsText.setText(Object.values(this.scene.pokeballCounts).map(c => `x${c}`).join("\n"));
2023-04-01 22:59:07 -04:00
}
setCursor(cursor: integer): boolean {
const ret = super.setCursor(cursor);
if (!this.cursorObj) {
this.cursorObj = this.scene.add.image(0, 0, "cursor");
2023-04-01 22:59:07 -04:00
this.pokeballSelectContainer.add(this.cursorObj);
}
this.cursorObj.setScale(this.scale * 6);
this.cursorObj.setPositionRelative(this.pokeballSelectBg, 12, 15 + (6 + this.cursor * 96) * this.scale);
2023-04-01 22:59:07 -04:00
return ret;
}
clear() {
super.clear();
this.pokeballSelectContainer.setVisible(false);
this.eraseCursor();
}
eraseCursor() {
if (this.cursorObj) {
2023-04-01 22:59:07 -04:00
this.cursorObj.destroy();
}
2023-04-01 22:59:07 -04:00
this.cursorObj = null;
}
}