import type { DialogLine } from '../core/types'; import { emo, h, kick, reducedMotion, type Ctx } from './dom'; import { keycap } from './labels'; const CHAR_MS = 26; /** * Caja de diálogo inferior con retrato, nombre y máquina de escribir. * Un mismo objeto atiende `dialog` (varias líneas) y `choice` (una línea + opciones). */ export class DialogBox { readonly el: HTMLElement; private face: HTMLElement; private faceEmo: HTMLElement; private name: HTMLElement; private typed: HTMLElement; private ghost: HTMLElement; private opts: HTMLElement; private lines: DialogLine[] = []; private idx = 0; private text = ''; private shown = 0; private timer = 0; private lineTok = 0; private options: string[] | null = null; private optEls: HTMLElement[] = []; private optSel = 0; private resolve: ((v: number) => void) | null = null; /** Pasos a ignorar tras abrir (evita que la misma pulsación avance). */ private guard = 0; open = false; constructor(private ctx: Ctx) { const el = (this.el = h('div', 'eh-dialog eh-hidden', null, ctx.root)); const box = h('div', 'eh-panel eh-dbox', null, el); this.face = h('div', 'eh-dface', null, box); this.faceEmo = emo('🙂', this.face); this.name = h('div', 'eh-pill eh-dname', '', box); const txt = h('div', 'eh-dtext', null, box); this.typed = h('span', '', '', txt); this.ghost = h('span', 'ghost', '', txt); const next = h('div', 'eh-dnext', null, box); keycap(ctx.game, 'interact', next); h('span', 'arrow', '▼', next); this.opts = h('div', 'eh-dopts', null, box); el.addEventListener('pointerdown', (ev) => { ev.preventDefault(); if ((ev.target as HTMLElement).closest('.eh-dopt')) return; if (this.guard <= 0) this.advance(); }); } show(lines: DialogLine[], options: string[] | null): Promise { // Si ya había uno abierto, se cierra resolviéndolo. this.finish(0); this.lines = lines.length ? lines : [{ speaker: '', emoji: '💬', text: '' }]; this.options = options && options.length ? options : null; this.idx = 0; this.open = true; this.guard = 6; this.el.classList.remove('eh-hidden', 'out'); this.ctx.root.classList.add('eh-dialog-open'); const box = this.el.firstElementChild as HTMLElement; kick(box, 'eh-dbox-anim', 10); this.startLine(true); return new Promise((res) => (this.resolve = res)); } private startLine(first = false): void { const l = this.lines[this.idx]; if (!first && this.faceEmo.textContent !== l.emoji) kick(this.face, 'swap', 520); this.faceEmo.textContent = l.emoji || '💬'; this.name.textContent = l.speaker; this.name.classList.toggle('eh-hidden', !l.speaker); this.text = l.text; this.shown = 0; this.renderText(); this.clearOptions(); this.el.classList.remove('ready', 'choosing'); this.el.classList.add('typing'); window.clearInterval(this.timer); this.lineTok++; if (reducedMotion()) { this.complete(); return; } this.timer = window.setInterval(() => this.tick(), CHAR_MS); } private tick(): void { if (this.shown >= this.text.length) { this.complete(); return; } const c = this.text[this.shown]; this.shown++; // Pausa breve tras signos de puntuación. if (/[.!?¡¿,…]/.test(c)) { window.clearInterval(this.timer); const tok = this.lineTok; window.setTimeout(() => { if (tok !== this.lineTok) return; if (this.open && this.shown < this.text.length) this.timer = window.setInterval(() => this.tick(), CHAR_MS); else if (this.open) this.complete(); }, c === ',' ? 90 : 170); } if (this.shown % 3 === 1 && /\S/.test(c)) this.ctx.sfx('talk', 0.9 + Math.random() * 0.3); this.renderText(); } private renderText(): void { this.typed.textContent = this.text.slice(0, this.shown); this.ghost.textContent = this.text.slice(this.shown); } private get typing(): boolean { return this.shown < this.text.length; } private complete(): void { window.clearInterval(this.timer); this.shown = this.text.length; this.renderText(); this.el.classList.remove('typing'); this.el.classList.add('ready'); if (this.options && this.idx === this.lines.length - 1 && !this.optEls.length) this.showOptions(); } private showOptions(): void { this.el.classList.add('choosing'); this.optSel = 0; this.optEls = this.options!.map((o, i) => { const b = h('button', 'eh-dopt', o, this.opts); b.style.animationDelay = `${i * 0.06}s`; b.addEventListener('pointerenter', () => this.selectOpt(i)); b.addEventListener('pointerdown', (ev) => { ev.preventDefault(); ev.stopPropagation(); this.selectOpt(i); this.ctx.sfx('ui_ok'); this.finish(i); }); return b; }); this.selectOpt(0, false); } private selectOpt(i: number, sound = true): void { const n = this.optEls.length; i = ((i % n) + n) % n; if (sound && i !== this.optSel) this.ctx.sfx('ui_move'); this.optSel = i; this.optEls.forEach((b, k) => b.classList.toggle('sel', k === i)); } private clearOptions(): void { this.opts.textContent = ''; this.optEls = []; } /** Primer toque completa el texto; el segundo avanza. */ advance(): void { if (!this.open) return; if (this.typing) { this.complete(); return; } if (this.optEls.length) { this.ctx.sfx('ui_ok'); this.finish(this.optSel); return; } if (this.idx < this.lines.length - 1) { this.idx++; this.ctx.sfx('ui_move', 1.2); this.startLine(); return; } this.ctx.sfx('ui_ok'); this.finish(0); } private finish(v: number): void { if (!this.open) return; this.open = false; window.clearInterval(this.timer); this.ctx.root.classList.remove('eh-dialog-open'); this.el.classList.add('out'); this.el.classList.remove('typing', 'ready', 'choosing'); window.setTimeout(() => { if (!this.open) this.el.classList.add('eh-hidden'); }, 200); const r = this.resolve; this.resolve = null; r?.(v); } /** Lee la entrada desde un sistema `always` (un paso de simulación). */ step(): void { if (!this.open) return; const i = this.ctx.game.input; const okA = i.pressed('jump') || i.pressed('attack'); const up = i.pressed('up'); const down = i.pressed('down'); const inter = i.pressed('interact') && !(this.optEls.length && up); for (const a of ['jump', 'attack', 'interact', 'up', 'down', 'pause', 'map'] as const) i.consume(a); if (this.guard > 0) { this.guard--; return; } if (this.optEls.length) { if (up) this.selectOpt(this.optSel - 1); if (down) this.selectOpt(this.optSel + 1); } if (okA || inter) this.advance(); } }