Initializes a new instance of the game control system, setting up initial state and configurations.
The Phaser scene associated with this instance.
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.
Private
buttonPrivate
buttonPrivate
buttonPrivate
gamepadPrivate
gamepadsPrivate
interactionsPrivate
playerPrivate
scenePrivate
timeDeactivates all currently pressed keys and resets their interaction states.
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:
buttonLock
and buttonLock2
), allowing them
to be pressed again or properly re-initialized in future interactions.Utils.getEnumValues(Button)
, and for
each button:interactions
dictionary.pressTime
to null, indicating that there is no ongoing interaction.isPressed
to false, marking the button as not currently active.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.
Clears the last interaction for a specified button.
The button for which to clear the interaction.
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.
Handles the 'down' event for gamepad buttons, emitting appropriate events and updating the interaction state.
The gamepad on which the button press occurred.
The button that was pressed.
The value associated with the button press, typically indicating pressure or degree of activation.
This method is triggered when a gamepad button is pressed. If gamepad support is enabled, it:
Handles the 'up' event for gamepad buttons, emitting appropriate events and clearing the interaction state.
The gamepad on which the button release occurred.
The button that was released.
The value associated with the button release, typically indicating pressure or degree of deactivation.
This method is triggered when a gamepad button is released. If gamepad support is enabled, it:
Retrieves the current gamepad mapping for in-game actions.
An object mapping gamepad buttons to in-game actions based on the player's current gamepad configuration.
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.
Sets up event handlers and initializes gamepad and keyboard controls.
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.
Checks if a specific button is currently locked.
The button to check for a lock status.
true
if the button is either of the two potentially locked buttons (buttonLock
or buttonLock2
), otherwise false
.
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.
Sets up event listeners for keyboard inputs on all registered keys.
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.
Handles actions to take when the game loses focus, such as deactivating pressed keys.
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.
Maps a gamepad ID to a specific gamepad configuration based on the ID's characteristics.
The gamepad ID string, typically representing a unique identifier for a gamepad model or make.
A GamepadConfig
object corresponding to the identified gamepad model.
This function analyzes the provided gamepad ID and matches it to a predefined configuration based on known identifiers:
Refreshes and re-indexes the list of connected gamepads.
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.
Releases a lock on a specific button, allowing it to be processed again.
The button whose lock is to be released.
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.
repeatInputDurationJustPassed returns true if
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.
Sets a lock on a given button if it is not already locked.
The button to lock.
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.
Enables or disables support for gamepad input.
A boolean indicating whether gamepad support should be enabled (true) or disabled (false).
This method toggles gamepad support. If disabled, it also ensures that all currently pressed gamepad buttons are deactivated to avoid stuck inputs.
This method updates the interaction state to reflect that the button is pressed.
The button for which to set the interaction.
The source of the input (defaults to 'keyboard'). This helps identify the origin of the input, especially useful in environments with multiple input devices.
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.
Configures a gamepad for use based on its device ID.
The gamepad to set up.
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.
Configures keyboard controls for the game, mapping physical keys to game actions.
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:
keyConfig
object that associates each Button
enum value with an array of KeyCodes
.Button
enum to set up these key bindings within the Phaser game scene.this.buttonKeys
.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.
Updates the interaction handling by processing input states. This method gives priority to certain buttons by reversing the order in which they are checked.
The method iterates over all possible buttons, checking for specific conditions such as:
interactions
dictionary.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.
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:
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.