/** * `item.checkpoint` `{ spawn }` 🚩 — se activa al pasar: animación, sfx `checkpoint`, * `game.checkpoint(zona, spawn)` (guarda la partida) y cura completa. `spawn` debe existir en la zona. */ import * as THREE from 'three'; import { Entity } from '../../core/entity'; import type { Props } from '../../core/types'; import { emojiMesh, toonMaterial } from '../../render'; let baseGeo: THREE.CylinderGeometry | null = null; let baseMat: THREE.Material | null = null; export class Checkpoint extends Entity { private readonly spawnName: string; private readonly flagMesh: THREE.Mesh; private readonly flagMat: THREE.MeshStandardMaterial; private readonly holder = new THREE.Group(); private active = false; private animT = -1; private t = 0; constructor(props: Props) { super(); this.team = 'neutral'; this.tags.add('item'); this.spawnName = String(props.spawn ?? 'default'); if (!baseGeo) { baseGeo = new THREE.CylinderGeometry(0.38, 0.46, 0.18, 20); baseGeo.userData.shared = true; baseMat = toonMaterial({ color: 0xb8b0d8, gloss: 0.4 }); baseMat.userData.shared = true; } const base = new THREE.Mesh(baseGeo, baseMat!); base.position.y = 0.09; this.view.add(base); this.flagMesh = emojiMesh('🚩', 1.7); this.flagMat = this.flagMesh.material as THREE.MeshStandardMaterial; // El asta del emoji queda a la izquierda: se desplaza para plantarla en la base. this.flagMesh.position.set(0.3, 0.75, 0); this.holder.add(this.flagMesh); this.view.add(this.holder); } onAdded(): void { const d = this.game.state.data; this.active = !!this.game.zone && d.zone === this.game.zone.id && d.spawn === this.spawnName; this.applyLook(); } private applyLook(): void { this.flagMat.color.setHex(this.active ? 0xffffff : 0x7a7890); this.flagMat.emissive.setHex(this.active ? 0x401010 : 0x000000); } update(): void { const g = this.game; const h = g.hero; if (!h || !h.alive || !g.zone) return; // Otro checkpoint pudo tomar el relevo. const d = g.state.data; if (this.active && (d.zone !== g.zone.id || d.spawn !== this.spawnName)) { this.active = false; this.applyLook(); } if (this.active) return; if (Math.abs(h.x - this.x) < 0.8 && h.y > this.y - 0.5 && h.y < this.y + 2.6) this.activate(); } private activate(): void { const g = this.game; const h = g.hero!; this.active = true; this.animT = 0; this.applyLook(); g.state.data.hp = g.state.data.maxHp; g.state.changed(); g.checkpoint(g.zone!.id, this.spawnName); g.audio.sfx('checkpoint', { x: this.x }); g.vfx.burst('sparkle', this.x + 0.2, this.y + 1.3, { count: 18, scale: 1.2, color: 0xffe066 }); g.vfx.ring(this.x, this.y + 0.9, 0xffffff, 1.3); g.vfx.burst('heart', h.x, h.y + 0.6, { count: 4 }); g.vfx.floatText('¡Punto de control!', this.x, this.y + 2.3, '#ffe066'); g.ui.toast('Partida guardada', '💾'); } render(alpha: number, dt: number): void { super.render(alpha, dt); this.t += dt; const wave = Math.sin(this.t * (this.active ? 4 : 1.5)) * (this.active ? 0.06 : 0.02); this.flagMesh.rotation.z = wave; if (this.animT >= 0) { this.animT += dt; const k = this.animT; // Salto con giro completo y aterrizaje elástico. this.holder.rotation.y = Math.min(1, k / 0.6) * Math.PI * 2; const s = 1 + Math.sin(Math.min(1, k / 0.5) * Math.PI) * 0.35; this.holder.scale.set(2 - s, s, 1); this.holder.position.y = Math.sin(Math.min(1, k / 0.5) * Math.PI) * 0.5; if (k > 0.8) { this.animT = -1; this.holder.rotation.y = 0; this.holder.scale.set(1, 1, 1); this.holder.position.y = 0; } } } }