Two players, one keyboard
- PROBLEM
- Versus and co-op put two players on the same keyboard, each with their own controls, and holding the attack key must not spam attacks.
- SOLUTION
-
A data-driven
InputManager: each key map (WASD + F, Arrows + K) is a config object, and every action resolves to a callback. Each scene creates one instance per player. In versus, attacks are edge-triggered: a per-player flag is consumed on attack and only reset when the key is released. - RESULT
- Both modes share the same input code, remapping controls is a config change, and holding a key gives exactly one attack.
// src/scenes/Coop.js (excerpt): same class, different key map per player
this.inputManagerPlayer1 = new InputManager({
scene: this,
inputConfig: inputConfigs.wasd, // W A S D + F
callbacks: {
left: () => this.movePlayer(this.player1, "left"),
right: () => this.movePlayer(this.player1, "right"),
action: () => this.shootBullet(this.player1, this.player1CanAttack),
stop: () => this.stopPlayer(this.player1)
}
});