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

163 lines
4.4 KiB
TypeScript
Raw Normal View History

import BattleScene, { Button } from "../battle-scene";
2023-04-20 20:46:05 +01:00
import { addTextObject, TextStyle } from "./text";
import { Type } from "../data/type";
2023-03-28 19:54:52 +01:00
import { Command } from "./command-ui-handler";
2023-04-01 01:19:57 +01:00
import { Mode } from "./ui";
import UiHandler from "./ui-handler";
2023-04-01 01:19:57 +01:00
import * as Utils from "../utils";
2023-04-10 19:12:01 +01:00
import { CommandPhase } from "../battle-phases";
2023-03-28 19:54:52 +01:00
export default class FightUiHandler extends UiHandler {
private movesContainer: Phaser.GameObjects.Container;
2023-04-01 01:19:57 +01:00
private typeIcon: Phaser.GameObjects.Sprite;
private ppText: Phaser.GameObjects.Text;
2023-03-28 19:54:52 +01:00
private cursorObj: Phaser.GameObjects.Image;
protected fieldIndex: integer = 0;
protected cursor2: integer = 0;
2023-03-28 19:54:52 +01:00
constructor(scene: BattleScene) {
super(scene, Mode.FIGHT);
}
setup() {
const ui = this.getUi();
2023-04-01 01:19:57 +01:00
this.movesContainer = this.scene.add.container(18, -38.7);
ui.add(this.movesContainer);
2023-03-28 19:54:52 +01:00
2023-04-01 01:19:57 +01:00
this.typeIcon = this.scene.add.sprite((this.scene.game.canvas.width / 6) - 33, -31, 'types', 'unknown');
this.typeIcon.setVisible(false);
ui.add(this.typeIcon);
this.ppText = addTextObject(this.scene, (this.scene.game.canvas.width / 6) - 18, -15.5, ' / ', TextStyle.WINDOW);
this.ppText.setOrigin(1, 0.5);
this.ppText.setVisible(false);
ui.add(this.ppText);
2023-03-28 19:54:52 +01:00
}
show(args: any[]) {
super.show(args);
this.fieldIndex = args.length ? args[0] as integer : 0;
2023-03-28 19:54:52 +01:00
const messageHandler = this.getUi().getMessageHandler();
messageHandler.bg.setTexture('bg_fight');
this.setCursor(this.getCursor());
2023-03-28 19:54:52 +01:00
this.displayMoves();
}
processInput(button: Button): boolean {
2023-03-28 19:54:52 +01:00
const ui = this.getUi();
let success = false;
const cursor = this.getCursor();
if (button === Button.CANCEL || button === Button.ACTION) {
if (button === Button.ACTION) {
if ((this.scene.getCurrentPhase() as CommandPhase).handleCommand(Command.FIGHT, cursor, false))
2023-03-28 19:54:52 +01:00
success = true;
2023-04-13 02:44:12 +01:00
else
ui.playError();
2023-03-28 19:54:52 +01:00
} else {
ui.setMode(Mode.COMMAND, this.fieldIndex);
2023-03-28 19:54:52 +01:00
success = true;
}
} else {
switch (button) {
case Button.UP:
if (cursor >= 2)
success = this.setCursor(cursor - 2);
2023-03-28 19:54:52 +01:00
break;
case Button.DOWN:
if (cursor < 2)
success = this.setCursor(cursor + 2);
2023-03-28 19:54:52 +01:00
break;
case Button.LEFT:
if (cursor % 2 === 1)
success = this.setCursor(cursor - 1);
2023-03-28 19:54:52 +01:00
break;
case Button.RIGHT:
if (cursor % 2 === 0)
success = this.setCursor(cursor + 1);
2023-03-28 19:54:52 +01:00
break;
}
}
if (success)
ui.playSelect();
return success;
2023-03-28 19:54:52 +01:00
}
getCursor(): integer {
return !this.fieldIndex ? this.cursor : this.cursor2;
}
2023-03-28 19:54:52 +01:00
setCursor(cursor: integer): boolean {
const ui = this.getUi();
const changed = this.getCursor() !== cursor;
if (changed) {
if (!this.fieldIndex)
this.cursor = cursor;
else
this.cursor2 = cursor;
}
2023-03-28 19:54:52 +01:00
if (!this.cursorObj) {
this.cursorObj = this.scene.add.image(0, 0, 'cursor');
ui.add(this.cursorObj);
}
const moveset = (this.scene.getCurrentPhase() as CommandPhase).getPokemon().getMoveset();
2023-04-04 04:38:31 +01:00
const hasMove = cursor < moveset.length;
if (hasMove) {
const pokemonMove = moveset[cursor];
this.typeIcon.setTexture('types', Type[pokemonMove.getMove().type].toLowerCase());
2023-04-01 01:19:57 +01:00
2023-04-04 04:38:31 +01:00
const maxPP = pokemonMove.getMove().pp + pokemonMove.ppUp;
const pp = maxPP - pokemonMove.ppUsed;
this.ppText.setText(`${Utils.padInt(pp, 2, ' ')}/${Utils.padInt(maxPP, 2, ' ')}`);
}
2023-04-01 01:19:57 +01:00
2023-04-04 04:38:31 +01:00
this.typeIcon.setVisible(hasMove);
this.ppText.setVisible(hasMove);
2023-04-01 01:19:57 +01:00
2023-03-28 19:54:52 +01:00
this.cursorObj.setPosition(13 + (cursor % 2 === 1 ? 100 : 0), -31 + (cursor >= 2 ? 15 : 0));
return changed;
2023-03-28 19:54:52 +01:00
}
displayMoves() {
const moveset = (this.scene.getCurrentPhase() as CommandPhase).getPokemon().getMoveset();
2023-03-28 19:54:52 +01:00
for (let m = 0; m < 4; m++) {
const moveText = addTextObject(this.scene, m % 2 === 0 ? 0 : 100, m < 2 ? 0 : 16, '-', TextStyle.WINDOW);
if (m < moveset.length)
moveText.setText(moveset[m].getName());
this.movesContainer.add(moveText);
}
}
clear() {
super.clear();
this.clearMoves();
2023-04-01 01:19:57 +01:00
this.typeIcon.setVisible(false);
this.ppText.setVisible(false);
2023-03-28 19:54:52 +01:00
this.eraseCursor();
}
clearMoves() {
this.movesContainer.removeAll(true);
}
eraseCursor() {
if (this.cursorObj)
this.cursorObj.destroy();
this.cursorObj = null;
}
}