/** * `item.teleporter` `{ id, name? }` 🌀 — portal de viaje rápido. * Al tocarlo por primera vez se activa (queda capturado): flag `tp.` = "|" y POI en el automapa. * Funciona desde el principio (ya no espera al Rey Frío). Con `interact` (botón Hablar) abre `ui.choice` con los demás portales activados y viaja con `game.loadZone(zona, 'tp-')`. * Cada zona debe definir el spawn `tp-` junto a su portal (si falta, se usa la posición del portal * dentro de la misma zona o el spawn 'default'). */ import * as THREE from 'three'; import { Entity } from '../../core/entity'; import type { Props } from '../../core/types'; import { emojiMesh, emojiSprite, toonMaterial } from '../../render'; import type { Interactable } from '../npcs/interact'; let padGeo: THREE.CylinderGeometry | null = null; let rimGeo: THREE.TorusGeometry | null = null; let padMat: THREE.Material | null = null; interface TpEntry { id: string; zone: string; name: string } export function activeTeleporters(flags: Record): TpEntry[] { const out: TpEntry[] = []; for (const k in flags) { if (!k.startsWith('tp.') || !flags[k]) continue; const [zone, name] = String(flags[k]).split('|'); out.push({ id: k.slice(3), zone, name: name || zone }); } return out; } export class Teleporter extends Entity implements Interactable { readonly interactRange = { x: 1.3, y: 1.8 }; private readonly tpId: string; private readonly tpName: string | null; private readonly swirl: THREE.Mesh; private readonly swirlMat: THREE.MeshStandardMaterial; private readonly rimMat: THREE.MeshStandardMaterial; private readonly halo: THREE.Sprite; private active = false; private busy = false; private spin = 0; private pulse = 0; private t = 0; private unlockOff: (() => void) | null = null; constructor(props: Props) { super(); this.team = 'neutral'; this.tags.add('item'); this.tpId = String(props.id ?? 'portal'); this.tpName = props.name ? String(props.name) : null; if (!padGeo) { padGeo = new THREE.CylinderGeometry(0.85, 0.95, 0.16, 28); padGeo.userData.shared = true; rimGeo = new THREE.TorusGeometry(0.85, 0.07, 10, 36); rimGeo.userData.shared = true; padMat = toonMaterial({ color: 0x5b4be0, gloss: 0.6 }); padMat.userData.shared = true; } const pad = new THREE.Mesh(padGeo, padMat!); pad.position.y = 0.08; this.view.add(pad); this.rimMat = toonMaterial({ color: 0xa455f0, emissive: 0xa455f0, emissiveIntensity: 0.2 }); const rim = new THREE.Mesh(rimGeo!, this.rimMat); rim.rotation.x = Math.PI / 2; rim.position.y = 0.17; this.view.add(rim); this.swirl = emojiMesh('🌀', 1.7, { glow: true, glowIntensity: 0.4, shadow: false }); this.swirlMat = this.swirl.material as THREE.MeshStandardMaterial; this.swirl.position.y = 1.25; this.view.add(this.swirl); this.halo = emojiSprite('✨', 1.2, { glow: 0.5, opacity: 0.8 }); this.halo.position.set(0, 1.25, -0.1); this.view.add(this.halo); } onAdded(): void { this.active = this.game.state.has(`tp.${this.tpId}`); this.applyLook(); this.unlockOff = this.game.events.on('flag', (e) => { if (e.key === `tp.${this.tpId}`) { this.active = this.game.state.has(`tp.${this.tpId}`); this.applyLook(); } }); } onRemoved(): void { this.unlockOff?.(); this.unlockOff = null; } private applyLook(): void { this.swirlMat.color.setHex(this.active ? 0xffffff : 0x8a86a8); this.swirlMat.emissiveIntensity = this.active ? 0.6 : 0.05; this.rimMat.emissiveIntensity = this.active ? 0.9 : 0.1; this.halo.visible = this.active; } get label(): string { return this.tpName ?? this.game.zone?.name ?? this.tpId; } update(dt: number): void { this.t += dt; const h = this.game.hero; if (!h || !h.alive || !this.game.zone) return; if (this.active) return; if (Math.abs(h.x - this.x) < 0.9 && h.y > this.y - 0.3 && h.y < this.y + 2.5) this.activate(); } private activate(): void { const g = this.game; const z = g.zone!; this.active = true; this.pulse = 1; this.applyLook(); g.state.set(`tp.${this.tpId}`, `${z.id}|${this.label}`); g.events.emit('map:poi', { zone: z.id, x: this.x, y: this.y + 1, emoji: '🌀', id: this.tpId }); g.audio.sfx('teleport', { x: this.x }); g.vfx.burst('sparkle', this.x, this.y + 1.2, { count: 20, scale: 1.3, color: 0xc89bff }); g.vfx.ring(this.x, this.y + 1.2, 0xa455f0, 1.6); g.ui.toast('¡Portal capturado! Pulsa Hablar en cualquier portal 🌀 para viajar', '🌀'); g.state.save(); } interactText(): string | null { return this.active && !this.busy ? 'Viajar' : null; } interactAnchor() { return { x: this.x, y: this.y + 2.6 }; } interact(): void { void this.openMenu(); } private async openMenu(): Promise { const g = this.game; const list = activeTeleporters(g.state.data.flags).filter((e) => e.id !== this.tpId); const prompt = { speaker: 'Portal', emoji: '🌀', text: '' }; this.busy = true; try { if (!list.length) { await g.ui.dialog([{ ...prompt, text: 'Este es tu único portal activado. ¡Encuentra más 🌀 por el mundo para viajar entre ellos!' }]); return; } const opts = list.map((e) => `🌀 ${e.name}`); opts.push('Cancelar'); const i = await g.ui.choice({ ...prompt, text: '¿A dónde quieres viajar?' }, opts); const dest = list[i]; if (!dest) { g.audio.sfx('ui_back'); return; } await this.travel(dest); } finally { this.busy = false; } } async travel(dest: TpEntry): Promise { const g = this.game; const h = g.hero; if (!h) return; const spawn = `tp-${dest.id}`; h.inputLocked = true; g.audio.sfx('teleport'); g.vfx.burst('sparkle', h.x, h.y, { count: 24, scale: 1.4, color: 0xc89bff }); g.vfx.ring(h.x, h.y, 0xa455f0, 2); this.pulse = 1; try { if (dest.zone === g.zone?.id || !g.hasZone(dest.zone)) { const z = g.zone; let sp = z?.spawns[spawn]; if (!sp) { const other = g.entities.find((e) => e instanceof Teleporter && e.tpId === dest.id); sp = other ? { x: other.x + 1.4, y: other.y } : z?.spawns.default; } if (!sp) return; await g.ui.fade(true, 0.3); h.teleport(sp.x, sp.y); g.gfx.cam.snap(sp.x, sp.y); await g.ui.fade(false, 0.3); } else { await g.loadZone(dest.zone, spawn); } const nh = g.hero; if (nh) { g.vfx.burst('sparkle', nh.x, nh.y, { count: 20, scale: 1.3, color: 0xc89bff }); g.audio.sfx('teleport', { pitch: 1.3 }); } } finally { h.inputLocked = false; } } render(alpha: number, dt: number): void { super.render(alpha, dt); this.spin += dt * (this.active ? 4.5 : 0.8); this.pulse = Math.max(0, this.pulse - dt * 1.5); this.swirl.rotation.z = -this.spin; const s = 1 + Math.sin(this.t * 3) * 0.04 + this.pulse * 0.4; this.swirl.scale.set(1.7 * s, 1.7 * s, 1.7); this.swirl.position.y = 1.25 + Math.sin(this.t * 1.6) * 0.08; this.halo.material.rotation = this.spin * 0.3; this.halo.material.opacity = 0.5 + Math.sin(this.t * 4) * 0.2; } }