From 1b41bdb8e3df939b8f0b182bad0bef820ae956dc Mon Sep 17 00:00:00 2001 From: Hans Sorongon Date: Tue, 16 Apr 2024 06:55:03 +0800 Subject: [PATCH] Added built-in support for gamepads using Phaser.Gamepad --- src/battle-scene.ts | 49 ++++++++++++++++++++++++++++++++++++++++++--- src/main.ts | 3 ++- 2 files changed, 48 insertions(+), 4 deletions(-) diff --git a/src/battle-scene.ts b/src/battle-scene.ts index 30c6c5603..2d7329927 100644 --- a/src/battle-scene.ts +++ b/src/battle-scene.ts @@ -198,6 +198,26 @@ export default class BattleScene extends SceneBase { // (i.e. by holding down a button) at a time private movementButtonLock: Button; + // using a dualshock controller as a map + private gamepadKeyConfig = { + [Button.UP]: 12, // up + [Button.DOWN]: 13, // down + [Button.LEFT]: 14, // left + [Button.RIGHT]: 15, // right + [Button.SUBMIT]: 17, // touchpad + [Button.ACTION]: 0, // X + [Button.CANCEL]: 1, // O + [Button.MENU]: 9, // options + [Button.CYCLE_SHINY]: 5, // RB + [Button.CYCLE_FORM]: 4, // LB + [Button.CYCLE_GENDER]: 6, // LT + [Button.CYCLE_ABILITY]: 7, // RT + [Button.CYCLE_NATURE]: 2, // square + [Button.SPEED_UP]: 10, // L3 + [Button.SLOW_DOWN]: 11 // R3 + } + public gamepadButtonStates: boolean[] = new Array(17).fill(false); + public rngCounter: integer = 0; public rngSeedOverride: string = ''; public rngOffset: integer = 0; @@ -1273,8 +1293,31 @@ export default class BattleScene extends SceneBase { navigator.vibrate(vibrationLength || 10); } + /** + * 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 + * again. + */ + gamepadButtonJustDown(button: Phaser.Input.Gamepad.Button) : boolean { + + if (!button) return; + + let returnValue = false; + if (button.pressed) { + if (!this.gamepadButtonStates[button.index]) { + returnValue = true; + } + this.gamepadButtonStates[button.index] = true; + } else { + this.gamepadButtonStates[button.index] = false; + } + + return returnValue; + } + buttonJustPressed(button: Button): boolean { - return this.buttonKeys[button].some(k => Phaser.Input.Keyboard.JustDown(k)); + 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]]); } /** @@ -1286,7 +1329,7 @@ export default class BattleScene extends SceneBase { if (this.movementButtonLock !== null && this.movementButtonLock !== button) { return false; } - if (this.buttonKeys[button].every(k => k.isUp)) { + if (this.buttonKeys[button].every(k => k.isUp) && this.gamepadButtonStates.every(b => b == false)) { this.movementButtonLock = null; return false; } @@ -1937,4 +1980,4 @@ export default class BattleScene extends SceneBase { return false; } -} \ No newline at end of file +} diff --git a/src/main.ts b/src/main.ts index 4c2260614..8695ffb34 100644 --- a/src/main.ts +++ b/src/main.ts @@ -44,6 +44,7 @@ const config: Phaser.Types.Core.GameConfig = { touch: { target: 'app' }, + gamepad: true }, dom: { createContainer: true @@ -78,4 +79,4 @@ document.fonts.load('16px emerald').then(() => document.fonts.load('10px pkmnems const game = new Phaser.Game(config); game.sound.pauseOnBlur = false; -export default game; \ No newline at end of file +export default game;