Skip to content
Make the player visible with a procedural 3D humanoid132be7c
JavaScript89 lines3.1 KB
// Keyboard → engine input flag mapping.// Engine input flags: { left, right, jump, punch, kick, block }. export const INPUT_KEYS = Object.freeze(['left', 'right', 'jump', 'punch', 'kick', 'block']); /** Player 1: WASD + J/K/L */export const PLAYER1_BINDINGS = Object.freeze({  KeyA: 'left', KeyD: 'right', KeyW: 'jump', KeyS: 'block',  KeyJ: 'punch', KeyK: 'kick', KeyL: 'block',}); /** Optional local player 2: arrows + numpad */export const PLAYER2_BINDINGS = Object.freeze({  ArrowLeft: 'left', ArrowRight: 'right', ArrowUp: 'jump', ArrowDown: 'block',  Numpad1: 'punch', Numpad2: 'kick', Numpad3: 'block',  Numpad4: 'left', Numpad6: 'right', Numpad8: 'jump', Numpad5: 'block',}); const EDGE_ACTIONS = new Set(['jump', 'punch', 'kick']); export function emptyInput() {  return { left: false, right: false, jump: false, punch: false, kick: false, block: false };} /** Returns the action a key code maps to for the given bindings, or null. */export function actionForKey(code, bindings = PLAYER1_BINDINGS) {  return Object.prototype.hasOwnProperty.call(bindings, code) ? bindings[code] : null;} /** True if the key code belongs to any of the provided binding sets. */export function isGameKey(code, bindingSets = [PLAYER1_BINDINGS, PLAYER2_BINDINGS]) {  return bindingSets.some((b) => actionForKey(code, b) !== null);} /** * Per-player keyboard tracker. Feed it keydown/keyup codes and read * `snapshot()` each engine tick. Jump/punch/kick are edge-triggered (reported * once per key press so holding a key does not spam attacks); left/right/block * are level-triggered (true while held). Opposite directions cancel out. */export function createInputTracker(bindings = PLAYER1_BINDINGS) {  const held = emptyInput();  const pressed = emptyInput();   return {    bindings,    /** @returns {boolean} whether the key was handled */    keyDown(code) {      const action = actionForKey(code, bindings);      if (!action) return false;      if (!held[action]) pressed[action] = true;      held[action] = true;      return true;    },    keyUp(code) {      const action = actionForKey(code, bindings);      if (!action) return false;      held[action] = false;      return true;    },    /** Current input flags; clears edge-triggered presses. */    snapshot() {      const out = emptyInput();      for (const k of INPUT_KEYS) {        out[k] = EDGE_ACTIONS.has(k) ? pressed[k] : held[k];        pressed[k] = false;      }      if (out.left && out.right) { out.left = false; out.right = false; }      return out;    },    /** Raw held state (level-triggered for all actions). */    heldState() { return { ...held }; },    clear() {      for (const k of INPUT_KEYS) { held[k] = false; pressed[k] = false; }    },  };} /** Stateless convenience: build flags from a list of currently held key codes. */export function inputFromKeys(codes, bindings = PLAYER1_BINDINGS) {  const out = emptyInput();  for (const code of codes) {    const action = actionForKey(code, bindings);    if (action) out[action] = true;  }  if (out.left && out.right) { out.left = false; out.right = false; }  return out;}