allow 2 button to be pressed at the same time

pull/512/merge
Greenlamp 2024-05-06 01:27:34 +02:00 committed by Samuel H
parent 77b6ad47d0
commit 4a49ff848a
1 changed files with 25 additions and 8 deletions

View File

@ -31,6 +31,7 @@ export class InputsController {
// buttonLock ensures only a single movement key is firing repeated inputs // buttonLock ensures only a single movement key is firing repeated inputs
// (i.e. by holding down a button) at a time // (i.e. by holding down a button) at a time
private buttonLock: Button; private buttonLock: Button;
private buttonLock2: Button;
private interactions: Map<Button, Map<string, boolean>> = new Map(); private interactions: Map<Button, Map<string, boolean>> = new Map();
private time: Time; private time: Time;
private player: Map<String, GamepadMapping> = new Map(); private player: Map<String, GamepadMapping> = new Map();
@ -87,7 +88,8 @@ export class InputsController {
} }
update(): void { update(): void {
for (const b of Utils.getEnumValues(Button)) { // reversed to let the cancel button have a kinda priority on the action button
for (const b of Utils.getEnumValues(Button).reverse()) {
if (!this.interactions.hasOwnProperty(b)) continue; if (!this.interactions.hasOwnProperty(b)) continue;
if (this.repeatInputDurationJustPassed(b) && this.interactions[b].isPressed) { if (this.repeatInputDurationJustPassed(b) && this.interactions[b].isPressed) {
this.events.emit('input_down', { this.events.emit('input_down', {
@ -243,35 +245,50 @@ export class InputsController {
* firing a repeated input - this is to prevent multiple buttons from firing repeatedly. * firing a repeated input - this is to prevent multiple buttons from firing repeatedly.
*/ */
repeatInputDurationJustPassed(button: Button): boolean { repeatInputDurationJustPassed(button: Button): boolean {
if (this.buttonLock === null || this.buttonLock !== button) { if (!this.isButtonLocked(button)) return false;
return false;
}
if (this.time.now - this.interactions[button].pressTime >= repeatInputDelayMillis) { if (this.time.now - this.interactions[button].pressTime >= repeatInputDelayMillis) {
this.buttonLock = null;
return true; return true;
} }
} }
setLastProcessedMovementTime(button: Button): void { setLastProcessedMovementTime(button: Button): void {
if (!this.interactions.hasOwnProperty(button)) return; if (!this.interactions.hasOwnProperty(button)) return;
this.buttonLock = button; this.setButtonLock(button);
this.interactions[button].pressTime = this.time.now; this.interactions[button].pressTime = this.time.now;
this.interactions[button].isPressed = true; this.interactions[button].isPressed = true;
} }
delLastProcessedMovementTime(button: Button): void { delLastProcessedMovementTime(button: Button): void {
if (!this.interactions.hasOwnProperty(button)) return; if (!this.interactions.hasOwnProperty(button)) return;
this.buttonLock = null; this.releaseButtonLock(button);
this.interactions[button].pressTime = null; this.interactions[button].pressTime = null;
this.interactions[button].isPressed = false; this.interactions[button].isPressed = false;
} }
deactivatePressedKey(): void { deactivatePressedKey(): void {
this.buttonLock = null; this.releaseButtonLock(this.buttonLock);
this.releaseButtonLock(this.buttonLock2);
for (const b of Utils.getEnumValues(Button)) { for (const b of Utils.getEnumValues(Button)) {
if (!this.interactions.hasOwnProperty(b)) return; if (!this.interactions.hasOwnProperty(b)) return;
this.interactions[b].pressTime = null; this.interactions[b].pressTime = null;
this.interactions[b].isPressed = false; this.interactions[b].isPressed = false;
} }
} }
isButtonLocked(button: Button): boolean {
return (this.buttonLock === button || this.buttonLock2 === button);
}
setButtonLock(button: Button): void {
if (this.buttonLock === button || this.buttonLock2 === button) return;
if (this.buttonLock === button) this.buttonLock2 = button;
else if (this.buttonLock2 === button) this.buttonLock = button;
else if(!!this.buttonLock) this.buttonLock2 = button;
else this.buttonLock = button;
}
releaseButtonLock(button: Button): void {
if (this.buttonLock === button) this.buttonLock = null;
else if (this.buttonLock2 === button) this.buttonLock2 = null;
}
} }