diff --git a/src/battle-scene.ts b/src/battle-scene.ts index a9796698f3f..f6e5a9c5948 100644 --- a/src/battle-scene.ts +++ b/src/battle-scene.ts @@ -200,7 +200,7 @@ export default class BattleScene extends SceneBase { this.load.atlas(key, `images/pokemon/${variant ? 'variant/' : ''}${experimental ? 'exp/' : ''}${atlasPath}.png`, `images/pokemon/${variant ? 'variant/' : ''}${experimental ? 'exp/' : ''}${atlasPath}.json`); } - async preload() { + async preload() { this.load.scenePlugin('inputController', InputsController); this.load.scenePlugin('uiInputs', UiInputs); if (DEBUG_RNG) { diff --git a/src/inputs-controller.ts b/src/inputs-controller.ts index 881c486c967..441649a5199 100644 --- a/src/inputs-controller.ts +++ b/src/inputs-controller.ts @@ -1,4 +1,4 @@ -import Phaser, { Time } from "phaser"; +import Phaser, {Time} from "phaser"; import * as Utils from "./utils"; import {initTouchControls} from './touch-controls'; import pad_generic from "#app/configs/pad_generic"; @@ -26,19 +26,20 @@ export enum Button { SPEED_UP, SLOW_DOWN } + const repeatInputDelayMillis = 250; export class InputsController extends Phaser.Plugins.ScenePlugin { - private game: Phaser.Game; - private buttonKeys: Phaser.Input.Keyboard.Key[][]; - private gamepads: Array = new Array(); - private scene: Phaser.Scene; + private game: Phaser.Game; + private buttonKeys: Phaser.Input.Keyboard.Key[][]; + private gamepads: Array = new Array(); + private scene: Phaser.Scene; - // buttonLock ensures only a single movement key is firing repeated inputs - // (i.e. by holding down a button) at a time - private buttonLock: Button; - private interactions: Map> = new Map(); - private time: Time; + // buttonLock ensures only a single movement key is firing repeated inputs + // (i.e. by holding down a button) at a time + private buttonLock: Button; + private interactions: Map> = new Map(); + private time: Time; constructor(scene: Phaser.Scene, pluginManager: Phaser.Plugins.PluginManager, pluginKey: string) { super(scene, pluginManager, pluginKey); @@ -57,7 +58,7 @@ export class InputsController extends Phaser.Plugins.ScenePlugin { delete this.interactions[Button.MENU]; } - boot() { + boot() { this.eventEmitter = this.systems.events; this.events = new Phaser.Events.EventEmitter(); this.game.events.on(Phaser.Core.Events.STEP, this.update, this); @@ -79,13 +80,13 @@ export class InputsController extends Phaser.Plugins.ScenePlugin { this.systems.input.gamepad.on('down', this.gamepadButtonDown, this); this.systems.input.gamepad.on('up', this.gamepadButtonUp, this); - } + } - // Keyboard - this.setupKeyboardControls(); - } + // Keyboard + this.setupKeyboardControls(); + } - update() { + update() { for (const b of Utils.getEnumValues(Button)) { if (!this.interactions.hasOwnProperty(b)) continue; if (this.repeatInputDurationJustPassed(b)) { @@ -96,17 +97,17 @@ export class InputsController extends Phaser.Plugins.ScenePlugin { this.setLastProcessedMovementTime(b); } } - } + } - setupGamepad(thisGamepad): void { + setupGamepad(thisGamepad): void { let gamepadID = thisGamepad.id.toLowerCase(); const mappedPad = this.mapGamepad(gamepadID); this.player = { 'mapping': mappedPad.gamepadMapping, } - } + } - refreshGamepads() :void { + refreshGamepads(): void { // Sometimes, gamepads are undefined. For some reason. this.gamepads = this.systems.input.gamepad.gamepads.filter(function (el) { return el != null; @@ -115,9 +116,9 @@ export class InputsController extends Phaser.Plugins.ScenePlugin { for (const [index, thisGamepad] of this.gamepads.entries()) { thisGamepad.index = index; // Overwrite the gamepad index, in case we had undefined gamepads earlier } - } + } - getActionGamepadMapping() { + getActionGamepadMapping() { const gamepadMapping = {}; gamepadMapping[this.player.mapping.LC_N] = Button.UP; gamepadMapping[this.player.mapping.LC_S] = Button.DOWN; @@ -138,9 +139,9 @@ export class InputsController extends Phaser.Plugins.ScenePlugin { gamepadMapping[this.player.mapping.RS] = Button.SLOW_DOWN; return gamepadMapping; - } + } - gamepadButtonDown(pad, button, value): void { + gamepadButtonDown(pad, button, value): void { const actionMapping = this.getActionGamepadMapping(); const buttonDown = actionMapping.hasOwnProperty(button.index) && actionMapping[button.index]; if (buttonDown !== undefined) { @@ -150,9 +151,9 @@ export class InputsController extends Phaser.Plugins.ScenePlugin { }); this.setLastProcessedMovementTime(buttonDown); } - } + } - gamepadButtonUp(pad, button, value): void { + gamepadButtonUp(pad, button, value): void { const actionMapping = this.getActionGamepadMapping(); const buttonUp = actionMapping.hasOwnProperty(button.index) && actionMapping[button.index]; if (buttonUp !== undefined) { @@ -162,9 +163,9 @@ export class InputsController extends Phaser.Plugins.ScenePlugin { }); this.delLastProcessedMovementTime(buttonUp); } - } + } - setupKeyboardControls(): void { + setupKeyboardControls(): void { const keyCodes = Phaser.Input.Keyboard.KeyCodes; const keyConfig = { [Button.UP]: [keyCodes.UP, keyCodes.W], @@ -198,9 +199,9 @@ export class InputsController extends Phaser.Plugins.ScenePlugin { initTouchControls(mobileKeyConfig); this.listenInputKeyboard(); - } + } - listenInputKeyboard(): void { + listenInputKeyboard(): void { this.buttonKeys.forEach((row, index) => { for (const key of row) { key.on('down', () => { @@ -219,7 +220,7 @@ export class InputsController extends Phaser.Plugins.ScenePlugin { }); } }); - } + } mapGamepad(id) { id = id.toLowerCase(); @@ -227,23 +228,21 @@ export class InputsController extends Phaser.Plugins.ScenePlugin { if (id.includes('081f') && id.includes('e401')) { padConfig = pad_unlicensedSNES; - } - else if (id.includes('xbox') && id.includes('360')) { + } else if (id.includes('xbox') && id.includes('360')) { padConfig = pad_xbox360; - } - else if (id.includes('054c')) { + } else if (id.includes('054c')) { padConfig = pad_dualshock; } return padConfig; } - /** - * repeatInputDurationJustPassed returns true if @param button has been held down long - * enough to fire a repeated input. A button must claim the buttonLock before - * firing a repeated input - this is to prevent multiple buttons from firing repeatedly. - */ - repeatInputDurationJustPassed(button: Button): boolean { + /** + * repeatInputDurationJustPassed returns true if @param button has been held down long + * enough to fire a repeated input. A button must claim the buttonLock before + * firing a repeated input - this is to prevent multiple buttons from firing repeatedly. + */ + repeatInputDurationJustPassed(button: Button): boolean { if (this.buttonLock === null || this.buttonLock !== button) { return false; } @@ -251,17 +250,17 @@ export class InputsController extends Phaser.Plugins.ScenePlugin { this.buttonLock = null; return true; } - } + } - setLastProcessedMovementTime(button: Button): void { + setLastProcessedMovementTime(button: Button): void { if (!this.interactions.hasOwnProperty(button)) return; this.buttonLock = button; this.interactions[button].pressTime = this.time.now; - } + } - delLastProcessedMovementTime(button: Button): void { + delLastProcessedMovementTime(button: Button): void { if (!this.interactions.hasOwnProperty(button)) return; this.buttonLock = null; this.interactions[button].pressTime = null; - } + } } \ No newline at end of file diff --git a/src/ui-inputs.ts b/src/ui-inputs.ts index 68168845a03..bcdcc34b1fa 100644 --- a/src/ui-inputs.ts +++ b/src/ui-inputs.ts @@ -19,7 +19,7 @@ export class UiInputs extends Phaser.Plugins.ScenePlugin { this.events = this.scene.inputController.events } - boot() { + boot() { this.listenInputs(); } @@ -41,7 +41,7 @@ export class UiInputs extends Phaser.Plugins.ScenePlugin { }, this); } - getActionsKeyDown() { + getActionsKeyDown() { const actions = {}; actions[Button.UP] = () => this.buttonDirection(Button.UP); actions[Button.DOWN] = () => this.buttonDirection(Button.DOWN); @@ -61,31 +61,31 @@ export class UiInputs extends Phaser.Plugins.ScenePlugin { actions[Button.SPEED_UP] = () => this.buttonSpeedChange(); actions[Button.SLOW_DOWN] = () => this.buttonSpeedChange(false); return actions; - } + } - getActionsKeyUp() { + getActionsKeyUp() { const actions = {}; actions[Button.STATS] = () => this.buttonStats(false); return actions; - } + } - buttonDirection(direction): Array { + buttonDirection(direction): Array { const inputSuccess = this.scene.ui.processInput(direction); const vibrationLength = 5; return [inputSuccess, vibrationLength]; - } + } - buttonAb(button): Array { + buttonAb(button): Array { const inputSuccess = this.scene.ui.processInput(button); return [inputSuccess, 0]; - } + } - buttonTouch(): Array { + buttonTouch(): Array { const inputSuccess = this.scene.ui.processInput(Button.SUBMIT) || this.scene.ui.processInput(Button.ACTION); return [inputSuccess, 0]; - } + } - buttonStats(pressed = true): Array { + buttonStats(pressed = true): Array { if (pressed) { for (let p of this.scene.getField().filter(p => p?.isActive(true))) p.toggleStats(true); @@ -94,9 +94,9 @@ export class UiInputs extends Phaser.Plugins.ScenePlugin { p.toggleStats(false); } return [true, 0]; - } + } - buttonMenu(): Array { + buttonMenu(): Array { let inputSuccess; if (this.scene.disableMenu) return [true, 0]; @@ -129,17 +129,17 @@ export class UiInputs extends Phaser.Plugins.ScenePlugin { return [true, 0]; } return [inputSuccess, 0]; - } + } - buttonCycleOption(button): Array { + buttonCycleOption(button): Array { let inputSuccess; if (this.scene.ui?.getHandler() instanceof StarterSelectUiHandler) { inputSuccess = this.scene.ui.processInput(button); } return [inputSuccess, 0]; - } + } - buttonSpeedChange(up= true): Array { + buttonSpeedChange(up = true): Array { if (up) { if (this.scene.gameSpeed < 5) { this.scene.gameData.saveSetting(Setting.Game_Speed, settingOptions[Setting.Game_Speed].indexOf(`${this.scene.gameSpeed}x`) + 1); @@ -154,6 +154,6 @@ export class UiInputs extends Phaser.Plugins.ScenePlugin { (this.scene.ui.getHandler() as SettingsUiHandler).show([]); } return [0, 0]; - } + } } \ No newline at end of file