From cffcf9afd0563082276f6d423cf6564fe14aecb0 Mon Sep 17 00:00:00 2001 From: Greenlamp Date: Sat, 11 May 2024 13:33:13 +0200 Subject: [PATCH] cleaned up a bit the code + added some more config --- src/configs/pad_dualshock.ts | 11 +++++++++++ src/configs/pad_generic.ts | 12 ++++++++++-- src/configs/pad_unlicensedSNES.ts | 7 ++++++- src/inputs-controller.ts | 18 +++++++++--------- src/system/settings-gamepad.ts | 6 +++++- 5 files changed, 41 insertions(+), 13 deletions(-) diff --git a/src/configs/pad_dualshock.ts b/src/configs/pad_dualshock.ts index 58fbf31b7..353027426 100644 --- a/src/configs/pad_dualshock.ts +++ b/src/configs/pad_dualshock.ts @@ -48,6 +48,17 @@ const pad_dualshock = { setting: { RC_S: SettingGamepad.Button_Action, RC_E: SettingGamepad.Button_Cancel, + RC_W: SettingGamepad.Button_Cycle_Nature, + RC_N: SettingGamepad.Button_Cycle_Variant, + START: SettingGamepad.Button_Menu, + SELECT: SettingGamepad.Button_Stats, + LB: SettingGamepad.Button_Cycle_Form, + RB: SettingGamepad.Button_Cycle_Shiny, + LT: SettingGamepad.Button_Cycle_Gender, + RT: SettingGamepad.Button_Cycle_Ability, + LS: SettingGamepad.Button_Speed_Up, + RS: SettingGamepad.Button_Slow_Down, + TOUCH: SettingGamepad.Button_Submit, }, default: { RC_S: Button.ACTION, diff --git a/src/configs/pad_generic.ts b/src/configs/pad_generic.ts index 9a7751d25..f64d6c342 100644 --- a/src/configs/pad_generic.ts +++ b/src/configs/pad_generic.ts @@ -42,11 +42,20 @@ const pad_generic = { LC_S: "T_X_Dpad_Down_Alt.png", LC_W: "T_X_Dpad_Left_Alt.png", LC_E: "T_X_Dpad_Right_Alt.png", - MENU: "" }, setting: { RC_S: SettingGamepad.Button_Action, RC_E: SettingGamepad.Button_Cancel, + RC_W: SettingGamepad.Button_Cycle_Nature, + RC_N: SettingGamepad.Button_Cycle_Variant, + START: SettingGamepad.Button_Menu, + SELECT: SettingGamepad.Button_Stats, + LB: SettingGamepad.Button_Cycle_Form, + RB: SettingGamepad.Button_Cycle_Shiny, + LT: SettingGamepad.Button_Cycle_Gender, + RT: SettingGamepad.Button_Cycle_Ability, + LS: SettingGamepad.Button_Speed_Up, + RS: SettingGamepad.Button_Slow_Down, }, default: { RC_S: Button.ACTION, @@ -65,7 +74,6 @@ const pad_generic = { LC_S: Button.DOWN, LC_W: Button.LEFT, LC_E: Button.RIGHT, - TOUCH: Button.SUBMIT, } }; diff --git a/src/configs/pad_unlicensedSNES.ts b/src/configs/pad_unlicensedSNES.ts index ca58d7649..8321c364e 100644 --- a/src/configs/pad_unlicensedSNES.ts +++ b/src/configs/pad_unlicensedSNES.ts @@ -38,6 +38,12 @@ const pad_unlicensedSNES = { setting: { RC_S: SettingGamepad.Button_Action, RC_E: SettingGamepad.Button_Cancel, + RC_W: SettingGamepad.Button_Cycle_Nature, + RC_N: SettingGamepad.Button_Cycle_Variant, + START: SettingGamepad.Button_Menu, + SELECT: SettingGamepad.Button_Stats, + LB: SettingGamepad.Button_Cycle_Form, + RB: SettingGamepad.Button_Cycle_Shiny, }, default: { RC_S: Button.ACTION, @@ -52,7 +58,6 @@ const pad_unlicensedSNES = { LC_S: Button.DOWN, LC_W: Button.LEFT, LC_E: Button.RIGHT, - TOUCH: Button.SUBMIT, } }; diff --git a/src/inputs-controller.ts b/src/inputs-controller.ts index 663d31a71..0ae3a592b 100644 --- a/src/inputs-controller.ts +++ b/src/inputs-controller.ts @@ -1,4 +1,3 @@ -import Phaser, {Time} from "phaser"; import * as Utils from "./utils"; import {initTouchControls} from './touch-controls'; import pad_generic from "./configs/pad_generic"; @@ -65,13 +64,14 @@ const repeatInputDelayMillis = 250; */ export class InputsController { private buttonKeys: Phaser.Input.Keyboard.Key[][]; - private gamepads: Array = new Array(); + private gamepads: Array = new Array(); private scene: Phaser.Scene; + private events: Phaser.Events.EventEmitter; private buttonLock: Button; private buttonLock2: Button; private interactions: Map> = new Map(); - private time: Time; + private time: Phaser.Time.Clock; private player; private gamepadSupport: boolean = true; @@ -118,7 +118,7 @@ export class InputsController { * Additionally, it manages the game's behavior when it loses focus to prevent unwanted game actions during this state. */ init(): void { - this.events = new Phaser.Events.EventEmitter(); + this.events = this.scene.game.events; if (localStorage.hasOwnProperty('chosenGamepad')) { this.chosenGamepad = localStorage.getItem('chosenGamepad'); @@ -206,16 +206,16 @@ export class InputsController { for (const b of Utils.getEnumValues(Button).reverse()) { if ( this.interactions.hasOwnProperty(b) && - this.repeatInputDurationJustPassed(b) && + this.repeatInputDurationJustPassed(b as Button) && this.interactions[b].isPressed ) { // Prevents repeating button interactions when gamepad support is disabled. if ( (!this.gamepadSupport && this.interactions[b].source === 'gamepad') || - (this.interactions[b].sourceName !== null && this.interactions[b].sourceName !== this.chosenGamepad) + (this.interactions[b].sourceName && this.interactions[b].sourceName !== this.chosenGamepad) ) { // Deletes the last interaction for a button if gamepad is disabled. - this.delLastProcessedMovementTime(b); + this.delLastProcessedMovementTime(b as Button); return; } // Emits an event for the button press. @@ -223,7 +223,7 @@ export class InputsController { controller_type: this.interactions[b].source, button: b, }); - this.setLastProcessedMovementTime(b, this.interactions[b].source, this.interactions[b].sourceName); + this.setLastProcessedMovementTime(b as Button, this.interactions[b].source, this.interactions[b].sourceName); } } } @@ -572,7 +572,7 @@ export class InputsController { * * Additionally, this method locks the button (by calling `setButtonLock`) to prevent it from being re-processed until it is released, ensuring that each press is handled distinctly. */ - setLastProcessedMovementTime(button: Button, source: String = 'keyboard', sourceName: String): void { + setLastProcessedMovementTime(button: Button, source: String = 'keyboard', sourceName?: String): void { if (!this.interactions.hasOwnProperty(button)) return; this.setButtonLock(button); this.interactions[button].pressTime = this.time.now; diff --git a/src/system/settings-gamepad.ts b/src/system/settings-gamepad.ts index 5922fee7b..ea71d0e12 100644 --- a/src/system/settings-gamepad.ts +++ b/src/system/settings-gamepad.ts @@ -21,6 +21,7 @@ export enum SettingGamepad { Button_Cycle_Variant = "BUTTON_CYCLE_VARIANT", Button_Speed_Up = "BUTTON_SPEED_UP", Button_Slow_Down = "BUTTON_SLOW_DOWN", + Button_Submit = "BUTTON_SUBMIT", } export const settingGamepadOptions: SettingOptions = { @@ -38,7 +39,8 @@ export const settingGamepadOptions: SettingOptions = { [SettingGamepad.Button_Cycle_Nature]: [`KEY ${Button.CYCLE_NATURE.toString()}`, 'Change'], [SettingGamepad.Button_Cycle_Variant]: [`KEY ${Button.CYCLE_VARIANT.toString()}`, 'Change'], [SettingGamepad.Button_Speed_Up]: [`KEY ${Button.SPEED_UP.toString()}`, 'Change'], - [SettingGamepad.Button_Slow_Down]: [`KEY ${Button.SLOW_DOWN.toString()}`, 'Change'] + [SettingGamepad.Button_Slow_Down]: [`KEY ${Button.SLOW_DOWN.toString()}`, 'Change'], + [SettingGamepad.Button_Submit]: [`KEY ${Button.SUBMIT.toString()}`, 'Change'] }; export const settingGamepadDefaults: SettingDefaults = { @@ -57,6 +59,7 @@ export const settingGamepadDefaults: SettingDefaults = { [SettingGamepad.Button_Cycle_Variant]: 0, [SettingGamepad.Button_Speed_Up]: 0, [SettingGamepad.Button_Slow_Down]: 0, + [SettingGamepad.Button_Submit]: 0, }; export const noOptionsCursors: Array = [ @@ -72,6 +75,7 @@ export const noOptionsCursors: Array = [ SettingGamepad.Button_Cycle_Variant, SettingGamepad.Button_Speed_Up, SettingGamepad.Button_Slow_Down, + SettingGamepad.Button_Submit, ]; export function setSettingGamepad(scene: BattleScene, setting: SettingGamepad, value: integer): boolean {