/** * `item.chest` `{ id, contains }` — cofre 3D que se abre con `interact`. * contains: 'coins:N' (alias 'stars:N') | 'diamond' | 'heart' | 'power:' | 'tool:'. * Flag `chest.` (queda abierto para siempre); POI 🎁 en el automapa mientras esté cerrado. */ import * as THREE from 'three'; import { RoundedBoxGeometry } from 'three/examples/jsm/geometries/RoundedBoxGeometry.js'; import { Entity } from '../../core/entity'; import type { PowerId, Props, ToolId } from '../../core/types'; import { clamp, ease } from '../../core/math'; import { tallyAdd } from '../../core/tally'; import { blobShadow, emojiSprite, toonMaterial } from '../../render'; import type { Interactable } from '../npcs/interact'; const W = 1.2; const H = 0.62; const D = 0.8; interface Res { body: THREE.BufferGeometry; lid: THREE.BufferGeometry; band: THREE.BufferGeometry; lock: THREE.BufferGeometry; glow: THREE.BufferGeometry; arc: THREE.BufferGeometry; wood: THREE.Material; woodDark: THREE.Material; gold: THREE.Material; } let res: Res | null = null; function shared }>(o: T): T { o.userData.shared = true; return o; } function resources(): Res { if (res) return res; const lid = new THREE.CylinderGeometry(D / 2, D / 2, W, 24, 1, false, 0, Math.PI); lid.rotateZ(Math.PI / 2); res = { body: shared(new RoundedBoxGeometry(W, H, D, 3, 0.08)), lid: shared(lid), band: shared(new RoundedBoxGeometry(0.14, H + 0.04, D + 0.06, 2, 0.03)), lock: shared(new RoundedBoxGeometry(0.22, 0.26, 0.1, 2, 0.04)), glow: shared(new THREE.PlaneGeometry(W - 0.16, D - 0.16)), arc: shared(new THREE.TorusGeometry(D / 2 + 0.015, 0.055, 8, 20, Math.PI).rotateY(Math.PI / 2)), wood: shared(toonMaterial({ color: 0xc27a3e, gloss: 0.35, rim: 0.4 })), woodDark: shared(toonMaterial({ color: 0xa05f2c, gloss: 0.35, rim: 0.4, side: THREE.DoubleSide })), gold: shared(toonMaterial({ color: 0xffc928, emissive: 0xffa000, emissiveIntensity: 0.18, gloss: 1, rim: 0.7 })), }; return res; } export class Chest extends Entity implements Interactable { readonly interactRange = { x: 1.5, y: 1.6 }; private readonly chestId: string; private readonly contains: string; private readonly flag: string; private readonly pivot = new THREE.Group(); private readonly root = new THREE.Group(); private readonly glow: THREE.Mesh; private readonly glowMat: THREE.MeshBasicMaterial; private readonly sparkle: THREE.Sprite; private opened = false; private openT = -1; private t = 0; constructor(props: Props, x: number, y: number) { super(); this.team = 'neutral'; this.tags.add('item'); this.chestId = String(props.id ?? `c${Math.round(x)}_${Math.round(y)}`); this.contains = String(props.contains ?? 'coins:5'); this.flag = `chest.${this.chestId}`; const r = resources(); const body = new THREE.Mesh(r.body, r.wood); body.position.y = H / 2; body.castShadow = true; this.root.add(body); for (const bx of [-W / 2 + 0.2, W / 2 - 0.2]) { const band = new THREE.Mesh(r.band, r.gold); band.position.set(bx, H / 2, 0); this.root.add(band); } const lock = new THREE.Mesh(r.lock, r.gold); lock.position.set(0, H - 0.06, D / 2 + 0.04); this.root.add(lock); this.glowMat = new THREE.MeshBasicMaterial({ color: 0xfff0a0, transparent: true, opacity: 0, blending: THREE.AdditiveBlending, depthWrite: false }); this.glow = new THREE.Mesh(r.glow, this.glowMat); this.glow.rotation.x = -Math.PI / 2; this.glow.position.y = H - 0.02; this.root.add(this.glow); // Tapa: bisagra en el borde trasero. this.pivot.position.set(0, H, -D / 2); const lid = new THREE.Mesh(r.lid, r.woodDark); lid.position.z = D / 2; lid.castShadow = true; this.pivot.add(lid); for (const bx of [-W / 2 + 0.2, W / 2 - 0.2]) { const arc = new THREE.Mesh(r.arc, r.gold); arc.position.set(bx, 0, D / 2); this.pivot.add(arc); } this.root.add(this.pivot); this.sparkle = emojiSprite('✨', 0.9, { glow: 0.6 }); this.sparkle.position.set(0, H + 0.6, 0.2); this.sparkle.visible = false; this.root.add(this.sparkle); const sh = blobShadow(0.8, 0.35); sh.position.y = 0.02; this.view.add(sh); this.view.add(this.root); } onAdded(): void { const g = this.game; // lo que guarda el cofre cuenta para el recuento de la zona const [kind, arg] = this.contains.split(':'); if (kind === 'coins' || kind === 'stars') tallyAdd(g, 'c', clamp(parseInt(arg ?? '5', 10) || 5, 1, 30)); else if (kind === 'diamond') tallyAdd(g, 'd', 1); if (g.state.has(this.flag)) { this.opened = true; this.pivot.rotation.x = -1.95; return; } if (g.zone) g.events.emit('map:poi', { zone: g.zone.id, x: this.x, y: this.y + 0.5, emoji: '🎁', id: this.flag }); } bounds() { return { x: this.x, y: this.y + H / 2, hw: W / 2, hh: H / 2 }; } interactText(): string | null { return this.opened ? null : 'Abrir'; } interactAnchor() { return { x: this.x, y: this.y + H + 1.2 }; } interact(): void { if (this.opened) return; const g = this.game; const st = g.state; this.opened = true; this.openT = 0; st.set(this.flag, true); if (g.zone) delete st.data.flags[`map.poi.${g.zone.id}.${this.flag}`]; st.save(); g.audio.sfx('chest', { x: this.x }); g.gfx.cam.shake(0.12, 0.15); g.after(0.35, () => this.release()); } private release(): void { const g = this.game; const x = this.x; const y = this.y + H + 0.1; g.vfx.burst('sparkle', x, y + 0.3, { count: 16, scale: 1.2, color: 0xffe066 }); g.vfx.burst('confetti', x, y + 0.4, { count: 18 }); g.vfx.ring(x, y + 0.3, 0xffe066, 1.2); const [kind, arg] = this.contains.split(':'); const pop = (type: string, i: number, n: number) => g.spawn(type, x, y, { pop: true, tally: true, vx: n > 1 ? (i / (n - 1) - 0.5) * 4.5 : 0.6 }); switch (kind) { case 'coins': case 'stars': { const n = clamp(parseInt(arg ?? '5', 10) || 5, 1, 30); for (let i = 0; i < n; i++) g.after(i * 0.05, () => pop('item.coin', i, n)); break; } case 'diamond': pop('item.diamond', 0, 1); break; case 'heart': pop('item.heart', 0, 1); break; case 'container': g.spawn('item.heartbox', x, y, { pop: true, vx: 0.6, id: `${this.chestId}.box` }); break; case 'power': case 'tool': { g.vfx.burst('emoji', x, y + 0.8, { emoji: kind === 'power' ? { fury: '😡', ice: '🥶', fire: '🔥', cool: '😎' }[arg] ?? '✨' : '🎁', count: 1 }); g.after(0.5, () => { const h = g.hero; if (!h) return; if (kind === 'power') h.grantPower(arg as PowerId); else h.grantTool(arg as ToolId); g.state.save(); }); break; } default: pop('item.coin', 0, 1); } g.events.emit('item:collected', { type: this.type, id: this.chestId, x, y }); } render(alpha: number, dt: number): void { super.render(alpha, dt); this.t += dt; if (this.openT >= 0) { this.openT += dt; const k = clamp(this.openT / 0.55, 0, 1); // Tapa con rebote; el cofre se aplasta y estira. this.pivot.rotation.x = -1.95 * ease.outBack(k); const sq = Math.sin(clamp(this.openT / 0.4, 0, 1) * Math.PI) * 0.18; this.root.scale.set(1 + sq * 0.6, 1 - sq, 1 + sq * 0.6); this.glowMat.opacity = this.openT < 0.3 ? this.openT / 0.3 : Math.max(0.35, 1 - (this.openT - 0.3) * 0.8); this.sparkle.visible = this.openT < 2.2; this.sparkle.scale.setScalar(0.9 + Math.sin(this.openT * 9) * 0.15); this.sparkle.position.y = H + 0.6 + this.openT * 0.3; if (this.openT > 3) this.openT = -1; } else if (!this.opened) { // Cerrado: respira un poco para llamar la atención. const b = Math.max(0, Math.sin(this.t * 2.2)) ** 8 * 0.06; this.root.scale.set(1 + b * 0.5, 1 - b, 1 + b * 0.5); this.pivot.rotation.x = -b * 1.2; } else { this.root.scale.set(1, 1, 1); this.glowMat.opacity = 0.35; } } }