From 6d8df791954f921c6fde123126ffd89e348de638 Mon Sep 17 00:00:00 2001 From: Will McCambley Date: Thu, 2 May 2024 20:47:02 -0700 Subject: [PATCH 1/2] basic analog support --- src/battle-scene.ts | 82 +++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 76 insertions(+), 6 deletions(-) diff --git a/src/battle-scene.ts b/src/battle-scene.ts index cbf363f68..c49d05c05 100644 --- a/src/battle-scene.ts +++ b/src/battle-scene.ts @@ -93,7 +93,11 @@ export enum Button { CYCLE_NATURE, CYCLE_VARIANT, SPEED_UP, - SLOW_DOWN + SLOW_DOWN, + LEFT_STICK_UP, + LEFT_STICK_DOWN, + LEFT_STICK_LEFT, + LEFT_STICK_RIGHT } export interface PokeballCounts { @@ -221,6 +225,7 @@ export default class BattleScene extends SceneBase { public rngCounter: integer = 0; public rngSeedOverride: string = ''; public rngOffset: integer = 0; + private analogStickThreshold = 0.5; constructor() { super('battle'); @@ -1447,8 +1452,66 @@ export default class BattleScene extends SceneBase { } if (inputSuccess && this.enableVibration && typeof navigator.vibrate !== 'undefined') navigator.vibrate(vibrationLength || 10); + + if(this.gamepadSupport){ + this.handleAnalogStick(); + } } + private analogStickDirections = { + [Button.LEFT_STICK_UP]: false, + [Button.LEFT_STICK_DOWN]: false, + [Button.LEFT_STICK_LEFT]: false, + [Button.LEFT_STICK_RIGHT]: false + }; + + handleAnalogStick(): void { + const gamepad = this.input.gamepad?.gamepads[0]; + if (!gamepad) + return; + + const leftStickX = gamepad.leftStick.x; + const leftStickY = gamepad.leftStick.y; + + if (Math.abs(leftStickX) > Math.abs(leftStickY)) { + if (leftStickX < -this.analogStickThreshold) { + // Left analog stick moved to the left + this.handleAnalogStickDirection(Button.LEFT_STICK_LEFT, Button.LEFT); + } else if (leftStickX > this.analogStickThreshold) { + // Left analog stick moved to the right + this.handleAnalogStickDirection(Button.LEFT_STICK_RIGHT, Button.RIGHT); + } else { + // Left analog stick is in the neutral position horizontally + this.analogStickDirections[Button.LEFT_STICK_LEFT] = false; + this.analogStickDirections[Button.LEFT_STICK_RIGHT] = false; + } + } else { + if (leftStickY < -this.analogStickThreshold) { + // Left analog stick moved up + this.handleAnalogStickDirection(Button.LEFT_STICK_UP, Button.UP); + } else if (leftStickY > this.analogStickThreshold) { + // Left analog stick moved down + this.handleAnalogStickDirection(Button.LEFT_STICK_DOWN, Button.DOWN); + } else { + // Left analog stick is in the neutral position vertically + this.analogStickDirections[Button.LEFT_STICK_UP] = false; + this.analogStickDirections[Button.LEFT_STICK_DOWN] = false; + } + } + } + + private handleAnalogStickDirection(analogStickButton: Button, direction: Button): void { + if (!this.analogStickDirections[analogStickButton]) { + // First button press + this.ui.processInput(direction); + this.setLastProcessedMovementTime(analogStickButton); + this.analogStickDirections[analogStickButton] = true; + } else if (this.repeatInputDurationJustPassed(analogStickButton)) { + // Subsequent holds + this.ui.processInput(direction); + this.setLastProcessedMovementTime(analogStickButton); + } + } /** * gamepadButtonJustDown returns true if @param button has just been pressed down * or not. It will only return true once, until the key is released and pressed down @@ -1498,15 +1561,22 @@ export default class BattleScene extends SceneBase { */ repeatInputDurationJustPassed(button: Button): boolean { if (this.movementButtonLock !== null && this.movementButtonLock !== button) { - return false; + return false; } - if (this.buttonKeys[button].every(k => k.isUp) && this.gamepadButtonStates.every(b => b == false)) { - this.movementButtonLock = null; - return false; + + const isAnalogStickButton = button >= Button.LEFT_STICK_UP && button <= Button.LEFT_STICK_RIGHT; + const isButtonPressed = isAnalogStickButton || this.buttonKeys[button]?.some(k => k.isDown); + + if (!isButtonPressed && this.gamepadButtonStates.every(b => b == false)) { + this.movementButtonLock = null; + return false; } + if (this.time.now - this.lastProcessedButtonPressTimes.get(button) >= repeatInputDelayMillis) { - return true; + return true; } + + return false; } setLastProcessedMovementTime(button: Button) { From 8071af00ea2556eb71a7660eb166285655cc5a39 Mon Sep 17 00:00:00 2001 From: Will McCambley Date: Fri, 3 May 2024 08:42:41 -0700 Subject: [PATCH 2/2] use existing direction inputs --- src/battle-scene.ts | 52 +++++++++++++++++++++------------------------ 1 file changed, 24 insertions(+), 28 deletions(-) diff --git a/src/battle-scene.ts b/src/battle-scene.ts index c49d05c05..2aad57930 100644 --- a/src/battle-scene.ts +++ b/src/battle-scene.ts @@ -93,11 +93,7 @@ export enum Button { CYCLE_NATURE, CYCLE_VARIANT, SPEED_UP, - SLOW_DOWN, - LEFT_STICK_UP, - LEFT_STICK_DOWN, - LEFT_STICK_LEFT, - LEFT_STICK_RIGHT + SLOW_DOWN } export interface PokeballCounts { @@ -1459,10 +1455,10 @@ export default class BattleScene extends SceneBase { } private analogStickDirections = { - [Button.LEFT_STICK_UP]: false, - [Button.LEFT_STICK_DOWN]: false, - [Button.LEFT_STICK_LEFT]: false, - [Button.LEFT_STICK_RIGHT]: false + [Button.UP]: false, + [Button.DOWN]: false, + [Button.LEFT]: false, + [Button.RIGHT]: false }; handleAnalogStick(): void { @@ -1476,40 +1472,40 @@ export default class BattleScene extends SceneBase { if (Math.abs(leftStickX) > Math.abs(leftStickY)) { if (leftStickX < -this.analogStickThreshold) { // Left analog stick moved to the left - this.handleAnalogStickDirection(Button.LEFT_STICK_LEFT, Button.LEFT); + this.handleAnalogStickDirection(Button.LEFT); } else if (leftStickX > this.analogStickThreshold) { // Left analog stick moved to the right - this.handleAnalogStickDirection(Button.LEFT_STICK_RIGHT, Button.RIGHT); + this.handleAnalogStickDirection(Button.RIGHT); } else { // Left analog stick is in the neutral position horizontally - this.analogStickDirections[Button.LEFT_STICK_LEFT] = false; - this.analogStickDirections[Button.LEFT_STICK_RIGHT] = false; + this.analogStickDirections[Button.LEFT] = false; + this.analogStickDirections[Button.RIGHT] = false; } } else { if (leftStickY < -this.analogStickThreshold) { // Left analog stick moved up - this.handleAnalogStickDirection(Button.LEFT_STICK_UP, Button.UP); + this.handleAnalogStickDirection(Button.UP); } else if (leftStickY > this.analogStickThreshold) { // Left analog stick moved down - this.handleAnalogStickDirection(Button.LEFT_STICK_DOWN, Button.DOWN); + this.handleAnalogStickDirection(Button.DOWN); } else { // Left analog stick is in the neutral position vertically - this.analogStickDirections[Button.LEFT_STICK_UP] = false; - this.analogStickDirections[Button.LEFT_STICK_DOWN] = false; + this.analogStickDirections[Button.UP] = false; + this.analogStickDirections[Button.DOWN] = false; } } } - private handleAnalogStickDirection(analogStickButton: Button, direction: Button): void { - if (!this.analogStickDirections[analogStickButton]) { + private handleAnalogStickDirection(button: Button): void { + if (!this.analogStickDirections[button]) { // First button press - this.ui.processInput(direction); - this.setLastProcessedMovementTime(analogStickButton); - this.analogStickDirections[analogStickButton] = true; - } else if (this.repeatInputDurationJustPassed(analogStickButton)) { + this.ui.processInput(button); + this.setLastProcessedMovementTime(button); + this.analogStickDirections[button] = true; + } else if (this.repeatInputDurationJustPassed(button)) { // Subsequent holds - this.ui.processInput(direction); - this.setLastProcessedMovementTime(analogStickButton); + this.ui.processInput(button); + this.setLastProcessedMovementTime(button); } } /** @@ -1534,7 +1530,7 @@ export default class BattleScene extends SceneBase { buttonJustPressed(button: Button): boolean { const gamepad = this.input.gamepad?.gamepads[0]; - return this.buttonKeys[button].some(k => Phaser.Input.Keyboard.JustDown(k)) || this.gamepadButtonJustDown(gamepad?.buttons[this.gamepadKeyConfig[button]]); + return this.buttonKeys[button].some(k => Phaser.Input.Keyboard.JustDown(k)) || this.gamepadButtonJustDown(gamepad?.buttons[this.gamepadKeyConfig[button]]) } /** @@ -1564,8 +1560,8 @@ export default class BattleScene extends SceneBase { return false; } - const isAnalogStickButton = button >= Button.LEFT_STICK_UP && button <= Button.LEFT_STICK_RIGHT; - const isButtonPressed = isAnalogStickButton || this.buttonKeys[button]?.some(k => k.isDown); + const analogStickHeld = this.analogStickDirections[button]; + const isButtonPressed = analogStickHeld || this.buttonKeys[button]?.some(k => k.isDown); if (!isButtonPressed && this.gamepadButtonStates.every(b => b == false)) { this.movementButtonLock = null;