import type { Action } from '../core/types'; import { isToolOn, slotTool } from '../core/slots'; import { TOOL_INFO } from './backpack'; import { emo, h, type Ctx } from './dom'; interface BtnDef { action: Action; emoji: string; s: number; r: number; b: number; cls?: string; when?: () => boolean; active?: boolean; tool?: string } /** Controles táctiles: stick a la izquierda, tres herramientas y acción de poder junto a los EmojiPowers. */ export class TouchControls { readonly el: HTMLElement; private stick: HTMLElement; private knob: HTMLElement; private btns: HTMLElement; private stickId: number | null = null; private origin = { x: 0, y: 0 }; private radius = 60; private layoutKey = ''; /** Acción → nº de dedos que la mantienen. */ private holds = new Map>(); private bagB: HTMLElement; constructor(private ctx: Ctx, private onMap: () => void, private onPause: () => void, private onBag: () => void) { const el = (this.el = h('div', 'eh-touch', null, ctx.root)); const zone = h('div', 'eh-stickzone', null, el); this.stick = h('div', 'eh-stick', null, el); this.knob = h('div', 'knob', null, this.stick); this.btns = h('div', 'eh-tbtns', null, el); const top = h('div', 'eh-ttop', null, el); const bagB = (this.bagB = h('button', 'eh-tb eh-hidden', null, el)); bagB.classList.add('eh-bag-left'); emo('🎒', bagB); bagB.addEventListener('pointerdown', (ev) => { ev.preventDefault(); ev.stopPropagation(); this.onBag(); }); const mapB = h('button', 'eh-tb', null, top); emo('🗺️', mapB); mapB.addEventListener('pointerdown', (ev) => { ev.preventDefault(); ev.stopPropagation(); this.onMap(); }); const pauseB = h('button', 'eh-tb', null, top); emo('⏸️', pauseB); pauseB.addEventListener('pointerdown', (ev) => { ev.preventDefault(); ev.stopPropagation(); this.onPause(); }); zone.addEventListener('pointerdown', (ev) => { if (this.stickId !== null) return; ev.preventDefault(); this.stickId = ev.pointerId; try { zone.setPointerCapture(ev.pointerId); } catch { /* sin captura */ } const r = this.stick.getBoundingClientRect(); this.radius = r.width * 0.42; // El stick NO se mueve de su sitio: el origen es siempre su centro, toque // donde toque el dedo dentro de la zona. (Antes saltaba hasta el dedo y // podía tapar la barra de EmojiPowers.) this.origin = { x: r.left + r.width / 2, y: r.top + r.height / 2 }; this.stick.classList.add('on'); this.moveStick(ev.clientX, ev.clientY); }); zone.addEventListener('pointermove', (ev) => { if (ev.pointerId === this.stickId) this.moveStick(ev.clientX, ev.clientY); }); const end = (ev: PointerEvent) => { if (ev.pointerId !== this.stickId) return; this.stickId = null; this.stick.classList.remove('on'); this.knob.style.transform = ''; ctx.game.input.setVirtualAxis(0, 0); }; zone.addEventListener('pointerup', end); zone.addEventListener('pointercancel', end); zone.addEventListener('lostpointercapture', end); } private moveStick(x: number, y: number): void { let dx = x - this.origin.x; let dy = y - this.origin.y; const d = Math.hypot(dx, dy); if (d > this.radius) { dx *= this.radius / d; dy *= this.radius / d; } this.knob.style.transform = `translate(${dx}px, ${dy}px)`; let ax = dx / this.radius; let ay = -dy / this.radius; if (Math.abs(ax) < 0.18) ax = 0; if (Math.abs(ay) < 0.18) ay = 0; this.ctx.game.input.setVirtualAxis(ax, ay); } private bindHold(b: HTMLElement, action: Action): void { b.addEventListener('pointerdown', (ev) => { ev.preventDefault(); ev.stopPropagation(); try { b.setPointerCapture(ev.pointerId); } catch { /* nada */ } b.classList.add('down'); this.hold(action, ev.pointerId, true); }); const up = (ev: PointerEvent) => { b.classList.remove('down'); this.hold(action, ev.pointerId, false); }; b.addEventListener('pointerup', up); b.addEventListener('pointercancel', up); b.addEventListener('lostpointercapture', up); } private hold(a: Action, id: number, down: boolean): void { let s = this.holds.get(a); if (!s) this.holds.set(a, (s = new Set())); const was = s.size > 0; if (down) s.add(id); else s.delete(id); const now = s.size > 0; if (was !== now) this.ctx.game.input.setVirtual(a, now); } /** Rehace los botones si cambió lo desbloqueado. */ refresh(): void { const st = this.ctx.game.state; const d = st.data; const special = d.powers.length > 1; const t1 = slotTool(st, 'tool'); const t2 = slotTool(st, 'tool2'); const t3 = slotTool(st, 'dig'); const bag = st.has('hero.backpack'); const key = [special, t1, t2, t3, bag, t1 ? (isToolOn(st, t1) ? 1 : 0) : '', t2 ? (isToolOn(st, t2) ? 1 : 0) : '', t3 ? (isToolOn(st, t3) ? 1 : 0) : ''].join('|'); if (key === this.layoutKey) return; this.layoutKey = key; this.bagB.classList.toggle('eh-hidden', !bag); const em = (x: typeof t1) => (x ? TOOL_INFO[x].emoji : ''); // Los tres botones reasignables comparten diámetro. La acción de poder vuelve al // grupo como un botón algo mayor y queda en el hueco libre sobre el Botón 1. const defs: BtnDef[] = [ { action: 'jump', emoji: '⬆️', s: 1.3, r: 0, b: 0.1, cls: 'jump' }, { action: 'attack', emoji: '👊', s: 1, r: 1.49, b: 0.15 }, { action: 'interact', emoji: '💬', s: 0.72, r: -0.16, b: 1.47 }, { action: 'special', emoji: '✨', s: 0.85, r: 0.67, b: 1.42, cls: 'special', when: () => special }, { action: 'tool', emoji: em(t1), s: 0.72, r: 2.71, b: 0.34, when: () => !!t1, active: !!t1 && isToolOn(st, t1), tool: t1 ?? undefined }, { action: 'tool2', emoji: em(t2), s: 0.72, r: 2.43, b: 1.12, when: () => !!t2, active: !!t2 && isToolOn(st, t2), tool: t2 ?? undefined }, { action: 'dig', emoji: em(t3), s: 0.72, r: 1.64, b: 1.33, when: () => !!t3, active: !!t3 && isToolOn(st, t3), tool: t3 ?? undefined }, ]; for (const [a, s] of this.holds) if (s.size) this.ctx.game.input.setVirtual(a, false); this.holds.clear(); this.btns.textContent = ''; for (const def of defs) { if (def.when && !def.when()) continue; const b = h('button', `eh-tb ${def.cls ?? ''}`.trim(), null, this.btns); b.dataset.action = def.action; if (def.tool) b.dataset.tool = def.tool; b.classList.toggle('on', !!def.active); b.style.setProperty('--s', String(def.s)); b.style.setProperty('--r', String(def.r)); b.style.setProperty('--b', String(def.b)); emo(def.emoji, b); this.bindHold(b, def.action); } } /** Suelta todo (al abrir un menú). */ releaseAll(): void { for (const [a, s] of this.holds) { if (s.size) this.ctx.game.input.setVirtual(a, false); s.clear(); } this.btns.querySelectorAll('.down').forEach((b) => b.classList.remove('down')); if (this.stickId !== null) { this.stickId = null; this.stick.classList.remove('on'); this.knob.style.transform = ''; this.ctx.game.input.setVirtualAxis(0, 0); } } }