import type { Game } from '../core/game'; import type { Action } from '../core/types'; /** Crea un elemento con clase, texto y padre opcionales. */ export function h( tag: K, cls?: string, text?: string | null, parent?: Element, ): HTMLElementTagNameMap[K] { const e = document.createElement(tag); if (cls) e.className = cls; if (text != null) e.textContent = text; if (parent) parent.appendChild(e); return e; } /** Emoji con la fuente de color. */ export function emo(emoji: string, parent?: Element, cls = ''): HTMLSpanElement { return h('span', `eh-emo ${cls}`.trim(), emoji, parent); } /** Reinicia una animación CSS asociada a una clase. */ export function kick(el: HTMLElement, cls: string, ms = 1000): void { el.classList.remove(cls); void el.offsetWidth; el.classList.add(cls); window.setTimeout(() => el.classList.remove(cls), ms); } export const reducedMotion = (): boolean => typeof matchMedia === 'function' && matchMedia('(prefers-reduced-motion: reduce)').matches; /** Contexto compartido por las piezas de la UI. */ export interface Ctx { game: Game; root: HTMLElement; sfx(id: string, pitch?: number): void; } /** Lectura de navegación de menús (un paso de simulación). */ export interface Nav { up: boolean; down: boolean; left: boolean; right: boolean; ok: boolean; back: boolean } const NAV_ACTIONS: Action[] = ['up', 'down', 'left', 'right', 'jump', 'interact', 'attack', 'pause', 'map']; export function readNav(game: Game): Nav { const i = game.input; const up = i.pressed('up'); const nav: Nav = { up, down: i.pressed('down'), left: i.pressed('left'), right: i.pressed('right'), // En mando, ↑ de la cruceta también es «interact»: no debe confirmar. ok: i.pressed('jump') || (i.pressed('interact') && !up), back: i.pressed('pause') || i.pressed('attack'), }; for (const a of NAV_ACTIONS) i.consume(a); return nav; }