added return type for ui-inputs

This commit is contained in:
Greenlamp 2024-05-04 22:52:20 +02:00
parent c6a2d98e4a
commit a84f9f89bf

View File

@ -7,11 +7,14 @@ import {Setting, settingOptions} from "./system/settings";
import SettingsUiHandler from "./ui/settings-ui-handler";
import {Button} from "./enums/buttons";
export interface ActionKeys {
[key in Button]: () => void;
}
export class UiInputs {
private scene: Phaser.Scene;
private events;
private inputsController;
private events: Phaser.Events;
private inputsController: InputsController;
constructor(scene: Phaser.Scene, inputsController: InputsController) {
this.scene = scene;
@ -19,7 +22,7 @@ export class UiInputs {
this.init();
}
init() {
init(): void {
this.events = this.inputsController.events;
this.listenInputs();
}
@ -28,21 +31,22 @@ export class UiInputs {
this.events.on('input_down', (event) => {
const actions = this.getActionsKeyDown();
if (!actions.hasOwnProperty(event.button)) return;
const [inputSuccess, vibrationLength] = actions[event.button]();
if (inputSuccess && this.scene.enableVibration && typeof navigator.vibrate !== 'undefined')
navigator.vibrate(vibrationLength);
actions[event.button]();
}, this);
this.events.on('input_up', (event) => {
const actions = this.getActionsKeyUp();
if (!actions.hasOwnProperty(event.button)) return;
const [inputSuccess, vibrationLength] = actions[event.button]();
if (inputSuccess && this.scene.enableVibration && typeof navigator.vibrate !== 'undefined')
navigator.vibrate(vibrationLength);
actions[event.button]();
}, this);
}
getActionsKeyDown() {
doVibration(inputSuccess: boolean, vibrationLength: number): void {
if (inputSuccess && this.scene.enableVibration && typeof navigator.vibrate !== 'undefined')
navigator.vibrate(vibrationLength);
}
getActionsKeyDown(): ActionKeys {
const actions = {};
actions[Button.UP] = () => this.buttonDirection(Button.UP);
actions[Button.DOWN] = () => this.buttonDirection(Button.DOWN);
@ -64,29 +68,27 @@ export class UiInputs {
return actions;
}
getActionsKeyUp() {
getActionsKeyUp(): ActionKeys {
const actions = {};
actions[Button.STATS] = () => this.buttonStats(false);
return actions;
}
buttonDirection(direction): Array<boolean | number> {
buttonDirection(direction: Button): void {
const inputSuccess = this.scene.ui.processInput(direction);
const vibrationLength = 5;
return [inputSuccess, vibrationLength];
this.doVibration(inputSuccess, vibrationLength);
}
buttonAb(button): Array<boolean | number> {
const inputSuccess = this.scene.ui.processInput(button);
return [inputSuccess, 0];
buttonAb(button: Button): void {
this.scene.ui.processInput(button);
}
buttonTouch(): Array<boolean | number> {
const inputSuccess = this.scene.ui.processInput(Button.SUBMIT) || this.scene.ui.processInput(Button.ACTION);
return [inputSuccess, 0];
buttonTouch(): void {
this.scene.ui.processInput(Button.SUBMIT) || this.scene.ui.processInput(Button.ACTION);
}
buttonStats(pressed = true): Array<boolean | number> {
buttonStats(pressed: boolean = true): void {
if (pressed) {
for (let p of this.scene.getField().filter(p => p?.isActive(true)))
p.toggleStats(true);
@ -94,17 +96,15 @@ export class UiInputs {
for (let p of this.scene.getField().filter(p => p?.isActive(true)))
p.toggleStats(false);
}
return [true, 0];
}
buttonMenu(): Array<boolean | number> {
let inputSuccess;
buttonMenu(): void {
if (this.scene.disableMenu)
return [true, 0];
return;
switch (this.scene.ui?.getMode()) {
case Mode.MESSAGE:
if (!(this.scene.ui.getHandler() as MessageUiHandler).pendingPrompt)
return [true, 0];
return;
case Mode.TITLE:
case Mode.COMMAND:
case Mode.FIGHT:
@ -117,44 +117,38 @@ export class UiInputs {
case Mode.CONFIRM:
case Mode.OPTION_SELECT:
this.scene.ui.setOverlayMode(Mode.MENU);
inputSuccess = true;
break;
case Mode.MENU:
case Mode.SETTINGS:
case Mode.ACHIEVEMENTS:
this.scene.ui.revertMode();
this.scene.playSound('select');
inputSuccess = true;
break;
default:
return [true, 0];
return
}
return [inputSuccess, 0];
}
buttonCycleOption(button): Array<boolean | number> {
let inputSuccess;
buttonCycleOption(button: Button): void {
if (this.scene.ui?.getHandler() instanceof StarterSelectUiHandler) {
inputSuccess = this.scene.ui.processInput(button);
this.scene.ui.processInput(button);
}
return [inputSuccess, 0];
}
buttonSpeedChange(up = true): Array<boolean | number> {
buttonSpeedChange(up = true): void {
if (up) {
if (this.scene.gameSpeed < 5) {
this.scene.gameData.saveSetting(Setting.Game_Speed, settingOptions[Setting.Game_Speed].indexOf(`${this.scene.gameSpeed}x`) + 1);
if (this.scene.ui?.getMode() === Mode.SETTINGS)
(this.scene.ui.getHandler() as SettingsUiHandler).show([]);
}
return [0, 0];
return;
}
if (this.scene.gameSpeed > 1) {
this.scene.gameData.saveSetting(Setting.Game_Speed, Math.max(settingOptions[Setting.Game_Speed].indexOf(`${this.scene.gameSpeed}x`) - 1, 0));
if (this.scene.ui?.getMode() === Mode.SETTINGS)
(this.scene.ui.getHandler() as SettingsUiHandler).show([]);
}
return [0, 0];
}
}