From 4a49ff848a653ff3a3dd6d9a2347eccba7328dfc Mon Sep 17 00:00:00 2001 From: Greenlamp Date: Mon, 6 May 2024 01:27:34 +0200 Subject: [PATCH] allow 2 button to be pressed at the same time --- src/inputs-controller.ts | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/src/inputs-controller.ts b/src/inputs-controller.ts index 83182629d..5f5615358 100644 --- a/src/inputs-controller.ts +++ b/src/inputs-controller.ts @@ -31,6 +31,7 @@ export class InputsController { // 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 buttonLock2: Button; private interactions: Map> = new Map(); private time: Time; private player: Map = new Map(); @@ -87,7 +88,8 @@ export class InputsController { } 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.repeatInputDurationJustPassed(b) && this.interactions[b].isPressed) { 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. */ repeatInputDurationJustPassed(button: Button): boolean { - if (this.buttonLock === null || this.buttonLock !== button) { - return false; - } + if (!this.isButtonLocked(button)) return false; if (this.time.now - this.interactions[button].pressTime >= repeatInputDelayMillis) { - this.buttonLock = null; return true; } } setLastProcessedMovementTime(button: Button): void { if (!this.interactions.hasOwnProperty(button)) return; - this.buttonLock = button; + this.setButtonLock(button); this.interactions[button].pressTime = this.time.now; this.interactions[button].isPressed = true; } delLastProcessedMovementTime(button: Button): void { if (!this.interactions.hasOwnProperty(button)) return; - this.buttonLock = null; + this.releaseButtonLock(button); this.interactions[button].pressTime = null; this.interactions[button].isPressed = false; } deactivatePressedKey(): void { - this.buttonLock = null; + this.releaseButtonLock(this.buttonLock); + this.releaseButtonLock(this.buttonLock2); for (const b of Utils.getEnumValues(Button)) { if (!this.interactions.hasOwnProperty(b)) return; this.interactions[b].pressTime = null; 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; + } } \ No newline at end of file