2024-02-22 04:57:49 +00:00
|
|
|
import { CommandPhase } from "../phases";
|
2024-05-03 17:59:10 +01:00
|
|
|
import BattleScene from "../battle-scene";
|
2024-02-29 04:13:05 +00:00
|
|
|
import { getPokeballName } from "../data/pokeball";
|
2023-04-20 20:46:05 +01:00
|
|
|
import { addTextObject, TextStyle } from "./text";
|
2023-04-02 03:59:07 +01:00
|
|
|
import { Command } from "./command-ui-handler";
|
|
|
|
import { Mode } from "./ui";
|
2023-12-21 00:19:23 +00:00
|
|
|
import UiHandler from "./ui-handler";
|
2024-04-01 02:14:35 +01:00
|
|
|
import { addWindow } from "./ui-theme";
|
2024-05-03 17:59:10 +01:00
|
|
|
import {Button} from "#app/inputs-controller";
|
2023-04-02 03:59:07 +01:00
|
|
|
|
|
|
|
export default class BallUiHandler extends UiHandler {
|
|
|
|
private pokeballSelectContainer: Phaser.GameObjects.Container;
|
2023-06-16 17:13:52 +01:00
|
|
|
private pokeballSelectBg: Phaser.GameObjects.NineSlice;
|
2023-04-02 03:59:07 +01:00
|
|
|
private countsText: Phaser.GameObjects.Text;
|
|
|
|
|
|
|
|
private cursorObj: Phaser.GameObjects.Image;
|
|
|
|
|
|
|
|
constructor(scene: BattleScene) {
|
|
|
|
super(scene, Mode.BALL);
|
|
|
|
}
|
|
|
|
|
|
|
|
setup() {
|
|
|
|
const ui = this.getUi();
|
|
|
|
|
|
|
|
this.pokeballSelectContainer = this.scene.add.container((this.scene.game.canvas.width / 6) - 115, -49);
|
|
|
|
this.pokeballSelectContainer.setVisible(false);
|
|
|
|
ui.add(this.pokeballSelectContainer);
|
|
|
|
|
2024-02-29 04:21:31 +00:00
|
|
|
this.pokeballSelectBg = addWindow(this.scene, 0, 0, 114, 112);
|
2023-04-02 03:59:07 +01:00
|
|
|
this.pokeballSelectBg.setOrigin(0, 1);
|
|
|
|
this.pokeballSelectContainer.add(this.pokeballSelectBg);
|
|
|
|
|
|
|
|
let optionsTextContent = '';
|
|
|
|
|
|
|
|
for (let pb = 0; pb < Object.keys(this.scene.pokeballCounts).length; pb++)
|
|
|
|
optionsTextContent += `${getPokeballName(pb)}\n`;
|
2023-10-18 23:01:15 +01:00
|
|
|
optionsTextContent += 'Cancel';
|
2023-04-02 03:59:07 +01:00
|
|
|
const optionsText = addTextObject(this.scene, 0, 0, optionsTextContent, TextStyle.WINDOW, { align: 'right', maxLines: 6 });
|
|
|
|
optionsText.setOrigin(0, 0);
|
|
|
|
optionsText.setPositionRelative(this.pokeballSelectBg, 42, 9);
|
|
|
|
optionsText.setLineSpacing(12);
|
|
|
|
this.pokeballSelectContainer.add(optionsText);
|
|
|
|
|
|
|
|
this.countsText = addTextObject(this.scene, 0, 0, '', TextStyle.WINDOW, { maxLines: 5 });
|
|
|
|
this.countsText.setPositionRelative(this.pokeballSelectBg, 18, 9);
|
|
|
|
this.countsText.setLineSpacing(12);
|
|
|
|
this.pokeballSelectContainer.add(this.countsText);
|
|
|
|
|
|
|
|
this.setCursor(0);
|
|
|
|
}
|
|
|
|
|
2023-12-30 23:41:25 +00:00
|
|
|
show(args: any[]): boolean {
|
2023-04-02 03:59:07 +01:00
|
|
|
super.show(args);
|
|
|
|
|
|
|
|
this.updateCounts();
|
|
|
|
this.pokeballSelectContainer.setVisible(true);
|
|
|
|
this.setCursor(this.cursor);
|
2023-12-30 23:41:25 +00:00
|
|
|
|
|
|
|
return true;
|
2023-04-02 03:59:07 +01:00
|
|
|
}
|
|
|
|
|
2023-11-12 05:31:40 +00:00
|
|
|
processInput(button: Button): boolean {
|
2023-04-02 03:59:07 +01:00
|
|
|
const ui = this.getUi();
|
|
|
|
|
|
|
|
let success = false;
|
|
|
|
|
2023-10-27 22:43:53 +01:00
|
|
|
const pokeballTypeCount = Object.keys(this.scene.pokeballCounts).length;
|
2023-04-02 03:59:07 +01:00
|
|
|
|
2023-04-11 05:24:55 +01:00
|
|
|
if (button === Button.ACTION || button === Button.CANCEL) {
|
2023-10-20 16:38:41 +01:00
|
|
|
const commandPhase = this.scene.getCurrentPhase() as CommandPhase;
|
2023-04-02 03:59:07 +01:00
|
|
|
success = true;
|
2023-04-11 05:24:55 +01:00
|
|
|
if (button === Button.ACTION && this.cursor < pokeballTypeCount) {
|
2023-10-27 22:43:53 +01:00
|
|
|
if (this.scene.pokeballCounts[this.cursor]) {
|
2023-10-20 16:38:41 +01:00
|
|
|
if (commandPhase.handleCommand(Command.BALL, this.cursor)) {
|
|
|
|
this.scene.ui.setMode(Mode.COMMAND, commandPhase.getFieldIndex());
|
2023-04-30 05:51:33 +01:00
|
|
|
this.scene.ui.setMode(Mode.MESSAGE);
|
|
|
|
success = true;
|
|
|
|
}
|
2023-04-02 03:59:07 +01:00
|
|
|
} else
|
|
|
|
ui.playError();
|
|
|
|
} else {
|
2023-10-20 16:38:41 +01:00
|
|
|
ui.setMode(Mode.COMMAND, commandPhase.getFieldIndex());
|
2023-04-02 03:59:07 +01:00
|
|
|
success = true;
|
|
|
|
}
|
|
|
|
} else {
|
2023-04-11 05:24:55 +01:00
|
|
|
switch (button) {
|
|
|
|
case Button.UP:
|
2023-04-02 03:59:07 +01:00
|
|
|
success = this.setCursor(this.cursor ? this.cursor - 1 : pokeballTypeCount);
|
|
|
|
break;
|
2023-04-11 05:24:55 +01:00
|
|
|
case Button.DOWN:
|
2023-04-02 03:59:07 +01:00
|
|
|
success = this.setCursor(this.cursor < pokeballTypeCount ? this.cursor + 1 : 0);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (success)
|
|
|
|
ui.playSelect();
|
2023-11-12 05:31:40 +00:00
|
|
|
|
|
|
|
return success;
|
2023-04-02 03:59:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
updateCounts() {
|
2023-10-27 22:43:53 +01:00
|
|
|
this.countsText.setText(Object.values(this.scene.pokeballCounts).map(c => `x${c}`).join('\n'));
|
2023-04-02 03:59:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
setCursor(cursor: integer): boolean {
|
|
|
|
const ret = super.setCursor(cursor);
|
|
|
|
|
|
|
|
if (!this.cursorObj) {
|
|
|
|
this.cursorObj = this.scene.add.image(0, 0, 'cursor');
|
|
|
|
this.pokeballSelectContainer.add(this.cursorObj);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.cursorObj.setPositionRelative(this.pokeballSelectBg, 12, 17 + 16 * this.cursor);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
clear() {
|
|
|
|
super.clear();
|
|
|
|
this.pokeballSelectContainer.setVisible(false);
|
|
|
|
this.eraseCursor();
|
|
|
|
}
|
|
|
|
|
|
|
|
eraseCursor() {
|
|
|
|
if (this.cursorObj)
|
|
|
|
this.cursorObj.destroy();
|
|
|
|
this.cursorObj = null;
|
|
|
|
}
|
|
|
|
}
|