2023-05-18 16:11:06 +01:00
|
|
|
import { BattlerIndex } from "../battle";
|
2024-05-05 15:30:00 +01:00
|
|
|
import BattleScene from "../battle-scene";
|
2024-02-28 16:34:55 +00:00
|
|
|
import { Moves } from "../data/enums/moves";
|
2023-05-18 16:11:06 +01:00
|
|
|
import { Mode } from "./ui";
|
2023-12-21 00:19:23 +00:00
|
|
|
import UiHandler from "./ui-handler";
|
2023-05-18 16:11:06 +01:00
|
|
|
import * as Utils from "../utils";
|
2024-02-28 16:34:55 +00:00
|
|
|
import { getMoveTargets } from "../data/move";
|
2024-05-05 15:30:00 +01:00
|
|
|
import {Button} from "../enums/buttons";
|
2023-05-18 16:11:06 +01:00
|
|
|
|
|
|
|
export type TargetSelectCallback = (cursor: integer) => void;
|
|
|
|
|
|
|
|
export default class TargetSelectUiHandler extends UiHandler {
|
|
|
|
private fieldIndex: integer;
|
|
|
|
private move: Moves;
|
|
|
|
private targetSelectCallback: TargetSelectCallback;
|
|
|
|
|
|
|
|
private targets: BattlerIndex[];
|
|
|
|
private targetFlashTween: Phaser.Tweens.Tween;
|
|
|
|
|
|
|
|
constructor(scene: BattleScene) {
|
|
|
|
super(scene, Mode.TARGET_SELECT);
|
|
|
|
|
|
|
|
this.cursor = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
setup(): void { }
|
|
|
|
|
2023-12-30 23:41:25 +00:00
|
|
|
show(args: any[]): boolean {
|
2023-05-18 16:11:06 +01:00
|
|
|
if (args.length < 3)
|
2023-12-30 23:41:25 +00:00
|
|
|
return false;
|
2023-05-18 16:11:06 +01:00
|
|
|
|
|
|
|
super.show(args);
|
|
|
|
|
|
|
|
this.fieldIndex = args[0] as integer;
|
|
|
|
this.move = args[1] as Moves;
|
|
|
|
this.targetSelectCallback = args[2] as TargetSelectCallback;
|
|
|
|
|
|
|
|
this.targets = getMoveTargets(this.scene.getPlayerField()[this.fieldIndex], this.move).targets;
|
|
|
|
|
|
|
|
if (!this.targets.length)
|
2023-12-30 23:41:25 +00:00
|
|
|
return false;
|
2023-05-18 16:11:06 +01:00
|
|
|
|
|
|
|
this.setCursor(this.targets.indexOf(this.cursor) > -1 ? this.cursor : this.targets[0]);
|
2023-12-30 23:41:25 +00:00
|
|
|
|
|
|
|
return true;
|
2023-05-18 16:11:06 +01:00
|
|
|
}
|
|
|
|
|
2023-11-12 05:31:40 +00:00
|
|
|
processInput(button: Button): boolean {
|
2023-05-18 16:11:06 +01:00
|
|
|
const ui = this.getUi();
|
|
|
|
|
|
|
|
let success = false;
|
|
|
|
|
|
|
|
if (button === Button.ACTION || button === Button.CANCEL) {
|
|
|
|
this.targetSelectCallback(button === Button.ACTION ? this.cursor : -1);
|
|
|
|
success = true;
|
|
|
|
} else {
|
|
|
|
switch (button) {
|
|
|
|
case Button.UP:
|
2023-05-31 20:04:03 +01:00
|
|
|
if (this.cursor < BattlerIndex.ENEMY && this.targets.findIndex(t => t >= BattlerIndex.ENEMY) > -1)
|
2023-05-18 16:11:06 +01:00
|
|
|
success = this.setCursor(this.targets.find(t => t >= BattlerIndex.ENEMY));
|
|
|
|
break;
|
|
|
|
case Button.DOWN:
|
2023-05-31 20:04:03 +01:00
|
|
|
if (this.cursor >= BattlerIndex.ENEMY && this.targets.findIndex(t => t < BattlerIndex.ENEMY) > -1)
|
2023-05-18 16:11:06 +01:00
|
|
|
success = this.setCursor(this.targets.find(t => t < BattlerIndex.ENEMY));
|
|
|
|
break;
|
|
|
|
case Button.LEFT:
|
2023-05-31 20:04:03 +01:00
|
|
|
if (this.cursor % 2 && this.targets.findIndex(t => t === this.cursor - 1) > -1)
|
2023-05-18 16:11:06 +01:00
|
|
|
success = this.setCursor(this.cursor - 1);
|
|
|
|
break;
|
|
|
|
case Button.RIGHT:
|
2023-05-31 20:04:03 +01:00
|
|
|
if (!(this.cursor % 2) && this.targets.findIndex(t => t === this.cursor + 1) > -1)
|
2023-05-18 16:11:06 +01:00
|
|
|
success = this.setCursor(this.cursor + 1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (success)
|
|
|
|
ui.playSelect();
|
2023-11-12 05:31:40 +00:00
|
|
|
|
|
|
|
return success;
|
2023-05-18 16:11:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
setCursor(cursor: integer): boolean {
|
|
|
|
const lastCursor = this.cursor;
|
|
|
|
|
|
|
|
const ret = super.setCursor(cursor);
|
|
|
|
|
|
|
|
if (this.targetFlashTween) {
|
|
|
|
this.targetFlashTween.stop();
|
|
|
|
const lastTarget = this.scene.getField()[lastCursor];
|
|
|
|
if (lastTarget)
|
|
|
|
lastTarget.setAlpha(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
const target = this.scene.getField()[cursor];
|
|
|
|
|
|
|
|
this.targetFlashTween = this.scene.tweens.add({
|
|
|
|
targets: [ target ],
|
|
|
|
alpha: 0,
|
|
|
|
loop: -1,
|
2023-10-26 04:15:54 +01:00
|
|
|
duration: Utils.fixedInt(250),
|
2023-05-18 16:11:06 +01:00
|
|
|
ease: 'Sine.easeIn',
|
|
|
|
yoyo: true,
|
|
|
|
onUpdate: t => {
|
|
|
|
if (target)
|
|
|
|
target.setAlpha(t.getValue());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
eraseCursor() {
|
|
|
|
const target = this.scene.getField()[this.cursor];
|
|
|
|
if (this.targetFlashTween) {
|
|
|
|
this.targetFlashTween.stop();
|
|
|
|
this.targetFlashTween = null;
|
|
|
|
}
|
|
|
|
if (target)
|
|
|
|
target.setAlpha(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
clear() {
|
|
|
|
super.clear();
|
|
|
|
this.eraseCursor();
|
|
|
|
}
|
|
|
|
}
|