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