import type { Action } from '../core/types'; import { emo, h, type Ctx, type Nav } from './dom'; import { GAMEPAD, KEYBOARD, keycap } from './labels'; /** Elemento navegable de un menú. */ export interface MenuItem { el: HTMLElement; activate?(): void; /** -1 / 1 con ←/→ (deslizadores). */ adjust?(dir: -1 | 1): void; } /** Lista vertical navegable con teclado, mando, ratón y toque. */ export class Menu { sel = 0; constructor(private ctx: Ctx, public items: MenuItem[], public onBack?: () => void) { items.forEach((it, i) => { it.el.addEventListener('pointerenter', (ev) => { if ((ev as PointerEvent).pointerType === 'mouse') this.select(i, false); }); it.el.addEventListener('click', (ev) => { ev.stopPropagation(); this.select(i, false); if (it.activate) { ctx.sfx('ui_ok'); it.activate(); } }); }); this.select(0, false); } select(i: number, sound = true): void { const n = this.items.length; if (!n) return; i = ((i % n) + n) % n; if (sound && i !== this.sel) this.ctx.sfx('ui_move'); this.sel = i; this.items.forEach((it, k) => it.el.classList.toggle('sel', k === i)); } handle(nav: Nav): void { if (nav.up) this.select(this.sel - 1); if (nav.down) this.select(this.sel + 1); const it = this.items[this.sel]; if (it?.adjust && (nav.left || nav.right)) it.adjust(nav.left ? -1 : 1); if (nav.ok && it?.activate) { this.ctx.sfx('ui_ok'); it.activate(); } else if (nav.back && this.onBack) { this.ctx.sfx('ui_back'); this.onBack(); } } } export function menuButton(parent: HTMLElement, emoji: string, label: string, cls = ''): HTMLButtonElement { const b = h('button', `eh-menu-btn ${cls}`.trim(), null, parent); emo(emoji, b); h('span', '', label, b); return b; } // ─────────────────────────── Logotipo ─────────────────────────── const STAR_PATH = 'M50 4 L62 36 L96 37 L69 58 L79 92 L50 72 L21 92 L31 58 L4 37 L38 36 Z'; export function starSvg(cls: string): SVGSVGElement { const ns = 'http://www.w3.org/2000/svg'; const svg = document.createElementNS(ns, 'svg'); svg.setAttribute('viewBox', '0 0 100 100'); svg.setAttribute('class', cls); svg.innerHTML = `` + `` + `` + ``; return svg; } function logo(parent: HTMLElement): HTMLElement { const wrap = h('div', 'eh-logo', null, parent); const ns = 'http://www.w3.org/2000/svg'; const sw = document.createElementNS(ns, 'svg'); sw.setAttribute('viewBox', '0 0 200 100'); sw.setAttribute('preserveAspectRatio', 'none'); sw.setAttribute('class', 'swoosh'); sw.innerHTML = ``; wrap.appendChild(sw); let i = 0; const row = (text: string, cls: string) => { const r = h('div', `row ${cls}`, null, wrap); for (const c of text) { const s = h('span', 'ch', c, r); s.dataset.t = c; s.style.setProperty('--i', String(i++)); } return r; }; const r1 = row('EMOJI', 'r1'); r1.appendChild(starSvg('star')); row('HEROES', 'r2'); return wrap; } // ─────────────────────────── Título ─────────────────────────── export class TitleScreen { readonly el: HTMLElement; readonly menu: Menu; constructor(private ctx: Ctx, hasSave: boolean, private done: (c: 'new' | 'continue') => void) { const el = (this.el = h('div', 'eh-titlescreen', null, ctx.root)); const left = h('div', 'eh-tleft', null, el); logo(left); const tag = h('div', 'eh-tagline', null, left); tag.append('Emojis con ', h('b', '', 'alma de héroes')); const right = h('div', 'eh-tright', null, el); const box = h('div', 'eh-panel eh-tmenu', null, right); const items: MenuItem[] = []; if (hasSave) items.push({ el: menuButton(box, '▶️', 'Continuar'), activate: () => this.pick('continue') }); items.push({ el: menuButton(box, '🌟', 'Nueva partida'), activate: () => this.pick('new') }); this.menu = new Menu(ctx, items); const help = h('div', 'eh-panel eh-thelp', null, right); h('span', 'eh-pill lbl', 'Controles', help); const g = ctx.game; const pair = (label: string, a: Action) => { const s = h('span', '', label, help); keycap(g, a, s); }; pair('Moverse', 'left'); pair('Saltar', 'jump'); pair('Golpear', 'attack'); pair('Especial', 'special'); pair('Hablar', 'interact'); pair('Pausa', 'pause'); h('div', 'eh-ver', 'v0.1 · Hecho con ❤️ y emojis', el); } private picked = false; private pick(c: 'new' | 'continue'): void { if (this.picked) return; this.picked = true; this.el.classList.add('out'); window.setTimeout(() => this.el.remove(), 550); this.done(c); } handle(nav: Nav): void { if (!this.picked) this.menu.handle({ ...nav, back: false }); } } // ─────────────────────────── Pausa ─────────────────────────── const CONTROL_ROWS: Array<[string, Action[]]> = [ ['Moverse', ['left', 'right']], ['Saltar', ['jump']], ['Golpear', ['attack']], ['Habilidad especial', ['special']], ['Botón 1 (herramienta)', ['tool']], ['Botón 2 (herramienta)', ['tool2']], ['Botón 3 (herramienta)', ['dig']], ['Mochila', ['bag']], ['Hablar / abrir', ['interact']], ['Cambiar poder', ['prevPower', 'nextPower']], ['Mapa', ['map']], ['Pausa', ['pause']], ]; export class PauseMenu { readonly el: HTMLElement; private box!: HTMLElement; private menu!: Menu; constructor(private ctx: Ctx, private close: () => void, private restart: () => void) { this.el = h('div', 'eh-pause', null, ctx.root); this.el.addEventListener('pointerdown', (ev) => { if (ev.target === this.el) this.close(); }); this.main(); } private page(title: string, wide = false): HTMLElement { this.box?.remove(); const b = (this.box = h('div', `eh-panel eh-pbox${wide ? ' wide' : ''}`, null, this.el)); h('div', 'eh-title-text eh-ptitle', title, b); return b; } main(sel = 0): void { const b = this.page('PAUSA'); this.menu = new Menu(this.ctx, [ { el: menuButton(b, '▶️', 'Continuar'), activate: () => this.close() }, { el: menuButton(b, '🎮', 'Controles'), activate: () => this.controls() }, { el: menuButton(b, '🔊', 'Volumen'), activate: () => this.volume() }, { el: menuButton(b, '🔄', 'Reiniciar partida', 'danger'), activate: () => this.confirm() }, ], () => this.close()); this.menu.select(sel, false); } private controls(): void { const b = this.page('CONTROLES', true); const t = h('div', 'eh-ctable', null, b); h('span', 'hd', 'Acción', t); h('span', 'hd', '⌨️ Teclado', t); h('span', 'hd', '🎮 Mando', t); for (const [label, acts] of CONTROL_ROWS) { h('span', '', label, t); const k = h('span', '', null, t); const p = h('span', 'pad', null, t); for (const a of acts) { h('span', 'eh-key', KEYBOARD[a], k); h('span', 'eh-key', GAMEPAD[a], p); } } h('div', 'eh-phint', 'Teclas 1-5: elegir EmojiPower directamente', b); this.menu = new Menu(this.ctx, [{ el: menuButton(b, '↩️', 'Volver'), activate: () => this.main(1) }], () => this.main(1)); } private volume(): void { const b = this.page('VOLUMEN'); const vol = this.ctx.game.audio.volume; const keys: Array<['master' | 'music' | 'sfx', string, string]> = [ ['master', '🔊', 'General'], ['music', '🎵', 'Música'], ['sfx', '💥', 'Efectos'], ]; const items: MenuItem[] = keys.map(([key, e, label]) => { const row = h('div', 'eh-vol', null, b); const lb = h('span', 'lb', null, row); emo(e, lb); h('span', '', label, lb); const minus = h('button', '', '−', row); const pips = h('div', 'pips', null, row); const plus = h('button', '', '+', row); const bars: HTMLElement[] = []; for (let i = 0; i < 10; i++) bars.push(h('i', '', null, pips)); const paint = () => { const n = Math.round((vol[key] ?? 0) * 10); bars.forEach((x, i) => x.classList.toggle('on', i < n)); }; const set = (v: number) => { vol[key] = Math.round(Math.max(0, Math.min(1, v)) * 10) / 10; paint(); this.ctx.sfx('ui_move', 0.8 + vol[key] * 0.6); }; const adjust = (d: -1 | 1) => set((vol[key] ?? 0) + d * 0.1); minus.addEventListener('click', (ev) => { ev.stopPropagation(); adjust(-1); }); plus.addEventListener('click', (ev) => { ev.stopPropagation(); adjust(1); }); pips.addEventListener('pointerdown', (ev) => { const r = pips.getBoundingClientRect(); set(Math.ceil(((ev.clientX - r.left) / r.width) * 10) / 10); }); paint(); return { el: row, adjust }; }); items.push({ el: menuButton(b, '↩️', 'Volver'), activate: () => this.main(2) }); h('div', 'eh-phint', '← → para ajustar', b); this.menu = new Menu(this.ctx, items, () => this.main(2)); } private confirm(): void { const b = this.page('¿REINICIAR?'); h('div', 'eh-ptext', 'Se borrará tu partida guardada y empezarás desde el principio. ¿Seguro?', b); this.menu = new Menu(this.ctx, [ { el: menuButton(b, '🙅', 'No, seguir jugando'), activate: () => this.main(3) }, { el: menuButton(b, '🔄', 'Sí, reiniciar', 'danger'), activate: () => this.restart() }, ], () => this.main(3)); } handle(nav: Nav): void { this.menu.handle(nav); } }