/** * Panel flotante del modo desarrollador (solo con `?dev=1`). * * Overlay HTML sobre #ui con el estilo del juego (paneles índigo, esquinas * redondeadas, títulos amarillos, Fredoka, todo en español). El juego sigue * corriendo mientras el panel está abierto; cerrado no bloquea nada. * * - Poderes: dar cada uno (o todos) y cambiar el activo. Tocar de nuevo el * poder activo lo desactiva (se quita y se vuelve a la forma base). * - Herramientas: dar cada una (o todas). * - Vida y tesoro: curar al máximo, monedas y diamantes. * - Viajar: teletransporte a cada zona y spawn. * - Misiones: marcar flags para saltar flujos + «Completar tramo 1». * - Modo dios: invulnerable (cubre golpes y caídas). * - Atajos: F2 o ` abren/cierran el panel. */ import type { Game } from '../core/game'; import type { HeroApi, PowerId, ToolId } from '../core/types'; import { autoAssignSlots } from '../core/slots'; import { h } from '../ui/dom'; interface PowerDef { id: PowerId; emoji: string; name: string } interface ToolDef { id: ToolId; emoji: string; name: string } interface FlagDef { key: string; emoji: string; label: string } const POWERS: PowerDef[] = [ { id: 'base', emoji: '🙂', name: 'Base' }, { id: 'fury', emoji: '😡', name: 'FURIA' }, { id: 'ice', emoji: '🥶', name: 'HIELO' }, { id: 'fire', emoji: '🔥', name: 'FUEGO' }, { id: 'cool', emoji: '😎', name: 'COOL' }, ]; // Nombres y emojis iguales que en src/entities/hero/hero.ts (TOOLS). const TOOLS: ToolDef[] = [ { id: 'bubbles', emoji: '🫧', name: 'BURBUJAS' }, { id: 'watergun', emoji: '🔫', name: 'PISTOLA DE AGUA' }, { id: 'shovel', emoji: '🪏', name: 'PALA' }, { id: 'rain', emoji: '🌧️', name: 'LLUVIA' }, { id: 'flashlight', emoji: '🔦', name: 'LINTERNA' }, ]; // Flags del contrato del tramo 1 (ver CLAUDE.md), en orden de la historia. const FLAGS: FlagDef[] = [ { key: 'emma.met', emoji: '🤗', label: 'Conociste a Emma' }, { key: 'luna.defeated', emoji: '🌝', label: 'Luna derrotada' }, { key: 'horde.cleared', emoji: '🌙', label: 'Horda superada' }, { key: 'eddy.met', emoji: '💧', label: 'Conociste a Eddy' }, { key: 'lagoon.crossed', emoji: '🪷', label: 'Laguna cruzada' }, { key: 'eddy.bubbles', emoji: '🫧', label: 'Eddy: burbujas' }, { key: 'eddy.watergun', emoji: '🔫', label: 'Eddy: pistola de agua' }, { key: 'eddy.reward', emoji: '🎁', label: 'Recompensa de Eddy' }, { key: 'cave.ring', emoji: '💍', label: 'Anillo encontrado' }, { key: 'emma.ringReturned', emoji: '🤗', label: 'Anillo devuelto a Emma' }, ]; const SPAWN_LABELS: Record = { default: 'Inicio', 'from-city': 'Desde la ciudad', 'from-lagoon': 'Desde la laguna', 'tp-city': 'Teletransporte', 'tp-lagoon': 'Teletransporte', tiendas: 'Tiendas', plaza: 'Plaza', eddy: 'Junto a Eddy', 'far-shore': 'Orilla lejana', cave: 'Entrada de la cueva', right: 'Derecha', }; export class DevPanel { private root: HTMLElement; private panelEl: HTMLElement; private fab: HTMLButtonElement; private statusEl!: HTMLElement; // lo crea buildPanel(), llamado desde el constructor private open = false; private godMode = false; private origTakeHit: HeroApi['takeHit'] | null = null; private powerBtns = new Map(); private toolBtns = new Map(); private flagBtns = new Map(); private zoneHeads = new Map(); private godBtn!: HTMLButtonElement; // lo crea buildPanel(), llamado desde el constructor constructor(private game: Game, host: HTMLElement) { this.root = h('div', 'eh-dev-root', null, host); // Rótulo siempre visible mientras el modo está activo (no bloquea toques). h('div', 'eh-dev-badge', '🛠️ Modo desarrollador', this.root); // Botón flotante para abrir/cerrar (ratón y táctil). this.fab = h('button', 'eh-dev-fab', '🛠️', this.root); this.fab.type = 'button'; this.fab.setAttribute('aria-label', 'Abrir el panel de desarrollador'); this.fab.addEventListener('click', () => this.toggle()); this.panelEl = h('div', 'eh-dev-panel eh-dev-hidden', null, this.root); this.buildPanel(); // Atajos: F2 o ` (ninguna es acción del juego). No choca con los controles. window.addEventListener('keydown', (e) => { if (e.code === 'F2' || e.code === 'Backquote') { e.preventDefault(); this.toggle(); } }); // Refresca los estados cuando el juego cambia (poderes, flags, zona, vida). game.events.on('state:changed', () => this.refresh()); game.events.on('flag', () => this.refresh()); game.events.on('hero:power', () => this.refresh()); game.events.on('zone:loaded', () => this.refresh()); // El modo dios también cubre el daño por caída (que no pasa por takeHit): // al sonar 'hero:hurt' se devuelve la vida al máximo. game.events.on('hero:hurt', () => { if (!this.godMode) return; const hero = this.game.hero; const st = this.game.state; if (hero && hero.alive && st.data.hp < st.data.maxHp) { st.data.hp = st.data.maxHp; st.changed(); } }); this.refresh(); } // ───────────── construcción ───────────── private buildPanel(): void { const head = h('div', 'eh-dev-head', null, this.panelEl); h('h2', 'eh-dev-title', '🛠️ Modo desarrollador', head); const close = h('button', 'eh-dev-close', '✕', head); close.type = 'button'; close.setAttribute('aria-label', 'Cerrar el panel de desarrollador'); close.addEventListener('click', () => this.close()); this.statusEl = h('div', 'eh-dev-status', '', this.panelEl); const body = h('div', 'eh-dev-body', null, this.panelEl); // ── Poderes ── const powers = this.section(body, 'Poderes', '✨'); const pg = h('div', 'eh-dev-grid', null, powers); for (const p of POWERS) { const b = this.mkButton(pg, p.emoji, p.name); b.setAttribute('aria-label', `Dar y activar el poder ${p.name}`); b.addEventListener('click', () => this.usePower(p)); this.powerBtns.set(p.id, b); } const allP = this.mkButton(pg, '🌟', 'Dar todos los poderes', true); allP.addEventListener('click', () => this.grantAllPowers()); h('div', 'eh-dev-hint', 'Toca de nuevo el poder activo para desactivarlo.', powers); // ── Herramientas ── const tools = this.section(body, 'Herramientas', '🎒'); const tg = h('div', 'eh-dev-grid', null, tools); for (const t of TOOLS) { const b = this.mkButton(tg, t.emoji, t.name); b.setAttribute('aria-label', `Dar la herramienta ${t.name}`); b.addEventListener('click', () => this.grantTool(t)); this.toolBtns.set(t.id, b); } const allT = this.mkButton(tg, '🧰', 'Dar todas las herramientas', true); allT.addEventListener('click', () => this.grantAllTools()); // ── Vida y tesoro ── const vault = this.section(body, 'Vida y tesoro', '❤️'); const vg = h('div', 'eh-dev-grid', null, vault); const heal = this.mkButton(vg, '❤️', 'Curar al máximo'); heal.addEventListener('click', () => this.healMax()); const coins = this.mkButton(vg, '🪙', '+100 monedas'); coins.addEventListener('click', () => this.addCoins()); const gems = this.mkButton(vg, '💎', '+10 diamantes'); gems.addEventListener('click', () => this.addDiamonds()); const save = this.mkButton(vg, '💾', 'Guardar partida'); save.addEventListener('click', () => this.saveGame()); // ── Viajar ── const travel = this.section(body, 'Viajar', '🗺️'); for (const id of this.game.zoneIds()) { const def = this.game.getZone(id); if (!def) continue; const zh = h('div', 'eh-dev-zone', `${def.emoji} ${def.name}`, travel); this.zoneHeads.set(id, zh); const zg = h('div', 'eh-dev-chips', null, travel); for (const spawn of Object.keys(def.spawns)) { const b = this.mkButton(zg, '📍', SPAWN_LABELS[spawn] ?? spawn); b.classList.add('eh-dev-chip'); b.setAttribute('aria-label', `Viajar a ${def.name}: ${SPAWN_LABELS[spawn] ?? spawn}`); b.addEventListener('click', () => void this.travel(id, spawn)); } } // ── Misiones ── const quests = this.section(body, 'Misiones (saltar flujos)', '📜'); const qg = h('div', 'eh-dev-grid', null, quests); for (const f of FLAGS) { const b = this.mkButton(qg, f.emoji, f.label); b.setAttribute('aria-label', `Marcar: ${f.label}`); b.addEventListener('click', () => this.setFlag(f)); this.flagBtns.set(f.key, b); } const tramo = this.mkButton(qg, '🏆', 'Completar tramo 1 (Ciudad + Laguna)', true); tramo.classList.add('eh-dev-gold'); tramo.addEventListener('click', () => this.completeTramo1()); // ── Modo dios ── const god = this.section(body, 'Modo dios', '🛡️'); const gg = h('div', 'eh-dev-grid', null, god); this.godBtn = this.mkButton(gg, '🛡️', 'Invulnerable (golpes y caídas)', true); this.godBtn.setAttribute('aria-label', 'Activar o desactivar el modo dios'); this.godBtn.addEventListener('click', () => this.setGod(!this.godMode)); h('div', 'eh-dev-hint', 'Atajo: F2 o ` para abrir y cerrar el panel.', body); } private section(parent: HTMLElement, title: string, emoji: string): HTMLElement { const s = h('section', 'eh-dev-section', null, parent); h('h3', 'eh-dev-section-title', `${emoji} ${title}`, s); return s; } private mkButton(parent: HTMLElement, emoji: string, label: string, wide = false): HTMLButtonElement { const b = h('button', wide ? 'eh-dev-btn eh-dev-wide' : 'eh-dev-btn', null, parent); b.type = 'button'; h('span', 'eh-dev-emo', emoji, b); h('span', 'eh-dev-label', label, b); h('span', 'eh-dev-check', '✓', b); return b; } // ───────────── abrir / cerrar ───────────── toggle(): void { if (this.open) this.close(); else this.openPanel(); } private openPanel(): void { this.open = true; this.panelEl.classList.remove('eh-dev-hidden'); this.fab.classList.add('eh-dev-hidden'); this.refresh(); this.game.audio.sfx('ui_ok'); } private close(): void { this.open = false; this.panelEl.classList.add('eh-dev-hidden'); this.fab.classList.remove('eh-dev-hidden'); this.game.audio.sfx('ui_back'); } // ───────────── estados ───────────── /** Actualiza marcas (✓), poder activo, modo dios y línea de estado. */ refresh(): void { const st = this.game.state; const active = st.data.power; for (const p of POWERS) { const b = this.powerBtns.get(p.id); if (!b) continue; b.classList.toggle('is-owned', st.hasPower(p.id)); b.classList.toggle('is-active', active === p.id); // El botón del poder activo desactiva al tocarlo de nuevo (salvo la base). b.setAttribute('aria-label', active === p.id && p.id !== 'base' ? `Desactivar el poder ${p.name}` : `Dar y activar el poder ${p.name}`); } for (const t of TOOLS) { const b = this.toolBtns.get(t.id); if (b) b.classList.toggle('is-owned', st.hasTool(t.id)); } for (const f of FLAGS) { const b = this.flagBtns.get(f.key); if (b) b.classList.toggle('is-owned', st.has(f.key)); } this.godBtn?.classList.toggle('is-on', this.godMode); const zone = this.game.zone; for (const [id, el] of this.zoneHeads) { const def = this.game.getZone(id); if (!def) continue; el.textContent = zone?.id === id ? `${def.emoji} ${def.name} · estás aquí 📍` : `${def.emoji} ${def.name}`; } const power = POWERS.find((p) => p.id === active); this.statusEl.textContent = `📍 ${zone ? `${zone.emoji} ${zone.name}` : '—'} · ${power ? `${power.emoji} ${power.name}` : ''} · ` + `❤️ ${st.data.hp}/${st.data.maxHp} · 🪙 ${st.data.coins} · 💎 ${st.data.diamonds}`; } // ───────────── acciones ───────────── private toast(text: string, emoji?: string): void { this.game.ui.toast(text, emoji); } private burst(kind: string, count: number): void { const hero = this.game.hero; if (hero) this.game.vfx.burst(kind, hero.x, hero.y + 0.8, { count }); } /** Da un poder sin la tarjeta modal (para no encadenar modales) y lo activa. */ private grantPowerSilent(p: PowerDef): boolean { const g = this.game; if (g.state.hasPower(p.id)) return false; g.state.addPower(p.id); g.events.emit('hero:grant', { kind: 'power', id: p.id }); return true; } /** Da una herramienta sin la tarjeta modal y la coloca en una ranura libre. */ private grantToolSilent(t: ToolDef): boolean { const g = this.game; if (g.state.hasTool(t.id)) return false; g.state.addTool(t.id); autoAssignSlots(g.state); g.events.emit('hero:grant', { kind: 'tool', id: t.id }); return true; } private usePower(p: PowerDef): void { const hero = this.game.hero; if (!hero) { this.toast('El héroe aún no está listo', '⏳'); return; } // Segundo toque sobre el poder activo: desactivarlo (se quita y se vuelve a Base). if (p.id !== 'base' && this.game.state.data.power === p.id) { this.game.state.removePower(p.id); hero.setPower('base'); this.game.audio.sfx('ui_back'); this.toast(`Poder ${p.name} desactivado`, p.emoji); this.refresh(); return; } const fresh = p.id === 'base' ? false : this.grantPowerSilent(p); hero.setPower(p.id); if (fresh) { this.game.audio.sfx('powerup'); this.burst('confetti', 24); this.toast(`¡Poder ${p.name} conseguido!`, p.emoji); } else { this.game.audio.sfx('ui_ok'); } this.refresh(); } private grantAllPowers(): void { let n = 0; for (const p of POWERS) if (p.id !== 'base' && this.grantPowerSilent(p)) n++; if (n > 0) { this.game.audio.sfx('powerup'); this.burst('confetti', 24); this.toast(`¡${n} poderes conseguidos!`, '🌟'); } else { this.toast('Ya tienes todos los poderes', '🌟'); } this.refresh(); } private grantTool(t: ToolDef): void { if (this.grantToolSilent(t)) { this.game.audio.sfx('powerup'); this.burst('confetti', 24); this.toast(`¡${t.name} conseguida!`, t.emoji); } else { this.toast(`Ya tienes ${t.name}`, t.emoji); } this.refresh(); } private grantAllTools(): void { let n = 0; for (const t of TOOLS) if (this.grantToolSilent(t)) n++; if (n > 0) { this.game.audio.sfx('powerup'); this.burst('confetti', 24); this.toast(`¡${n} herramientas conseguidas!`, '🧰'); } else { this.toast('Ya tienes todas las herramientas', '🧰'); } this.refresh(); } private healMax(): void { const hero = this.game.hero; const st = this.game.state; if (hero) hero.heal(st.data.maxHp); else { st.data.hp = st.data.maxHp; st.changed(); } this.toast('Vida al máximo', '❤️'); this.refresh(); } private addCoins(): void { this.game.state.addCoins(100); this.game.audio.sfx('star'); this.toast('+100 monedas', '🪙'); this.refresh(); } private addDiamonds(): void { this.game.state.addDiamonds(10); this.game.audio.sfx('gem'); this.toast('+10 diamantes', '💎'); this.refresh(); } private saveGame(): void { this.game.state.save(); this.game.audio.sfx('checkpoint'); this.toast('Partida guardada', '💾'); } private async travel(zoneId: string, spawn: string): Promise { const g = this.game; if (g.ui.modalOpen) { this.toast('Cierra el menú actual primero', '⏳'); return; } const def = g.getZone(zoneId); if (!def) return; const label = SPAWN_LABELS[spawn] ?? spawn; if (g.zone?.id === zoneId && g.hero) { // Misma zona: teletransporte directo, sin recargar el nivel. const sp = def.spawns[spawn] ?? def.spawns.default; g.hero.teleport(sp.x, sp.y); g.gfx.cam.snap(sp.x, sp.y); } else { await g.loadZone(zoneId, spawn); } g.audio.sfx('teleport'); this.toast(`${def.name} — ${label}`, '📍'); this.refresh(); } private setFlag(f: FlagDef): void { const st = this.game.state; if (st.has(f.key)) { this.toast(`Ya está marcado: ${f.label}`, f.emoji); return; } st.set(f.key, true); if (f.key === 'horde.cleared') st.set('horde.wave', 3); this.game.audio.sfx('quest'); this.toast(`Marcado: ${f.label}`, f.emoji); this.refresh(); } /** * Marca todos los flags del tramo 1 en el orden de la historia, con las * recompensas que la historia entrega junto a ellos (burbujas, pistola de * agua y COOL por el anillo). Si Luna o la horda estaban en curso, se * retiran para dejar la ciudad en calma. */ private completeTramo1(): void { const g = this.game; const st = g.state; st.set('emma.met', true); st.set('hero.backpack', true); st.set('luna.defeated', true); st.set('horde.wave', 3); st.set('horde.cleared', true); st.set('eddy.met', true); st.set('lagoon.crossed', true); st.set('eddy.bubbles', true); st.set('eddy.watergun', true); st.set('lagoon.balloons', 3); st.set('eddy.reward', true); st.set('cave.ring', true); st.set('emma.ringReturned', true); this.grantToolSilent(TOOLS[0]); // burbujas this.grantToolSilent(TOOLS[1]); // pistola de agua this.grantPowerSilent(POWERS[4]); // COOL, regalo de Emma por el anillo for (const e of g.allByTag('boss')) e.dead = true; for (const e of g.allByTag('enemy')) e.dead = true; st.changed(); g.audio.sfx('victory'); this.burst('confetti', 40); this.toast('Tramo 1 completado: Ciudad y Laguna listas', '🏆'); this.refresh(); } /** * Modo dios: el héroe ignora los golpes (se envuelve su takeHit en caliente; * el héroe es persistente, así que el cambio sobrevive a la muerte y a los * cambios de zona). El daño por caída lo cubre el oyente de 'hero:hurt'. */ private setGod(on: boolean): void { const hero = this.game.hero; if (on) { if (!hero) { this.toast('El héroe aún no está listo', '⏳'); return; } if (!this.godMode) { this.origTakeHit = hero.takeHit; hero.takeHit = () => false; this.godMode = true; hero.heal(999); this.game.audio.sfx('powerup'); this.toast('Modo dios activado: invulnerable', '🛡️'); } } else { if (this.godMode && this.origTakeHit && hero) hero.takeHit = this.origTakeHit; this.godMode = false; this.origTakeHit = null; this.game.audio.sfx('ui_back'); this.toast('Modo dios desactivado', '🛡️'); } this.refresh(); } }