/** * Tarjeta de recompensa: modal que aparece al obtener un poder, una herramienta o un objeto especial. * Tarjeta con medallón y el emoji correspondiente, categoría, nombre y descripción. * Pausa el juego (modalOpen) hasta que el jugador la cierra; las tarjetas seguidas se encolan. */ import type { RewardInfo } from '../core/types'; import { emo, h, type Ctx } from './dom'; import { keycap } from './labels'; const KIND_LABEL: Record = { power: 'Nuevo EmojiPower', tool: 'Nueva herramienta', item: 'Objeto especial', }; const KIND_COLOR: Record = { power: '#ff7ac0', tool: '#4fc3ff', item: '#ffc928', }; export class RewardCard { private el: HTMLElement; private queue: Array<{ info: RewardInfo; done: () => void }> = []; private current: { info: RewardInfo; done: () => void } | null = null; private guard = 0; private shownAt = 0; constructor(private ctx: Ctx) { this.el = h('div', 'eh-reward eh-hidden', null, ctx.root); this.el.addEventListener('pointerdown', (e) => { e.preventDefault(); this.close(); }); } get open(): boolean { return !!this.current; } show(info: RewardInfo): Promise { return new Promise((done) => { this.queue.push({ info, done }); if (!this.current) this.next(); }); } private next(): void { const item = this.queue.shift(); if (!item) { this.current = null; this.el.classList.add('eh-hidden'); return; } this.current = item; this.guard = 20; this.shownAt = performance.now(); const { info } = item; const el = this.el; el.textContent = ''; el.classList.remove('eh-hidden', 'out'); el.style.setProperty('--accent', info.color ?? KIND_COLOR[info.kind]); h('div', 'eh-reward-dim', null, el); const card = h('div', 'eh-reward-card', null, el); h('div', 'eh-reward-rays', null, card); h('div', 'eh-pill eh-reward-kind', KIND_LABEL[info.kind], card); const medal = h('div', 'eh-reward-medal', null, card); emo(info.emoji, medal, 'eh-reward-emoji'); for (let i = 0; i < 6; i++) { const s = emo(i % 2 ? '✨' : '⭐', card, 'eh-reward-spark'); const a = (i / 6) * Math.PI * 2 - Math.PI / 2; s.style.setProperty('--x', `${Math.cos(a) * 7.2}em`); s.style.setProperty('--y', `${Math.sin(a) * 6.2 - 2.4}em`); s.style.animationDelay = `${0.35 + i * 0.06}s`; } h('div', 'eh-title-text eh-reward-name', info.name, card); if (info.desc) h('div', 'eh-reward-desc', info.desc, card); const hint = h('div', 'eh-reward-hint', null, card); keycap(this.ctx.game, 'jump', hint); h('span', '', 'Continuar', hint); this.ctx.sfx('powerup', 1.1); } private closing = false; private close(): void { if (!this.current || this.closing || performance.now() - this.shownAt < 450) return; const cur = this.current; this.closing = true; this.el.classList.add('out'); this.ctx.sfx('ui_ok'); // El modal sigue "abierto" durante la salida para que el juego no se reanude entre tarjetas. window.setTimeout(() => { this.closing = false; cur.done(); this.next(); }, 220); } /** Lee la entrada desde el sistema de la UI (un paso de simulación). */ step(): void { if (!this.current) return; const i = this.ctx.game.input; const ok = i.pressed('jump') || i.pressed('interact') || i.pressed('attack'); for (const a of ['jump', 'attack', 'interact', 'up', 'down', 'pause', 'map', 'special', 'tool'] as const) i.consume(a); if (this.guard > 0) { this.guard--; return; } if (ok) this.close(); } }