import * as THREE from 'three'; import { emo, h, reducedMotion, type Ctx } from './dom'; import { richText } from './labels'; /** * Fundido «estrella»: una estrella sólida crece desde el héroe hasta cubrir la pantalla y luego se encoge. * Solo usa transform + clip-path (sin máscaras SVG, que algunos navegadores móviles colocaban mal). * Con movimiento reducido, fundido de opacidad simple. */ export class Fade { private star: HTMLElement; private simple: HTMLElement; private covered = false; private token = 0; private geom = { x: 0, y: 0, size: 1 }; constructor(private ctx: Ctx) { this.star = h('div', 'eh-starwipe', null, ctx.root); this.simple = h('div', 'eh-fade-simple', null, ctx.root); } private center(): { x: number; y: number; size: number } { const w = window.innerWidth || 1280; const hgt = window.innerHeight || 720; const g = this.ctx.game; let x = w / 2; let y = hgt / 2; if (g.hero && g.gfx?.camera && !g.loading) { const v = new THREE.Vector3(g.hero.x, g.hero.y + 0.4, 0).project(g.gfx.camera); if (Number.isFinite(v.x) && Math.abs(v.x) < 0.95 && Math.abs(v.y) < 0.95) { x = (v.x * 0.5 + 0.5) * w; y = (-v.y * 0.5 + 0.5) * hgt; } } // la estrella inscrita tiene radio interior ~0.38: se dimensiona para cubrir la esquina más lejana const far = Math.hypot(Math.max(x, w - x), Math.max(y, hgt - y)); return { x, y, size: (far / 0.36) * 2 * 1.05 }; } run(out: boolean, seconds = 0.4): Promise { const tok = ++this.token; const ms = Math.max(0, seconds * 1000); const useSimple = reducedMotion(); return new Promise((resolve) => { let finished = false; const end = () => { if (finished) return; finished = true; if (tok === this.token) this.apply(out ? 1 : 0, useSimple, true); this.covered = out; resolve(); }; if (ms < 20 || (out && this.covered && !useSimple)) { end(); return; } if (out || !this.covered) this.geom = this.center(); const t0 = performance.now(); const frame = () => { if (finished || tok !== this.token) return; const k = Math.min(1, (performance.now() - t0) / ms); // cover: 0 → 1 (la estrella crece), al abrir 1 → 0 this.apply(out ? easeOut(k) : 1 - easeIn(k), useSimple, false); if (k >= 1) end(); else requestAnimationFrame(frame); }; this.apply(out ? 0 : 1, useSimple, false); requestAnimationFrame(frame); // Garantía: sin rAF (pestaña en segundo plano) igual termina. window.setTimeout(end, ms + 60); }); } /** cover = 0 descubierto … 1 cubierto. */ private apply(cover: number, simple: boolean, final: boolean): void { if (simple) { this.star.style.display = 'none'; this.simple.style.display = cover <= 0 && final ? 'none' : 'block'; this.simple.style.opacity = String(cover); return; } this.simple.style.display = 'none'; if (cover <= 0 && final) { this.star.style.display = 'none'; return; } const { x, y, size } = this.geom; const s = this.star.style; s.display = 'block'; s.width = s.height = size + 'px'; s.transform = `translate(${x - size / 2}px, ${y - size / 2}px) rotate(${(1 - cover) * -140}deg) scale(${Math.max(0.0001, cover)})`; } } const easeIn = (k: number) => k * k * k; const easeOut = (k: number) => 1 - Math.pow(1 - k, 3); /** Avisos: toasts, rótulos, nombre de zona e indicador de interacción. */ export class Notices { private toasts: HTMLElement; private banner: HTMLElement | null = null; private zone: HTMLElement | null = null; private prompt: HTMLElement; private promptIn: HTMLElement; private promptState: { text: string | null; x?: number; y?: number } = { text: null }; private v = new THREE.Vector3(); constructor(private ctx: Ctx) { this.toasts = h('div', 'eh-toasts', null, ctx.root); this.prompt = h('div', 'eh-prompt eh-hidden', null, ctx.root); this.promptIn = h('div', 'eh-pill in', null, this.prompt); this.prompt.addEventListener('pointerdown', (ev) => { ev.preventDefault(); const i = ctx.game.input; i.setVirtual('interact', true); window.setTimeout(() => i.setVirtual('interact', false), 80); }); } toast(text: string, emoji?: string): void { const t = h('div', 'eh-toast', null, this.toasts); if (emoji) emo(emoji, t); h('span', '', text, t); while (this.toasts.children.length > 4) this.toasts.firstElementChild!.remove(); window.setTimeout(() => t.classList.add('out'), 2300); window.setTimeout(() => t.remove(), 2700); } showBanner(title: string, subtitle?: string, emoji?: string): void { this.banner?.remove(); const b = (this.banner = h('div', 'eh-banner', null, this.ctx.root)); if (emoji) { h('div', 'rays', null, b); emo(emoji, b, 'big'); for (let i = 0; i < 8; i++) { const a = (i / 8) * Math.PI * 2; const s = emo(i % 2 ? '✨' : '⭐', b, 'st'); s.style.setProperty('--dx', `${Math.cos(a) * 9}em`); s.style.setProperty('--dy', `${Math.sin(a) * 6}em`); s.style.setProperty('--r', `${(i % 2 ? 1 : -1) * 200}deg`); s.style.animationDelay = `${0.25 + i * 0.03}s`; } } h('div', 'eh-title-text t', title, b); if (subtitle) h('div', 'eh-pill s', subtitle, b); window.setTimeout(() => { if (this.banner === b) this.banner = null; b.remove(); }, 3500); } zoneName(name: string, emoji: string): void { this.zone?.remove(); const z = (this.zone = h('div', 'eh-zone', null, this.ctx.root)); emo(emoji, z); h('span', 'eh-title-text t', name, z); window.setTimeout(() => { if (this.zone === z) this.zone = null; z.remove(); }, 3300); } setPrompt(p: { text: string | null; x?: number; y?: number }): void { this.promptState = p ?? { text: null }; if (p?.text) richText(p.text, this.promptIn); this.prompt.classList.toggle('eh-hidden', !p?.text); } /** Por frame: proyecta la posición del indicador. */ render(hidden: boolean): void { const p = this.promptState; if (!p.text) return; this.prompt.classList.toggle('eh-hidden', hidden); if (hidden) return; const g = this.ctx.game; let sx = 0.5; let sy = 0.3; const wx = p.x ?? g.hero?.x; const wy = p.y ?? (g.hero ? g.hero.y + 1.4 : undefined); if (wx !== undefined && wy !== undefined && g.gfx?.camera) { this.v.set(wx, wy, 0).project(g.gfx.camera); sx = this.v.x * 0.5 + 0.5; sy = -this.v.y * 0.5 + 0.5; } const w = this.ctx.root.clientWidth; const hh = this.ctx.root.clientHeight; const x = Math.max(60, Math.min(w - 60, sx * w)); const y = Math.max(50, Math.min(hh - 10, sy * hh)); this.prompt.style.transform = `translate(${x.toFixed(1)}px, ${y.toFixed(1)}px)`; } }