import type { Action, InputService } from './types'; const KEYS: Record = { ArrowLeft: 'left', KeyA: 'left', ArrowRight: 'right', KeyD: 'right', ArrowUp: 'up', KeyW: 'up', ArrowDown: 'down', KeyS: 'down', Space: 'jump', KeyJ: 'attack', KeyF: 'attack', KeyK: 'special', KeyG: 'special', KeyX: 'tool', KeyL: 'tool', KeyY: 'dig', KeyC: 'dig', KeyV: 'tool2', KeyB: 'bag', KeyI: 'bag', KeyE: 'interact', Enter: 'interact', KeyR: 'nextPower', Tab: 'nextPower', KeyQ: 'prevPower', Digit1: 'power1', Digit2: 'power2', Digit3: 'power3', Digit4: 'power4', Digit5: 'power5', KeyM: 'map', Escape: 'pause', KeyP: 'pause', }; /** Mando estándar: índice de botón → acción. */ const PAD: Array<[number, Action]> = [ [0, 'jump'], [1, 'attack'], [2, 'tool'], [3, 'dig'], [5, 'special'], [7, 'special'], [4, 'nextPower'], [6, 'prevPower'], [12, 'up'], [13, 'down'], [14, 'left'], [15, 'right'], [12, 'interact'], [8, 'map'], [9, 'pause'], [11, 'tool2'], [10, 'bag'], ]; const ALL: Action[] = [ 'left', 'right', 'up', 'down', 'jump', 'attack', 'special', 'tool', 'tool2', 'dig', 'bag', 'interact', 'nextPower', 'prevPower', 'power1', 'power2', 'power3', 'power4', 'power5', 'map', 'pause', ]; type Flags = Record; const flags = (): Flags => Object.fromEntries(ALL.map((a) => [a, false])) as Flags; export class Input implements InputService { device: 'keyboard' | 'gamepad' | 'touch' = 'keyboard'; axisX = 0; axisY = 0; private kb = flags(); private pad = flags(); private virt = flags(); private down = flags(); private pressedLatch = flags(); private releasedLatch = flags(); private stickX = 0; private stickY = 0; private vAxisX = 0; private vAxisY = 0; constructor(target: Window = window) { target.addEventListener('keydown', (e) => { const a = KEYS[e.code]; if (!a) return; if (!e.ctrlKey && !e.metaKey && !e.altKey) e.preventDefault(); if (e.repeat) return; this.device = 'keyboard'; // Varias teclas pueden mapear a la misma acción: se cuenta por código. this.codes.add(e.code); this.kb[a] = true; this.refresh(a); }); target.addEventListener('keyup', (e) => { const a = KEYS[e.code]; if (!a) return; this.codes.delete(e.code); let still = false; for (const c of this.codes) if (KEYS[c] === a) still = true; this.kb[a] = still; this.refresh(a); }); target.addEventListener('blur', () => { this.codes.clear(); for (const a of ALL) { this.kb[a] = false; this.virt[a] = false; this.refresh(a); } }); } private codes = new Set(); private refresh(a: Action): void { const now = this.kb[a] || this.pad[a] || this.virt[a]; if (now && !this.down[a]) this.pressedLatch[a] = true; if (!now && this.down[a]) this.releasedLatch[a] = true; this.down[a] = now; } /** Una vez por frame: lee el mando y recalcula los ejes. */ poll(): void { const pads = typeof navigator.getGamepads === 'function' ? navigator.getGamepads() : []; let gp: Gamepad | null = null; for (const p of pads) if (p && p.connected && p.mapping === 'standard') { gp = p; break; } const next = flags(); this.stickX = 0; this.stickY = 0; if (gp) { for (const [i, a] of PAD) if (gp.buttons[i]?.pressed) next[a] = true; const dz = 0.28; const sx = gp.axes[0] ?? 0; const sy = gp.axes[1] ?? 0; if (Math.abs(sx) > dz) this.stickX = sx; if (Math.abs(sy) > dz) this.stickY = -sy; if (sx < -0.5) next.left = true; if (sx > 0.5) next.right = true; if (sy < -0.6) next.up = true; if (sy > 0.6) next.down = true; } for (const a of ALL) { if (next[a] !== this.pad[a]) { this.pad[a] = next[a]; if (next[a]) this.device = 'gamepad'; this.refresh(a); } } const digX = (this.down.right ? 1 : 0) - (this.down.left ? 1 : 0); const digY = (this.down.up ? 1 : 0) - (this.down.down ? 1 : 0); this.axisX = this.vAxisX || this.stickX || digX; this.axisY = this.vAxisY || this.stickY || digY; } /** Al final de cada paso de simulación: limpia los flancos. */ endStep(): void { for (const a of ALL) { this.pressedLatch[a] = false; this.releasedLatch[a] = false; } } held(a: Action): boolean { return this.down[a]; } pressed(a: Action): boolean { return this.pressedLatch[a]; } released(a: Action): boolean { return this.releasedLatch[a]; } consume(a: Action): void { this.pressedLatch[a] = false; } setVirtual(a: Action, down: boolean): void { if (down) this.device = 'touch'; this.virt[a] = down; this.refresh(a); } setVirtualAxis(x: number, y: number): void { this.vAxisX = x; this.vAxisY = y; if (x || y) this.device = 'touch'; const set = (a: Action, v: boolean) => { if (this.virt[a] !== v) { this.virt[a] = v; this.refresh(a); } }; set('left', x < -0.3); set('right', x > 0.3); set('up', y > 0.55); set('down', y < -0.55); } }