/** * Mochila 🎒 — arriba, una MINIATURA de los botones de control con lo que lleva asignado cada uno * (poder activo en el botón especial, herramientas en los botones 1·2·3). Debajo, poderes y herramientas * en cuadrícula sin celdas, como el teclado de emojis del móvil. * · Los poderes son informativos: a color los obtenidos, en gris los que faltan (se cambian en la barra EmojiPowers). * · Tocar una herramienta → se marcan los botones 1·2·3 de la miniatura: toca uno para ponérsela * (si ya estaba en ese botón, se quita). Volver a tocar la herramienta cancela. * Se abre con 🎒 en pantalla, la tecla B o L3 en el mando. */ import { SLOT_LABEL, SLOTS, USABLE_TOOLS, assignSlot, isToolOn, slotTool, toolSlot, type SlotAction } from '../core/slots'; import type { PowerId, ToolId } from '../core/types'; import { emo, h, type Ctx } from './dom'; import type { Nav } from './dom'; export const TOOL_INFO: Record = { bubbles: { emoji: '🫧', name: 'Burbujas', desc: 'Burbujas para cruzar abismos y subir a lo alto. La lluvia, el fuego y la lava las revientan.' }, watergun: { emoji: '🔫', name: 'Agua', desc: 'Chorros de agua: riega, apaga llamas y empuja. Mantén pulsado para un chorro continuo.' }, shovel: { emoji: '🪏', name: 'Pala', desc: 'Cava la tierra blanda (hacia delante o hacia abajo) para abrir túneles y encontrar tesoros.' }, rain: { emoji: '🌧️', name: 'Lluvia', desc: 'Interruptor: endurece la lava, apaga el fuego… y con HIELO crea bloques para trepar.' }, flashlight: { emoji: '🔦', name: 'Linterna', desc: 'Interruptor: ilumina las zonas oscuras de la cueva.' }, }; const ORDER: ToolId[] = ['bubbles', 'watergun', 'shovel', 'rain', 'flashlight']; const POWER_INFO: Record = { base: { emoji: '🙂', name: 'Normal', desc: 'Equilibrado y valiente.' }, fury: { emoji: '😡', name: 'Furia', desc: 'Embestida que rompe rocas agrietadas y voltea escarabajos.' }, ice: { emoji: '🥶', name: 'Hielo', desc: 'Aliento helado que congela. Con lluvia, crea bloques de hielo.' }, fire: { emoji: '🔥', name: 'Fuego', desc: 'Bola de fuego rebotona: quema espinos y enciende antorchas. La lluvia la apaga.' }, cool: { emoji: '😎', name: 'Cool', desc: 'Onda que detiene enemigos y rompe escudos mágicos.' }, }; const POWER_ORDER: PowerId[] = ['base', 'fury', 'ice', 'fire', 'cool']; /** Posiciones de los botones de la pantalla (mismas proporciones que los controles táctiles). */ const PAD: Array<{ key: string; s: number; r: number; b: number; cls?: string }> = [ { key: 'jump', s: 1.3, r: 0, b: 0.1, cls: 'jump' }, { key: 'attack', s: 1, r: 1.49, b: 0.15 }, { key: 'interact', s: 0.72, r: -0.16, b: 1.47 }, { key: 'special', s: 0.85, r: 0.67, b: 1.42, cls: 'special' }, { key: 'tool', s: 0.72, r: 2.71, b: 0.34 }, { key: 'tool2', s: 0.72, r: 2.43, b: 1.12 }, { key: 'dig', s: 0.72, r: 1.64, b: 1.33 }, ]; interface Cell { el: HTMLElement; activate: () => void; info: { name: string; desc: string } } export class BackpackMenu { readonly el: HTMLElement; private box!: HTMLElement; private cells: Cell[] = []; private sel = 0; /** Herramienta elegida para asignar (modo «toca un botón»). */ private picking: ToolId | null = null; private info!: HTMLElement; private cols = 5; constructor(private ctx: Ctx, private close: () => void) { this.el = h('div', 'eh-pause eh-bag', null, ctx.root); this.el.addEventListener('pointerdown', (ev) => { if (ev.target === this.el) this.close(); }); this.main(); } /** Dibuja la mochila entera. */ main(sel = this.sel): void { const g = this.ctx.game; const st = g.state; const d = st.data; this.box?.remove(); const b = (this.box = h('div', 'eh-panel eh-pbox eh-bagbox', null, this.el)); h('div', 'eh-title-text eh-ptitle', 'MOCHILA', b); const x = h('button', 'eh-mclose', '✕', b); x.addEventListener('click', (ev) => { ev.stopPropagation(); this.close(); }); this.cells = []; const counts = h('div', 'eh-bagcounts', null, b); for (const [e, n] of [['🪙', d.coins], ['💎', d.diamonds], ['❤️', d.maxHp / 2]] as const) { const c = h('div', 'eh-counter', null, counts); emo(e, c); h('b', '', String(n), c); } // ── miniatura de los controles ── const pad = h('div', `eh-minipad${this.picking ? ' picking' : ''}`, null, b); const area = h('div', 'eh-minipad-in', null, pad); const hero = g.hero as { power?: PowerId } | null; const power = hero?.power ?? 'base'; for (const p of PAD) { const slot = SLOTS.includes(p.key as SlotAction) ? (p.key as SlotAction) : null; const tool = slot ? slotTool(st, slot) : null; if (p.key === 'special' && d.powers.length <= 1) continue; const btn = h('button', `eh-mini ${p.cls ?? ''}${slot ? ' slot' : ''}${slot && !tool ? ' free' : ''}`, null, area); btn.style.setProperty('--s', String(p.s)); btn.style.setProperty('--r', String(p.r)); btn.style.setProperty('--b', String(p.b)); const emoji = p.key === 'jump' ? '⬆️' : p.key === 'attack' ? '👊' : p.key === 'interact' ? '💬' : p.key === 'special' ? POWER_INFO[power].emoji : tool ? TOOL_INFO[tool].emoji : '+'; emo(emoji, btn); if (slot) { h('i', 'num', String(SLOTS.indexOf(slot) + 1), btn); if (tool && isToolOn(st, tool)) btn.classList.add('on'); if (this.picking) { btn.addEventListener('click', (ev) => { ev.stopPropagation(); this.assign(slot); }); this.cells.push({ el: btn, activate: () => this.assign(slot), info: { name: SLOT_LABEL[slot], desc: tool ? `Ahora: ${TOOL_INFO[tool].name}` : 'Libre' }, }); } } else btn.disabled = true; } const capt = h('div', 'eh-minicap', null, pad); if (this.picking) { const t = TOOL_INFO[this.picking]; emo(t.emoji, capt); h('span', '', ' Toca el botón 1, 2 o 3 para ponérsela', capt); } else { h('span', '', 'Tus botones', capt); } // ── cuadrícula de poderes y herramientas (sin celdas, como un teclado de emojis) ── const powers = POWER_ORDER.filter((p) => d.powers.includes(p)); const tools = ORDER.filter((t) => d.tools.includes(t)); const grid = (title: string) => { h('div', 'eh-emokb-title', title, b); return h('div', 'eh-emokb', null, b); }; if (!this.picking) { const tg = grid('HERRAMIENTAS'); if (!tools.length) h('div', 'eh-baghint', 'Aún no llevas herramientas. ¡Explora y habla con todos!', tg); for (const t of tools) { const inf = TOOL_INFO[t]; const slot = toolSlot(st, t); const k = h('button', 'eh-emokey', null, tg); emo(inf.emoji, k); h('small', '', inf.name, k); if (slot) h('i', 'num', String(SLOTS.indexOf(slot) + 1), k); const usable = USABLE_TOOLS.includes(t); const act = () => { if (!usable) return; this.picking = t; this.main(0); }; k.addEventListener('click', (ev) => { ev.stopPropagation(); this.ctx.sfx('ui_ok'); act(); }); this.cells.push({ el: k, activate: act, info: inf }); } // Poderes: solo informativos (se cambian desde la barra de EmojiPowers). A color los obtenidos, // en gris los que faltan. const pg = grid('EMOJIPOWERS'); for (const p of POWER_ORDER) { if (p === 'base') continue; const inf = POWER_INFO[p]; const has = powers.includes(p); const k = h('button', `eh-emokey info${has ? '' : ' locked'}${has && p === power ? ' cur' : ''}`, null, pg); emo(inf.emoji, k); h('small', '', inf.name, k); k.addEventListener('click', (ev) => { ev.stopPropagation(); this.select(this.cells.findIndex((c) => c.el === k)); }); this.cells.push({ el: k, activate: () => {}, info: has ? inf : { name: `${inf.name} (sin obtener)`, desc: 'Aún no tienes este poder. ¡Sigue la aventura!' }, }); } } else { const back = h('button', 'eh-menu-btn eh-bagcancel', null, b); emo('↩️', back); h('span', '', 'Cancelar', back); const cancel = () => { this.picking = null; this.main(0); }; back.addEventListener('click', (ev) => { ev.stopPropagation(); this.ctx.sfx('ui_back'); cancel(); }); this.cells.push({ el: back, activate: cancel, info: { name: 'Cancelar', desc: 'Volver sin cambiar los botones.' } }); } this.info = h('div', 'eh-baginfo', null, b); this.cols = Math.max(1, Math.min(5, this.picking ? this.cells.length : 5)); this.select(Math.max(0, Math.min(sel, this.cells.length - 1)), false); } private assign(slot: SlotAction): void { const st = this.ctx.game.state; const t = this.picking; if (!t) return; if (slotTool(st, slot) === t) assignSlot(st, slot, null); // tocar su mismo botón la quita else assignSlot(st, slot, t); st.save(); this.ctx.sfx('ui_ok'); this.picking = null; this.main(0); } private select(i: number, sound = true): void { const n = this.cells.length; if (!n) { this.info.textContent = ''; return; } i = ((i % n) + n) % n; if (sound && i !== this.sel) this.ctx.sfx('ui_move'); this.sel = i; this.cells.forEach((c, k) => c.el.classList.toggle('sel', k === i)); const c = this.cells[i]; this.info.textContent = ''; h('b', '', c.info.name, this.info); h('span', '', c.info.desc, this.info); } handle(nav: Nav): void { if (nav.left) this.select(this.sel - 1); if (nav.right) this.select(this.sel + 1); if (nav.up) this.select(this.sel - this.cols); if (nav.down) this.select(this.sel + this.cols); const c = this.cells[this.sel]; if (nav.ok && c) { this.ctx.sfx('ui_ok'); c.activate(); } else if (nav.back) { this.ctx.sfx('ui_back'); if (this.picking) { this.picking = null; this.main(0); } else this.close(); } } }