import { Game } from './core/game'; import { setChibiViewCamera } from './characters/chibi'; import { createRenderer } from './render'; import { emojiTexture } from './render/emoji'; import type { RendererExtras } from './render'; import { chooseEmojiPath, showDiagnostics, watchShaderErrors } from './diag'; import { setupDomEmoji } from './render/emojiDom'; import { install as installWorld } from './world'; import { install as installHero } from './entities/hero'; import { install as installEnemies } from './entities/enemies'; import { install as installNpcs } from './entities/npcs'; import { install as installItems } from './entities/items'; import { install as installQuests } from './quests'; import { install as installUi } from './ui'; import { install as installAudio } from './audio'; import { install as installVfx } from './vfx'; import { install as installDev } from './dev'; /** * Arranque. Parámetros de URL para pruebas: * ?zone=&spawn= entra directo a una zona (sin título ni partida guardada) * ?fresh ignora la partida guardada * * La portada aparece al instante; mientras se mira, detrás se cargan fuentes y emojis, se construye el * nivel y se compilan los shaders. Al elegir, solo se espera lo que falte. */ /** Emojis que el juego usa como objetos 3D: se piden a la fuente cuanto antes. */ const PRELOAD_EMOJIS = [ '🪙', '💎', '❤️', '🚩', '⭐', '🌙', '🌠', '✨', '🎈', '🌀', '🎁', '🎒', '🫧', '🔫', '🔦', '🪏', '🌧️', '💧', '🙂', '😊', '😡', '🥶', '🔥', '😎', '🤗', '🌝', '💖', '📮', '🐈', '🌷', '🌻', '🌼', '🚗', '🚙', '🚕', '🛵', '🚲', '🚦', '🧯', '🗑️', '📡', '🏗️', '🚧', '⛓️', '🧱', '🦺', '⛲', '🕊️', '🔔', '🛢️', '🏀', '🎠', '🦆', '🚒', '🚌', '🕳️', '⛏️', '🥐', '❗', '💬', ]; const wait = (ms: number) => new Promise((r) => setTimeout(r, ms)); const nextFrame = () => new Promise((r) => requestAnimationFrame(() => r())); async function boot(): Promise { const canvas = document.getElementById('game') as HTMLCanvasElement; const game = new Game(createRenderer(canvas)); (window as any).__eh = game; watchShaderErrors(game.gfx.three); setChibiViewCamera(game.gfx.camera); const gfx = game.gfx as typeof game.gfx & { onPhase?: (p: import('./core/types').DayPhase) => void }; gfx.onPhase = (phase) => game.events.emit('env:phase', { phase }); // El orden importa: servicios antes que quienes los usan al instalarse. installAudio(game); installVfx(game); installUi(game); installWorld(game); installHero(game); installEnemies(game); installNpcs(game); installItems(game); installQuests(game); installDev(game); const unlock = () => game.audio.unlock(); window.addEventListener('pointerdown', unlock, { once: true }); window.addEventListener('keydown', unlock, { once: true }); const params = new URLSearchParams(location.search); const debugZone = params.get('zone'); game.spawn('hero', 0, 2); game.start(); const hasSave = !debugZone && !params.has('fresh') && game.state.hasSave(); if (hasSave) game.state.load(); const startZone = debugZone ?? (game.hasZone(game.state.data.zone) ? game.state.data.zone : 'city'); const startSpawn = debugZone ? params.get('spawn') ?? 'default' : game.state.data.spawn; // Preparación en segundo plano (detrás de la portada). const prepare = (async () => { // Los emojis se redibujan solos cuando llega su fuente: no hace falta esperarla para jugar. for (const e of PRELOAD_EMOJIS) emojiTexture(e); // Pruebas de dibujo con la misma configuración que el juego: si los emojis no salen en esta GPU, se // elige automáticamente una alternativa (mezcla en vez de recorte, sin MSAA o material sencillo). const diag = chooseEmojiPath(game.gfx as typeof game.gfx & RendererExtras, params); setupDomEmoji(params); showDiagnostics(game.gfx.three, diag); await nextFrame(); await game.loadZone(startZone, startSpawn, false); const renderer = game.gfx.three as typeof game.gfx.three & { compileAsync?: (s: unknown, c: unknown) => Promise }; try { if (renderer.compileAsync) await Promise.race([renderer.compileAsync(game.gfx.scene, game.gfx.camera), wait(2500)]); else renderer.compile(game.gfx.scene, game.gfx.camera); } catch (err) { console.warn('[boot] precompilación de shaders', err); } })(); if (debugZone) { await prepare; return; } const choice = await game.ui.title(hasSave); await prepare; if (choice === 'new') { game.state.reset(); // El nivel se construyó con la partida guardada: si había una, se reconstruye desde cero. if (hasSave) { await game.loadZone('city', 'default'); return; } } // El nivel ya estaba listo: vuelve su música (la portada pone la suya). game.audio.music(game.zone?.music ?? null); } void boot();