added full TSDoc, helped by chatGPT, refined every entry manually of course
parent
92c493fe83
commit
58845410e6
|
@ -23,13 +23,33 @@ export interface ActionGamepadMapping {
|
||||||
|
|
||||||
const repeatInputDelayMillis = 250;
|
const repeatInputDelayMillis = 250;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Manages and handles all input controls for the game, including keyboard and gamepad interactions.
|
||||||
|
*
|
||||||
|
* @remarks
|
||||||
|
* This class is designed to centralize input management across the game. It facilitates the setup,
|
||||||
|
* configuration, and handling of all game inputs, making it easier to manage various input devices
|
||||||
|
* such as keyboards and gamepads. The class provides methods for setting up input devices, handling
|
||||||
|
* their events, and responding to changes in input state (e.g., button presses, releases).
|
||||||
|
*
|
||||||
|
* The `InputsController` class also includes mechanisms to handle game focus events to ensure input
|
||||||
|
* states are correctly reset and managed when the game loses or regains focus, maintaining robust
|
||||||
|
* and responsive control handling throughout the game's lifecycle.
|
||||||
|
*
|
||||||
|
* Key responsibilities include:
|
||||||
|
* - Initializing and configuring gamepad and keyboard controls.
|
||||||
|
* - Emitting events related to specific input actions.
|
||||||
|
* - Responding to external changes such as gamepad connection/disconnection.
|
||||||
|
* - Managing game state transitions in response to input events, particularly focus loss and recovery.
|
||||||
|
*
|
||||||
|
* Usage of this class is intended to simplify input management across various parts of the game,
|
||||||
|
* providing a unified interface for all input-related interactions.
|
||||||
|
*/
|
||||||
export class InputsController {
|
export class InputsController {
|
||||||
private buttonKeys: Phaser.Input.Keyboard.Key[][];
|
private buttonKeys: Phaser.Input.Keyboard.Key[][];
|
||||||
private gamepads: Array<string> = new Array();
|
private gamepads: Array<string> = new Array();
|
||||||
private scene: Phaser.Scene;
|
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 buttonLock: Button;
|
||||||
private buttonLock2: Button;
|
private buttonLock2: Button;
|
||||||
private interactions: Map<Button, Map<string, boolean>> = new Map();
|
private interactions: Map<Button, Map<string, boolean>> = new Map();
|
||||||
|
@ -38,6 +58,17 @@ export class InputsController {
|
||||||
|
|
||||||
private gamepadSupport: boolean = true;
|
private gamepadSupport: boolean = true;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initializes a new instance of the game control system, setting up initial state and configurations.
|
||||||
|
*
|
||||||
|
* @param scene - The Phaser scene associated with this instance.
|
||||||
|
*
|
||||||
|
* @remarks
|
||||||
|
* This constructor initializes the game control system with necessary setups for handling inputs.
|
||||||
|
* It prepares an interactions array indexed by button identifiers and configures default states for each button.
|
||||||
|
* Specific buttons like MENU and STATS are set not to repeat their actions.
|
||||||
|
* It concludes by calling the `init` method to complete the setup.
|
||||||
|
*/
|
||||||
constructor(scene: Phaser.Scene) {
|
constructor(scene: Phaser.Scene) {
|
||||||
this.scene = scene;
|
this.scene = scene;
|
||||||
this.time = this.scene.time;
|
this.time = this.scene.time;
|
||||||
|
@ -56,9 +87,16 @@ export class InputsController {
|
||||||
this.init();
|
this.init();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets up event handlers and initializes gamepad and keyboard controls.
|
||||||
|
*
|
||||||
|
* @remarks
|
||||||
|
* This method configures event listeners for both gamepad and keyboard inputs.
|
||||||
|
* It handles gamepad connections/disconnections and button press events, and ensures keyboard controls are set up.
|
||||||
|
* Additionally, it manages the game's behavior when it loses focus to prevent unwanted game actions during this state.
|
||||||
|
*/
|
||||||
init(): void {
|
init(): void {
|
||||||
this.events = new Phaser.Events.EventEmitter();
|
this.events = new Phaser.Events.EventEmitter();
|
||||||
// Handle the game losing focus
|
|
||||||
this.scene.game.events.on(Phaser.Core.Events.BLUR, () => {
|
this.scene.game.events.on(Phaser.Core.Events.BLUR, () => {
|
||||||
this.loseFocus()
|
this.loseFocus()
|
||||||
})
|
})
|
||||||
|
@ -86,13 +124,25 @@ export class InputsController {
|
||||||
this.setupKeyboardControls();
|
this.setupKeyboardControls();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handles actions to take when the game loses focus, such as deactivating pressed keys.
|
||||||
|
*
|
||||||
|
* @remarks
|
||||||
|
* This method is triggered when the game or the browser tab loses focus. It ensures that any keys pressed are deactivated to prevent stuck keys affecting gameplay when the game is not active.
|
||||||
|
*/
|
||||||
loseFocus(): void {
|
loseFocus(): void {
|
||||||
this.deactivatePressedKey();
|
this.deactivatePressedKey();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Enables or disables support for gamepad input.
|
||||||
|
*
|
||||||
|
* @param value - A boolean indicating whether gamepad support should be enabled (true) or disabled (false).
|
||||||
|
*
|
||||||
|
* @remarks
|
||||||
|
* This method toggles gamepad support. If disabled, it also ensures that all currently pressed gamepad buttons are deactivated to avoid stuck inputs.
|
||||||
|
*/
|
||||||
setGamepadSupport(value: boolean): void {
|
setGamepadSupport(value: boolean): void {
|
||||||
// value is true if we activate gamepad
|
|
||||||
// value is false if we deactivate gamepad
|
|
||||||
if (value) {
|
if (value) {
|
||||||
this.gamepadSupport = true;
|
this.gamepadSupport = true;
|
||||||
} else {
|
} else {
|
||||||
|
@ -102,24 +152,33 @@ export class InputsController {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Updates the interaction handling by processing input states.
|
||||||
|
* This method gives priority to certain buttons by reversing the order in which they are checked.
|
||||||
|
*
|
||||||
|
* @remarks
|
||||||
|
* The method iterates over all possible buttons, checking for specific conditions such as:
|
||||||
|
* - If the button is registered in the `interactions` dictionary.
|
||||||
|
* - If the button has been held down long enough.
|
||||||
|
* - If the button is currently pressed.
|
||||||
|
*
|
||||||
|
* Special handling is applied if gamepad support is disabled but a gamepad source is still triggering inputs,
|
||||||
|
* preventing potential infinite loops by removing the last processed movement time for the button.
|
||||||
|
*/
|
||||||
update(): void {
|
update(): void {
|
||||||
// reversed to let the cancel button have a kinda priority on the action button
|
|
||||||
for (const b of Utils.getEnumValues(Button).reverse()) {
|
for (const b of Utils.getEnumValues(Button).reverse()) {
|
||||||
if (
|
if (
|
||||||
this.interactions.hasOwnProperty(b) && // if the button is in the interactions dict
|
this.interactions.hasOwnProperty(b) &&
|
||||||
this.repeatInputDurationJustPassed(b) && // if we hold enough the button
|
this.repeatInputDurationJustPassed(b) &&
|
||||||
this.interactions[b].isPressed // if the button is pressed
|
this.interactions[b].isPressed
|
||||||
) {
|
) {
|
||||||
// if the gamepad support is disable and the source is a gamepad
|
// Prevents repeating button interactions when gamepad support is disabled.
|
||||||
// we don't want to repeat the button
|
|
||||||
// even if we have disabled the gamepad, at the exact moment of the change in the menu
|
|
||||||
// we can be here and have an infinite loop since the code can't know we
|
|
||||||
// have released the key since the gamepad is not there anymore
|
|
||||||
if (!this.gamepadSupport && this.interactions[b].source === 'gamepad') {
|
if (!this.gamepadSupport && this.interactions[b].source === 'gamepad') {
|
||||||
// if we are here and the gamepad is disabled, we delete the latest interracted button
|
// Deletes the last interaction for a button if gamepad is disabled.
|
||||||
this.delLastProcessedMovementTime(b);
|
this.delLastProcessedMovementTime(b);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
// Emits an event for the button press.
|
||||||
this.events.emit('input_down', {
|
this.events.emit('input_down', {
|
||||||
controller_type: this.interactions[b].source,
|
controller_type: this.interactions[b].source,
|
||||||
button: b,
|
button: b,
|
||||||
|
@ -129,12 +188,30 @@ export class InputsController {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Configures a gamepad for use based on its device ID.
|
||||||
|
*
|
||||||
|
* @param thisGamepad - The gamepad to set up.
|
||||||
|
*
|
||||||
|
* @remarks
|
||||||
|
* This method initializes a gamepad by mapping its ID to a predefined configuration.
|
||||||
|
* It updates the player's gamepad mapping based on the identified configuration, ensuring
|
||||||
|
* that the gamepad controls are correctly mapped to in-game actions.
|
||||||
|
*/
|
||||||
setupGamepad(thisGamepad: Phaser.Input.Gamepad.Gamepad): void {
|
setupGamepad(thisGamepad: Phaser.Input.Gamepad.Gamepad): void {
|
||||||
let gamepadID = thisGamepad.id.toLowerCase();
|
let gamepadID = thisGamepad.id.toLowerCase();
|
||||||
const mappedPad = this.mapGamepad(gamepadID);
|
const mappedPad = this.mapGamepad(gamepadID);
|
||||||
this.player['mapping'] = mappedPad.gamepadMapping;
|
this.player['mapping'] = mappedPad.gamepadMapping;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Refreshes and re-indexes the list of connected gamepads.
|
||||||
|
*
|
||||||
|
* @remarks
|
||||||
|
* This method updates the list of gamepads to exclude any that are undefined.
|
||||||
|
* It corrects the index of each gamepad to account for any previously undefined entries,
|
||||||
|
* ensuring that all gamepads are properly indexed and can be accurately referenced within the game.
|
||||||
|
*/
|
||||||
refreshGamepads(): void {
|
refreshGamepads(): void {
|
||||||
// Sometimes, gamepads are undefined. For some reason.
|
// Sometimes, gamepads are undefined. For some reason.
|
||||||
this.gamepads = this.scene.input.gamepad.gamepads.filter(function (el) {
|
this.gamepads = this.scene.input.gamepad.gamepads.filter(function (el) {
|
||||||
|
@ -146,6 +223,17 @@ export class InputsController {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves the current gamepad mapping for in-game actions.
|
||||||
|
*
|
||||||
|
* @returns An object mapping gamepad buttons to in-game actions based on the player's current gamepad configuration.
|
||||||
|
*
|
||||||
|
* @remarks
|
||||||
|
* This method constructs a mapping of gamepad buttons to in-game action buttons according to the player's
|
||||||
|
* current gamepad configuration. If no configuration is available, it returns an empty mapping.
|
||||||
|
* The mapping includes directional controls, action buttons, and system commands among others,
|
||||||
|
* adjusted for any custom settings such as swapped action buttons.
|
||||||
|
*/
|
||||||
getActionGamepadMapping(): ActionGamepadMapping {
|
getActionGamepadMapping(): ActionGamepadMapping {
|
||||||
const gamepadMapping = {};
|
const gamepadMapping = {};
|
||||||
if (!this.player?.mapping) return gamepadMapping;
|
if (!this.player?.mapping) return gamepadMapping;
|
||||||
|
@ -170,6 +258,19 @@ export class InputsController {
|
||||||
return gamepadMapping;
|
return gamepadMapping;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handles the 'down' event for gamepad buttons, emitting appropriate events and updating the interaction state.
|
||||||
|
*
|
||||||
|
* @param pad - The gamepad on which the button press occurred.
|
||||||
|
* @param button - The button that was pressed.
|
||||||
|
* @param value - The value associated with the button press, typically indicating pressure or degree of activation.
|
||||||
|
*
|
||||||
|
* @remarks
|
||||||
|
* This method is triggered when a gamepad button is pressed. If gamepad support is enabled, it:
|
||||||
|
* - Retrieves the current gamepad action mapping.
|
||||||
|
* - Checks if the pressed button is mapped to a game action.
|
||||||
|
* - If mapped, emits an 'input_down' event with the controller type and button action, and updates the interaction of this button.
|
||||||
|
*/
|
||||||
gamepadButtonDown(pad: Phaser.Input.Gamepad.Gamepad, button: Phaser.Input.Gamepad.Button, value: number): void {
|
gamepadButtonDown(pad: Phaser.Input.Gamepad.Gamepad, button: Phaser.Input.Gamepad.Button, value: number): void {
|
||||||
if (!this.gamepadSupport) return;
|
if (!this.gamepadSupport) return;
|
||||||
const actionMapping = this.getActionGamepadMapping();
|
const actionMapping = this.getActionGamepadMapping();
|
||||||
|
@ -183,6 +284,19 @@ export class InputsController {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handles the 'up' event for gamepad buttons, emitting appropriate events and clearing the interaction state.
|
||||||
|
*
|
||||||
|
* @param pad - The gamepad on which the button release occurred.
|
||||||
|
* @param button - The button that was released.
|
||||||
|
* @param value - The value associated with the button release, typically indicating pressure or degree of deactivation.
|
||||||
|
*
|
||||||
|
* @remarks
|
||||||
|
* This method is triggered when a gamepad button is released. If gamepad support is enabled, it:
|
||||||
|
* - Retrieves the current gamepad action mapping.
|
||||||
|
* - Checks if the released button is mapped to a game action.
|
||||||
|
* - If mapped, emits an 'input_up' event with the controller type and button action, and clears the interaction for this button.
|
||||||
|
*/
|
||||||
gamepadButtonUp(pad: Phaser.Input.Gamepad.Gamepad, button: Phaser.Input.Gamepad.Button, value: number): void {
|
gamepadButtonUp(pad: Phaser.Input.Gamepad.Gamepad, button: Phaser.Input.Gamepad.Button, value: number): void {
|
||||||
if (!this.gamepadSupport) return;
|
if (!this.gamepadSupport) return;
|
||||||
const actionMapping = this.getActionGamepadMapping();
|
const actionMapping = this.getActionGamepadMapping();
|
||||||
|
@ -196,6 +310,24 @@ export class InputsController {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Configures keyboard controls for the game, mapping physical keys to game actions.
|
||||||
|
*
|
||||||
|
* @remarks
|
||||||
|
* This method sets up keyboard bindings for game controls using Phaser's `KeyCodes`. Each game action, represented
|
||||||
|
* by a button in the `Button` enum, is associated with one or more physical keys. For example, movement actions
|
||||||
|
* (up, down, left, right) are mapped to both arrow keys and WASD keys. Actions such as submit, cancel, and other
|
||||||
|
* game-specific functions are mapped to appropriate keys like Enter, Space, etc.
|
||||||
|
*
|
||||||
|
* The method does the following:
|
||||||
|
* - Defines a `keyConfig` object that associates each `Button` enum value with an array of `KeyCodes`.
|
||||||
|
* - Iterates over all values of the `Button` enum to set up these key bindings within the Phaser game scene.
|
||||||
|
* - For each button, it adds the respective keys to the game's input system and stores them in `this.buttonKeys`.
|
||||||
|
* - Additional configurations for mobile or alternative input schemes are stored in `mobileKeyConfig`.
|
||||||
|
*
|
||||||
|
* Post-setup, it initializes touch controls (if applicable) and starts listening for keyboard inputs using
|
||||||
|
* `listenInputKeyboard`, ensuring that all configured keys are actively monitored for player interactions.
|
||||||
|
*/
|
||||||
setupKeyboardControls(): void {
|
setupKeyboardControls(): void {
|
||||||
const keyCodes = Phaser.Input.Keyboard.KeyCodes;
|
const keyCodes = Phaser.Input.Keyboard.KeyCodes;
|
||||||
const keyConfig = {
|
const keyConfig = {
|
||||||
|
@ -232,6 +364,24 @@ export class InputsController {
|
||||||
this.listenInputKeyboard();
|
this.listenInputKeyboard();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets up event listeners for keyboard inputs on all registered keys.
|
||||||
|
*
|
||||||
|
* @remarks
|
||||||
|
* This method iterates over an array of keyboard button rows (`this.buttonKeys`), adding 'down' and 'up'
|
||||||
|
* event listeners for each key. These listeners handle key press and release actions respectively.
|
||||||
|
*
|
||||||
|
* - **Key Down Event**: When a key is pressed down, the method emits an 'input_down' event with the button
|
||||||
|
* and the source ('keyboard'). It also records the time and state of the key press by calling
|
||||||
|
* `setLastProcessedMovementTime`.
|
||||||
|
*
|
||||||
|
* - **Key Up Event**: When a key is released, the method emits an 'input_up' event similarly, specifying the button
|
||||||
|
* and source. It then clears the recorded press time and state by calling
|
||||||
|
* `delLastProcessedMovementTime`.
|
||||||
|
*
|
||||||
|
* This setup ensures that each key on the keyboard is monitored for press and release events,
|
||||||
|
* and that these events are properly communicated within the system.
|
||||||
|
*/
|
||||||
listenInputKeyboard(): void {
|
listenInputKeyboard(): void {
|
||||||
this.buttonKeys.forEach((row, index) => {
|
this.buttonKeys.forEach((row, index) => {
|
||||||
for (const key of row) {
|
for (const key of row) {
|
||||||
|
@ -253,6 +403,19 @@ export class InputsController {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Maps a gamepad ID to a specific gamepad configuration based on the ID's characteristics.
|
||||||
|
*
|
||||||
|
* @param id - The gamepad ID string, typically representing a unique identifier for a gamepad model or make.
|
||||||
|
* @returns A `GamepadConfig` object corresponding to the identified gamepad model.
|
||||||
|
*
|
||||||
|
* @remarks
|
||||||
|
* This function analyzes the provided gamepad ID and matches it to a predefined configuration based on known identifiers:
|
||||||
|
* - If the ID includes both '081f' and 'e401', it is identified as an unlicensed SNES gamepad.
|
||||||
|
* - If the ID contains 'xbox' and '360', it is identified as an Xbox 360 gamepad.
|
||||||
|
* - If the ID contains '054c', it is identified as a DualShock gamepad.
|
||||||
|
* If no specific identifiers are recognized, a generic gamepad configuration is returned.
|
||||||
|
*/
|
||||||
mapGamepad(id: string): GamepadConfig {
|
mapGamepad(id: string): GamepadConfig {
|
||||||
id = id.toLowerCase();
|
id = id.toLowerCase();
|
||||||
|
|
||||||
|
@ -279,7 +442,21 @@ export class InputsController {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// added source so when we repeat the key, we can also tell from which source the key is from
|
/**
|
||||||
|
* This method updates the interaction state to reflect that the button is pressed.
|
||||||
|
*
|
||||||
|
* @param button - The button for which to set the interaction.
|
||||||
|
* @param source - The source of the input (defaults to 'keyboard'). This helps identify the origin of the input, especially useful in environments with multiple input devices.
|
||||||
|
*
|
||||||
|
* @remarks
|
||||||
|
* This method is responsible for updating the interaction state of a button within the `interactions` dictionary. If the button is not already registered, this method returns immediately.
|
||||||
|
* When invoked, it performs the following updates:
|
||||||
|
* - `pressTime`: Sets this to the current time, representing when the button was initially pressed.
|
||||||
|
* - `isPressed`: Marks the button as currently being pressed.
|
||||||
|
* - `source`: Identifies the source device of the input, which can vary across different hardware (e.g., keyboard, gamepad).
|
||||||
|
*
|
||||||
|
* 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'): void {
|
setLastProcessedMovementTime(button: Button, source: String = 'keyboard'): void {
|
||||||
if (!this.interactions.hasOwnProperty(button)) return;
|
if (!this.interactions.hasOwnProperty(button)) return;
|
||||||
this.setButtonLock(button);
|
this.setButtonLock(button);
|
||||||
|
@ -288,6 +465,20 @@ export class InputsController {
|
||||||
this.interactions[button].source = source;
|
this.interactions[button].source = source;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Clears the last interaction for a specified button.
|
||||||
|
*
|
||||||
|
* @param button - The button for which to clear the interaction.
|
||||||
|
*
|
||||||
|
* @remarks
|
||||||
|
* This method resets the interaction details of the button, allowing it to be processed as a new input when pressed again.
|
||||||
|
* If the button is not registered in the `interactions` dictionary, this method returns immediately, otherwise:
|
||||||
|
* - `pressTime` is cleared. This was previously storing the timestamp of when the button was initially pressed.
|
||||||
|
* - `isPressed` is set to false, indicating that the button is no longer being pressed.
|
||||||
|
* - `source` is set to null, which had been indicating the device from which the button input was originating.
|
||||||
|
*
|
||||||
|
* It releases the button lock, which prevents the button from being processed repeatedly until it's explicitly released.
|
||||||
|
*/
|
||||||
delLastProcessedMovementTime(button: Button): void {
|
delLastProcessedMovementTime(button: Button): void {
|
||||||
if (!this.interactions.hasOwnProperty(button)) return;
|
if (!this.interactions.hasOwnProperty(button)) return;
|
||||||
this.releaseButtonLock(button);
|
this.releaseButtonLock(button);
|
||||||
|
@ -296,6 +487,24 @@ export class InputsController {
|
||||||
this.interactions[button].source = null;
|
this.interactions[button].source = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Deactivates all currently pressed keys and resets their interaction states.
|
||||||
|
*
|
||||||
|
* @remarks
|
||||||
|
* This method is used to reset the state of all buttons within the `interactions` dictionary,
|
||||||
|
* effectively deactivating any currently pressed keys. It performs the following actions:
|
||||||
|
*
|
||||||
|
* - Releases button locks for predefined buttons (`buttonLock` and `buttonLock2`), allowing them
|
||||||
|
* to be pressed again or properly re-initialized in future interactions.
|
||||||
|
* - Iterates over all possible button values obtained via `Utils.getEnumValues(Button)`, and for
|
||||||
|
* each button:
|
||||||
|
* - Checks if the button is currently registered in the `interactions` dictionary.
|
||||||
|
* - Resets `pressTime` to null, indicating that there is no ongoing interaction.
|
||||||
|
* - Sets `isPressed` to false, marking the button as not currently active.
|
||||||
|
* - Clears the `source` field, removing the record of which device the button press came from.
|
||||||
|
*
|
||||||
|
* This method is typically called when needing to ensure that all inputs are neutralized.
|
||||||
|
*/
|
||||||
deactivatePressedKey(): void {
|
deactivatePressedKey(): void {
|
||||||
this.releaseButtonLock(this.buttonLock);
|
this.releaseButtonLock(this.buttonLock);
|
||||||
this.releaseButtonLock(this.buttonLock2);
|
this.releaseButtonLock(this.buttonLock2);
|
||||||
|
@ -308,10 +517,31 @@ export class InputsController {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if a specific button is currently locked.
|
||||||
|
*
|
||||||
|
* @param button - The button to check for a lock status.
|
||||||
|
* @returns `true` if the button is either of the two potentially locked buttons (`buttonLock` or `buttonLock2`), otherwise `false`.
|
||||||
|
*
|
||||||
|
* @remarks
|
||||||
|
* This method is used to determine if a given button is currently prevented from being processed due to a lock.
|
||||||
|
* It checks against two separate lock variables, allowing for up to two buttons to be locked simultaneously.
|
||||||
|
*/
|
||||||
isButtonLocked(button: Button): boolean {
|
isButtonLocked(button: Button): boolean {
|
||||||
return (this.buttonLock === button || this.buttonLock2 === button);
|
return (this.buttonLock === button || this.buttonLock2 === button);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets a lock on a given button if it is not already locked.
|
||||||
|
*
|
||||||
|
* @param button - The button to lock.
|
||||||
|
*
|
||||||
|
* @remarks
|
||||||
|
* This method ensures that a button is not processed multiple times inadvertently.
|
||||||
|
* It checks if the button is already locked by either of the two lock variables (`buttonLock` or `buttonLock2`).
|
||||||
|
* If not, it locks the button using the first available lock variable.
|
||||||
|
* This mechanism allows for up to two buttons to be locked at the same time.
|
||||||
|
*/
|
||||||
setButtonLock(button: Button): void {
|
setButtonLock(button: Button): void {
|
||||||
if (this.buttonLock === button || this.buttonLock2 === button) return;
|
if (this.buttonLock === button || this.buttonLock2 === button) return;
|
||||||
if (this.buttonLock === button) this.buttonLock2 = button;
|
if (this.buttonLock === button) this.buttonLock2 = button;
|
||||||
|
@ -320,6 +550,16 @@ export class InputsController {
|
||||||
else this.buttonLock = button;
|
else this.buttonLock = button;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Releases a lock on a specific button, allowing it to be processed again.
|
||||||
|
*
|
||||||
|
* @param button - The button whose lock is to be released.
|
||||||
|
*
|
||||||
|
* @remarks
|
||||||
|
* This method checks both lock variables (`buttonLock` and `buttonLock2`).
|
||||||
|
* If either lock matches the specified button, that lock is cleared.
|
||||||
|
* This action frees the button to be processed again, ensuring it can respond to new inputs.
|
||||||
|
*/
|
||||||
releaseButtonLock(button: Button): void {
|
releaseButtonLock(button: Button): void {
|
||||||
if (this.buttonLock === button) this.buttonLock = null;
|
if (this.buttonLock === button) this.buttonLock = null;
|
||||||
else if (this.buttonLock2 === button) this.buttonLock2 = null;
|
else if (this.buttonLock2 === button) this.buttonLock2 = null;
|
||||||
|
|
Loading…
Reference in New Issue