import type { PowerId, ToolId, Action } from '../core/types'; import { emo, h, kick, type Ctx } from './dom'; import { keycap } from './labels'; import { SLOTS, USABLE_TOOLS, isToolOn, slotTool, toolOnFlag } from '../core/slots'; import { TOOL_INFO } from './backpack'; export const POWERS: Record = { base: { emoji: '🙂', name: 'Base', color: '#ffc928' }, fury: { emoji: '😡', name: 'Furia', color: '#f2452e' }, ice: { emoji: '🥶', name: 'Hielo', color: '#2fa8f5' }, fire: { emoji: '🔥', name: 'Fuego', color: '#ff7a1a' }, cool: { emoji: '😎', name: 'Cool', color: '#a455f0' }, }; export const POWER_ORDER: PowerId[] = ['base', 'fury', 'ice', 'fire', 'cool']; export const TOOLS: Record = { bubbles: { emoji: '🫧', name: 'Burbujas', action: 'tool' }, watergun: { emoji: '🔫', name: 'Pistola de agua', action: 'tool' }, flashlight: { emoji: '🔦', name: 'Linterna', action: 'tool' }, shovel: { emoji: '⛏️', name: 'Pala', action: 'dig' }, rain: { emoji: '🌧️', name: 'Lluvia', action: 'tool' }, }; export const TOOL_ORDER: ToolId[] = ['bubbles', 'watergun', 'flashlight', 'rain', 'shovel']; /** HUD: vida, contadores, poderes, herramientas, objetivo, jefe. */ export class Hud { readonly el: HTMLElement; private hearts: HTMLElement; private coins: HTMLElement; private gems: HTMLElement; private objective: HTMLElement; private powersEl: HTMLElement; private toolsEl: HTMLElement; private boss: HTMLElement | null = null; private bossFrac = 1; private lastHp = -1; private lastMax = -1; private lastCoins = -1; private lastGems = -1; private lastPowers = ''; private lastPower: PowerId | '' = ''; private lastTools = ''; private objText: string | null = null; constructor(private ctx: Ctx) { const el = (this.el = h('div', 'eh-hud', null, ctx.root)); const tl = h('div', 'eh-tl', null, el); this.hearts = h('div', 'eh-hearts', null, tl); const counters = h('div', 'eh-counters', null, tl); this.coins = this.counter(counters, '🪙'); this.gems = this.counter(counters, '💎'); this.objective = h('div', 'eh-pill eh-objective eh-hidden', null, el); const bottom = h('div', 'eh-bottom', null, el); this.powersEl = h('div', 'eh-powers', null, bottom); this.toolsEl = h('div', 'eh-tools', null, el); // Tocar/clic en la barra de poderes: cambia al siguiente (o al tocado). this.powersEl.addEventListener('pointerdown', (ev) => { ev.preventDefault(); const t = (ev.target as HTMLElement).closest('.eh-power'); const game = ctx.game; if (t?.dataset.power && game.hero && !game.frozen) { const p = t.dataset.power as PowerId; if (p !== game.hero.power) game.hero.setPower(p); else this.pulse('nextPower'); } else this.pulse('nextPower'); }); } private pulse(a: Action): void { const i = this.ctx.game.input; i.setVirtual(a, true); window.setTimeout(() => i.setVirtual(a, false), 60); } private counter(parent: HTMLElement, emoji: string): HTMLElement { const c = h('div', 'eh-counter', null, parent); emo(emoji, c); h('b', '', '0', c); return c; } /** Recalcula todo lo que haya cambiado (barato: compara con lo último pintado). */ refresh(): void { const d = this.ctx.game.state.data; if (d.hp !== this.lastHp || d.maxHp !== this.lastMax) this.renderHearts(d.hp, d.maxHp); if (d.coins !== this.lastCoins) this.setCount(this.coins, d.coins, this.lastCoins); if (d.diamonds !== this.lastGems) this.setCount(this.gems, d.diamonds, this.lastGems); this.lastCoins = d.coins; this.lastGems = d.diamonds; const pk = d.powers.join(','); const active = this.ctx.game.hero?.power ?? d.power; if (pk !== this.lastPowers || active !== this.lastPower) this.renderPowers(d.powers, active); if (this.toolsKey(d.tools) !== this.lastTools) this.renderTools(d.tools); } private setCount(c: HTMLElement, v: number, prev: number): void { c.querySelector('b')!.textContent = String(v); if (prev >= 0 && v > prev) kick(c, 'bump', 520); } private renderHearts(hp: number, max: number): void { const n = Math.ceil(max / 2); if (this.hearts.children.length !== n) { this.hearts.textContent = ''; for (let i = 0; i < n; i++) { const hEl = h('div', 'eh-heart', null, this.hearts); h('i', 'eh-emo bg', '❤️', hEl); h('i', 'eh-emo fg', '❤️', hEl); } } const prev = this.lastHp; [...this.hearts.children].forEach((c, i) => { const el = c as HTMLElement; const v = Math.max(0, Math.min(2, hp - i * 2)); const fill = v >= 2 ? 'full' : v === 1 ? 'half' : 'empty'; if (prev >= 0 && el.dataset.fill && el.dataset.fill !== fill) { const before = Math.max(0, Math.min(2, prev - i * 2)); kick(el, v < before ? 'lost' : 'gain', 650); } el.dataset.fill = fill; }); if (prev >= 0 && hp < prev) kick(this.hearts, 'hurt', 480); this.hearts.classList.toggle('low', hp > 0 && hp <= 2); this.lastHp = hp; this.lastMax = max; } private renderPowers(powers: PowerId[], active: PowerId): void { const prevSet = new Set(this.lastPowers ? this.lastPowers.split(',') : []); const first = this.lastPowers === ''; const list = POWER_ORDER.filter((p) => powers.includes(p)); this.powersEl.textContent = ''; this.powersEl.classList.toggle('eh-hidden', list.length < 2); const hint = h('span', 'eh-pw-hint', null, this.powersEl); keycap(this.ctx.game, 'prevPower', hint); list.forEach((p, idx) => { const info = POWERS[p]; const b = h('div', 'eh-power', null, this.powersEl); b.dataset.power = p; b.style.setProperty('--pc', info.color); emo(info.emoji, b); const k = h('span', 'eh-key', String(POWER_ORDER.indexOf(p) + 1), b); k.dataset.action = `power${POWER_ORDER.indexOf(p) + 1}`; void idx; h('span', 'eh-pill name', info.name, b); if (p === active) b.classList.add('active'); if (!first && !prevSet.has(p)) kick(b, 'new', 1000); else if (p === active && this.lastPower && this.lastPower !== active) kick(b, 'switch', 600); }); const hint2 = h('span', 'eh-pw-hint', null, this.powersEl); keycap(this.ctx.game, 'nextPower', hint2); this.lastPowers = powers.join(','); this.lastPower = active; } private renderTools(tools: ToolId[]): void { const st = this.ctx.game.state; const prev = new Set(this.lastTools ? this.lastTools.split(/[,|]/) : []); const first = this.lastTools === ''; this.toolsEl.textContent = ''; // Ya no se muestran las herramientas en el HUD (están en los botones y en la mochila). this.lastTools = this.toolsKey(tools); if (this.lastTools) return; // Botones asignables (mochila) con su tecla; las herramientas pasivas, sin tecla. for (const s of SLOTS) { const t = slotTool(st, s); if (!t) continue; const b = h('div', 'eh-tool', null, this.toolsEl); const on = isToolOn(st, t); b.title = on ? TOOL_INFO[t].name + ' · ACTIVA' : TOOL_INFO[t].name; b.dataset.tool = t; b.classList.toggle('on', on); emo(TOOL_INFO[t].emoji, b); keycap(this.ctx.game, s, b); if (!first && !prev.has(t)) kick(b, 'new', 1000); } for (const t of tools) { if (USABLE_TOOLS.includes(t)) continue; const b = h('div', 'eh-tool', null, this.toolsEl); b.title = TOOL_INFO[t].name; emo(TOOL_INFO[t].emoji, b); } this.lastTools = this.toolsKey(tools); } private toolsKey(tools: ToolId[]): string { const st = this.ctx.game.state; return tools.join(',') + '|' + SLOTS.map((s) => slotTool(st, s) ?? '').join(',') + '|' + (st.has(toolOnFlag('flashlight')) ? 'f1' : 'f0') + '|' + (st.has(toolOnFlag('rain')) ? 'r1' : 'r0'); } setObjective(text: string | null): void { if (text === this.objText) return; this.objText = text; const o = this.objective; if (!text) { o.classList.add('eh-hidden'); this.placeBoss(); return; } o.textContent = ''; emo('🎯', o); h('span', '', text, o); o.classList.remove('eh-hidden'); kick(o, 'show', 700); this.placeBoss(); } bossBar(info: { name: string; emoji: string; frac: number } | null): void { if (!info) { this.boss?.remove(); this.boss = null; return; } const f = Math.max(0, Math.min(1, info.frac)); if (!this.boss) { const b = (this.boss = h('div', 'eh-boss', null, this.el)); const face = h('div', 'face', null, b); emo(info.emoji, face); const col = h('div', 'col', null, b); h('span', 'eh-pill name', info.name, col); const bar = h('div', 'bar', null, col); h('div', 'lag', null, bar); h('div', 'fill', null, bar); this.bossFrac = f; } const b = this.boss; b.querySelector('.face .eh-emo')!.textContent = info.emoji; b.querySelector('.name')!.textContent = info.name; (b.querySelector('.fill') as HTMLElement).style.transform = `scaleX(${f})`; (b.querySelector('.lag') as HTMLElement).style.transform = `scaleX(${f})`; if (f < this.bossFrac - 1e-4) kick(b, 'hit', 380); this.bossFrac = f; this.placeBoss(); } /** La barra del jefe va justo DEBAJO de la barra de misión (o de vidas/contadores si no hay misión). */ private placeBoss(): void { const b = this.boss; if (!b) return; const base = this.el.getBoundingClientRect(); const tl = this.el.querySelector('.eh-tl') as HTMLElement | null; let bottom = tl ? tl.getBoundingClientRect().bottom : base.top; if (!this.objective.classList.contains('eh-hidden')) bottom = Math.max(bottom, this.objective.getBoundingClientRect().bottom); const em = parseFloat(getComputedStyle(b).fontSize) || 16; b.style.top = `${Math.round(bottom - base.top + em * 0.7)}px`; } }