/** * Interfaz de Emoji Heroes: HUD, diálogos, avisos, título, pausa, automapa y controles táctiles. * HTML/CSS sobre #ui. Eventos propios (declaration merging): * 'map:poi' → punto de interés en el automapa. * 'ui:prompt' → indicador flotante de interacción ("Pulsa [E] para hablar"). */ import '@fontsource/fredoka/400.css'; import '@fontsource/fredoka/500.css'; import '@fontsource/fredoka/600.css'; import '@fontsource/fredoka/700.css'; import '@fontsource/noto-color-emoji'; import './style.css'; import type { Game } from '../core/game'; import type { DialogLine, RewardInfo, UiService } from '../core/types'; import { h, readNav, type Ctx } from './dom'; import { currentDevice, refreshKeycaps, setForcedDevice, type Device } from './labels'; import { Hud } from './hud'; import { DialogBox } from './dialog'; import { Fade, Notices } from './fx'; import { PauseMenu, TitleScreen } from './menus'; import { AutoMap } from './map'; import { TouchControls } from './touch'; import { RewardCard } from './reward'; import { BackpackMenu } from './backpack'; export { actionLabel } from './labels'; declare module '../core/types' { interface GameEvents { /** Punto de interés para el automapa (cofres, puertas, tiendas, teletransportes…). Se guarda en flags. */ 'map:poi': { zone: string; x: number; y: number; emoji: string; id: string }; /** Indicador flotante de interacción; text null lo oculta. x/y en mundo (por defecto, sobre el héroe). */ 'ui:prompt': { text: string | null; x?: number; y?: number }; } } class Ui implements UiService { private ctx: Ctx; private hud: Hud; private dlg: DialogBox; private fx: Notices; private fader: Fade; private rewardCard: RewardCard; private map: AutoMap; private touch: TouchControls; private titleScr: TitleScreen | null = null; private pauseMenu: PauseMenu | null = null; private bag: BackpackMenu | null = null; private mapOpen = false; private device: Device | '' = ''; constructor(private game: Game, host: HTMLElement) { const root = h('div', 'eh-root', null, host); this.ctx = { game, root, sfx: (id, pitch) => game.audio.sfx(id, pitch ? { pitch } : undefined), }; this.hud = new Hud(this.ctx); this.touch = new TouchControls(this.ctx, () => this.toggleMap(), () => this.openPause(), () => this.toggleBag()); this.fx = new Notices(this.ctx); this.dlg = new DialogBox(this.ctx); this.map = new AutoMap(this.ctx); this.fader = new Fade(this.ctx); this.rewardCard = new RewardCard(this.ctx); const rot = h('div', 'eh-rotate', null, root); h('span', 'eh-emo', '📱', rot); h('div', 'eh-title-text', '¡Gira tu dispositivo!', rot); h('p', '', 'Emoji Heroes se juega en horizontal. Pon el teléfono de lado y ¡a la aventura!', rot); // Detección de dispositivo para mostrar los controles correctos. window.addEventListener('touchstart', () => setForcedDevice('touch'), { passive: true, capture: true }); window.addEventListener('pointerdown', (e) => { if (e.pointerType === 'touch') setForcedDevice('touch'); }, { capture: true }); window.addEventListener('keydown', () => setForcedDevice(null), { capture: true }); window.addEventListener('gamepadconnected', () => setForcedDevice(null)); const ev = game.events; const refresh = () => { this.hud.refresh(); this.touch.refresh(); }; ev.on('state:changed', refresh); ev.on('hero:power', refresh); ev.on('hero:hurt', refresh); ev.on('flag', (f) => { if (!f.key.startsWith('map.')) refresh(); }); ev.on('zone:loaded', () => { const z = game.zone; if (z && !this.titleScr) this.fx.zoneName(z.name, z.emoji); this.fx.setPrompt({ text: null }); }); ev.on('ui:prompt', (p) => this.fx.setPrompt(p)); refresh(); void checkEmojiFont(root); } // ─────────── UiService ─────────── get modalOpen(): boolean { return this.dlg.open || this.rewardCard.open || !!this.titleScr || !!this.pauseMenu || !!this.bag || this.mapOpen; } reward(info: RewardInfo): Promise { this.touch.releaseAll(); return this.rewardCard.show(info); } dialog(lines: DialogLine[]): Promise { this.touch.releaseAll(); return this.dlg.show(lines, null).then(() => undefined); } choice(prompt: DialogLine, options: string[]): Promise { this.touch.releaseAll(); return this.dlg.show([prompt], options); } toast(text: string, emoji?: string): void { this.fx.toast(text, emoji); } banner(title: string, subtitle?: string, emoji?: string): void { this.fx.showBanner(title, subtitle, emoji); } setObjective(text: string | null): void { this.hud.setObjective(text); } bossBar(info: { name: string; emoji: string; frac: number } | null): void { this.hud.bossBar(info); } fade(out: boolean, seconds?: number): Promise { return this.fader.run(out, seconds); } title(hasSave: boolean): Promise<'new' | 'continue'> { this.titleScr?.el.remove(); this.ctx.root.classList.add('eh-title-open'); this.game.audio.music('title'); return new Promise((resolve) => { this.titleScr = new TitleScreen(this.ctx, hasSave, (c) => { this.titleScr = null; this.ctx.root.classList.remove('eh-title-open'); this.game.audio.unlock(); resolve(c); }); }); } // ─────────── Pausa y mapa ─────────── private openPause(): void { if (this.modalOpen || this.game.loading) return; this.touch.releaseAll(); this.game.paused = true; this.ctx.sfx('ui_ok'); this.pauseMenu = new PauseMenu(this.ctx, () => this.closePause(), () => this.restart()); } private closePause(): void { if (!this.pauseMenu) return; this.ctx.sfx('ui_back'); this.pauseMenu.el.remove(); this.pauseMenu = null; this.game.paused = false; } private restart(): void { this.pauseMenu?.el.remove(); this.pauseMenu = null; this.game.paused = false; this.hud.bossBar(null); this.hud.setObjective(null); this.game.state.reset(); const z = this.game.state.data.zone; void this.game.loadZone(this.game.hasZone(z) ? z : this.game.zone?.id ?? 'test', this.game.state.data.spawn); } /** Mochila: solo cuando Emma ya la ha regalado. */ private toggleBag(): void { if (this.bag) { this.bag.el.remove(); this.bag = null; this.game.paused = false; this.ctx.sfx('ui_back'); return; } if (this.modalOpen || this.game.loading || !this.game.state.has('hero.backpack')) return; this.touch.releaseAll(); this.game.paused = true; this.ctx.sfx('ui_ok'); this.bag = new BackpackMenu(this.ctx, () => this.toggleBag()); } private toggleMap(): void { if (this.mapOpen) { this.mapOpen = false; this.map.hide(); this.ctx.sfx('ui_back'); return; } if (this.modalOpen || this.game.loading || !this.game.zone) return; this.touch.releaseAll(); this.mapOpen = true; this.ctx.sfx('ui_ok'); this.map.show(() => this.toggleMap()); } // ─────────── Sistema ─────────── /** Paso de simulación (corre también en pausa). */ step(frozen: boolean): void { const input = this.game.input; if (this.titleScr) { this.titleScr.handle(readNav(this.game)); return; } if (this.dlg.open) { this.dlg.step(); return; } if (this.rewardCard.open) { this.rewardCard.step(); return; } if (this.pauseMenu) { this.pauseMenu.handle(readNav(this.game)); return; } if (this.bag) { const close = input.pressed('bag'); input.consume('bag'); if (close) this.toggleBag(); else this.bag.handle(readNav(this.game)); return; } if (this.mapOpen) { const m = input.pressed('map'); const n = readNav(this.game); if (m || n.ok || n.back) this.toggleMap(); return; } if (!this.game.loading) { if (input.pressed('pause')) { input.consume('pause'); this.openPause(); return; } if (input.pressed('map')) { input.consume('map'); this.toggleMap(); return; } if (input.pressed('bag')) { input.consume('bag'); this.toggleBag(); return; } } if (!frozen) this.map.sample(); } /** Por frame: dispositivo, HUD e indicador flotante. */ render(): void { const d = currentDevice(this.game); if (d !== this.device) { this.device = d; this.ctx.root.dataset.device = d; refreshKeycaps(this.game, this.ctx.root); } const modal = this.modalOpen; this.ctx.root.classList.toggle('eh-modal', modal); this.hud.refresh(); this.fx.render(modal || this.game.loading); } } /** * Algunos navegadores no pintan la fuente Noto Color Emoji (COLRv1): se prueba en un canvas * y, si sale vacía, se usa la fuente de emojis del sistema. */ async function checkEmojiFont(root: HTMLElement): Promise { try { await Promise.race([ document.fonts.load('32px "Noto Color Emoji"', '❤️🪙'), new Promise((r) => window.setTimeout(r, 4000)), ]); const c = document.createElement('canvas'); c.width = c.height = 40; const x = c.getContext('2d', { willReadFrequently: true }); if (!x) return; x.font = '32px "Noto Color Emoji"'; x.textBaseline = 'top'; x.fillText('❤️', 2, 2); const d = x.getImageData(0, 0, 40, 40).data; for (let i = 3; i < d.length; i += 4) if (d[i] > 0) return; root.classList.add('eh-sysemoji'); } catch { root.classList.add('eh-sysemoji'); } } export function install(game: Game): void { const host = document.getElementById('ui') ?? document.body; const ui = new Ui(game, host); game.ui = ui; game.addSystem({ always: true, update: (_dt, frozen) => ui.step(frozen), render: () => ui.render(), }); (window as any).__ehUi = ui; }