You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
50 lines
1.4 KiB
50 lines
1.4 KiB
import { Phase, PlayerActions } from "./enums"; |
|
import { assert } from "./utils"; |
|
|
|
const valid_actions = Object.values(PlayerActions); |
|
|
|
class InputHandler { |
|
/** |
|
* Handles input relating to the player's game actions. |
|
* @class |
|
* @property {import('./game.js').GameState} game_state - The game state this handler manipulates |
|
* @property {import('./Time.js').Time} time - The time manager for the current game loop |
|
* |
|
* The InputHandler manages player events (clicks, keystrokes, etc.). |
|
* It validates actions, updates game state, and interacts with the time system as needed. |
|
*/ |
|
game_state; |
|
time; |
|
constructor(game_state, time) { |
|
this.game_state = game_state; |
|
this.time = time; |
|
} |
|
|
|
setup() { |
|
document.addEventListener("click", this.handleClickEvent); |
|
} |
|
|
|
/** |
|
* Click handler |
|
* @param {Event} e |
|
* @returns |
|
*/ |
|
handleClickEvent(e) { |
|
if (!e.target) return; |
|
const el = /** @type {Element} */ (e.target) |
|
|
|
const target = /** @type {HTMLElement | null} */ (el.closest("[data-action]")); |
|
assert(!!target, "there must be a valid target"); |
|
|
|
const action = target?.dataset?.action ?? ""; |
|
switch (action) { |
|
case PlayerActions.OPEN: |
|
this.game_state.time.phase = Phase.OPEN; |
|
break; |
|
case PlayerActions.CLOSE: |
|
this.game_state.time.phase = Phase.CLOSE; |
|
break; |
|
|
|
} |
|
} |
|
}
|
|
|