import type { Game } from '../core/game'; import type { Action } from '../core/types'; import { h } from './dom'; export type Device = 'keyboard' | 'gamepad' | 'touch'; export const KEYBOARD: Record = { left: '←', right: '→', up: '↑', down: '↓', jump: 'Espacio', attack: 'J', special: 'K', tool: 'X', tool2: 'V', dig: 'C', bag: 'B', interact: 'E', nextPower: 'R', prevPower: 'Q', power1: '1', power2: '2', power3: '3', power4: '4', power5: '5', map: 'M', pause: 'Esc', }; export const GAMEPAD: Record = { left: '←', right: '→', up: '↑', down: '↓', jump: 'A', attack: 'B', special: 'RB', tool: 'X', tool2: 'R3', dig: 'Y', bag: 'L3', interact: '↑', nextPower: 'LB', prevPower: 'LT', power1: 'LB', power2: 'LB', power3: 'LB', power4: 'LB', power5: 'LB', map: 'Select', pause: 'Start', }; export const TOUCH: Record = { left: '◀', right: '▶', up: '▲', down: '▼', jump: '⬆️', attack: '👊', special: '✨', tool: '1️⃣', tool2: '2️⃣', dig: '3️⃣', bag: '🎒', interact: '💬', nextPower: '🙂', prevPower: '🙂', power1: '🙂', power2: '😡', power3: '🥶', power4: '🔥', power5: '😎', map: '🗺️', pause: '⏸️', }; /** Dispositivo efectivo: lo fija la UI (touchstart/teclado) y, si no, el de la entrada. */ let forced: Device | null = null; export function setForcedDevice(d: Device | null): void { forced = d; } export function currentDevice(game: Game): Device { return forced ?? game.input.device; } /** * Etiqueta del botón de una acción según el dispositivo en uso * (tecla, botón de mando o icono táctil). Ej.: `Pulsa [${actionLabel(game, 'interact')}] para hablar`. */ export function actionLabel(game: Game, action: Action): string { const d = currentDevice(game); return (d === 'gamepad' ? GAMEPAD : d === 'touch' ? TOUCH : KEYBOARD)[action] ?? '?'; } /** Elemento «tecla» que se actualiza solo al cambiar de dispositivo. */ export function keycap(game: Game, action: Action, parent?: Element): HTMLElement { const k = h('span', 'eh-key', actionLabel(game, action), parent); k.dataset.action = action; return k; } /** Refresca todas las teclas vivas bajo `root`. */ export function refreshKeycaps(game: Game, root: HTMLElement): void { root.querySelectorAll('.eh-key[data-action]').forEach((k) => { k.textContent = actionLabel(game, k.dataset.action as Action); }); } /** Convierte "Pulsa [E] para hablar" en nodos con la tecla resaltada. */ export function richText(text: string, parent: HTMLElement): void { parent.textContent = ''; const parts = text.split(/\[([^\]]+)\]/); parts.forEach((p, i) => { if (!p) return; if (i % 2) h('span', 'eh-key', p, parent); else parent.appendChild(document.createTextNode(p)); }); }