pull/405/merge
Will McCambley 2024-05-05 04:00:11 -07:00 committed by GitHub
commit 1d8bf25c67
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 72 additions and 6 deletions

View File

@ -221,6 +221,7 @@ export default class BattleScene extends SceneBase {
public rngCounter: integer = 0; public rngCounter: integer = 0;
public rngSeedOverride: string = ''; public rngSeedOverride: string = '';
public rngOffset: integer = 0; public rngOffset: integer = 0;
private analogStickThreshold = 0.5;
constructor() { constructor() {
super('battle'); super('battle');
@ -1456,8 +1457,66 @@ export default class BattleScene extends SceneBase {
} }
if (inputSuccess && this.enableVibration && typeof navigator.vibrate !== 'undefined') if (inputSuccess && this.enableVibration && typeof navigator.vibrate !== 'undefined')
navigator.vibrate(vibrationLength || 10); navigator.vibrate(vibrationLength || 10);
if(this.gamepadSupport){
this.handleAnalogStick();
}
} }
private analogStickDirections = {
[Button.UP]: false,
[Button.DOWN]: false,
[Button.LEFT]: false,
[Button.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);
} else if (leftStickX > this.analogStickThreshold) {
// Left analog stick moved to the right
this.handleAnalogStickDirection(Button.RIGHT);
} else {
// Left analog stick is in the neutral position horizontally
this.analogStickDirections[Button.LEFT] = false;
this.analogStickDirections[Button.RIGHT] = false;
}
} else {
if (leftStickY < -this.analogStickThreshold) {
// Left analog stick moved up
this.handleAnalogStickDirection(Button.UP);
} else if (leftStickY > this.analogStickThreshold) {
// Left analog stick moved down
this.handleAnalogStickDirection(Button.DOWN);
} else {
// Left analog stick is in the neutral position vertically
this.analogStickDirections[Button.UP] = false;
this.analogStickDirections[Button.DOWN] = false;
}
}
}
private handleAnalogStickDirection(button: Button): void {
if (!this.analogStickDirections[button]) {
// First button press
this.ui.processInput(button);
this.setLastProcessedMovementTime(button);
this.analogStickDirections[button] = true;
} else if (this.repeatInputDurationJustPassed(button)) {
// Subsequent holds
this.ui.processInput(button);
this.setLastProcessedMovementTime(button);
}
}
/** /**
* gamepadButtonJustDown returns true if @param button has just been pressed down * 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 * or not. It will only return true once, until the key is released and pressed down
@ -1480,7 +1539,7 @@ export default class BattleScene extends SceneBase {
buttonJustPressed(button: Button): boolean { buttonJustPressed(button: Button): boolean {
const gamepad = this.input.gamepad?.gamepads[0]; 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]])
} }
/** /**
@ -1509,13 +1568,20 @@ export default class BattleScene extends SceneBase {
if (this.movementButtonLock !== null && this.movementButtonLock !== button) { 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)) {
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; this.movementButtonLock = null;
return false; return false;
} }
if (this.time.now - this.lastProcessedButtonPressTimes.get(button) >= repeatInputDelayMillis) { if (this.time.now - this.lastProcessedButtonPressTimes.get(button) >= repeatInputDelayMillis) {
return true; return true;
} }
return false;
} }
setLastProcessedMovementTime(button: Button) { setLastProcessedMovementTime(button: Button) {