/** * Piezas de puzzle compartidas por el bosque y el castillo. * * · puzzle.door — Reja/portón que se abre cuando TODAS las banderas de `requires` están puestas * (antorchas, braseros, cristales COOL, flores…). Muestra encima unas luces: una por * requisito, que se encienden al cumplirlo (el jugador ve cuánto le falta). * Props: { id, requires: 'flag1,flag2', w?=1.4, h?=4, style?: 'wood'|'castle', hint? } * · puzzle.deposit — Cofre del tesoro del castillo: pide devolver X monedas y Y diamantes. Al llenarlo * pone la bandera `castle.deposited.` (las puertas pueden exigirla). * Props: { id, coins, diamonds } */ import * as THREE from 'three'; import { Entity } from '../core/entity'; import type { Collider, Props } from '../core/types'; import type { Game } from '../core/game'; import type { Interactable } from '../entities/npcs/interact'; import { kitMat } from './kit/util'; import { emojiSprite } from '../render'; const shared = }>(o: T): T => { o.userData.shared = true; return o; }; const GEO = { box: shared(new THREE.BoxGeometry(1, 1, 1)), cyl: shared(new THREE.CylinderGeometry(1, 1, 1, 12)), sph: shared(new THREE.SphereGeometry(1, 16, 12)), cone: shared(new THREE.ConeGeometry(1, 1, 12)), }; const MAT = { iron: kitMat('pzIron', { color: 0x4a4e66, gloss: 0.7, rim: 0.6 }), ironDark: kitMat('pzIronDark', { color: 0x2c2f40, gloss: 0.5, rim: 0.4 }), wood: kitMat('pzWood', { color: 0x8a5a34, gloss: 0.3, rim: 0.35 }), woodDark: kitMat('pzWoodDark', { color: 0x5a3a20, gloss: 0.25, rim: 0.3 }), leaf: kitMat('pzLeaf', { color: 0x3f9b45, gloss: 0.4, rim: 0.4 }), stone: kitMat('pzStone', { color: 0x8a86a8, gloss: 0.3, rim: 0.45 }), lampOff: kitMat('pzLampOff', { color: 0x3a3550, gloss: 0.6, rim: 0.5 }), lampOn: kitMat('pzLampOn', { color: 0xffe066, emissive: 0xffc928, emissiveIntensity: 1.1, gloss: 0.8 }), gold: kitMat('pzGold', { color: 0xffc928, gloss: 0.85, rim: 0.7 }), chest: kitMat('pzChest', { color: 0x9a5a2e, gloss: 0.35, rim: 0.4 }), chestDark: kitMat('pzChestDark', { color: 0x5e3418, gloss: 0.3, rim: 0.3 }), velvet: kitMat('pzVelvet', { color: 0x6a2a8a, gloss: 0.2, rim: 0.3 }), }; function part(parent: THREE.Object3D, geo: THREE.BufferGeometry, mat: THREE.Material, x: number, y: number, z: number, sx: number, sy: number, sz: number, rz = 0): THREE.Mesh { const m = new THREE.Mesh(geo, mat); m.position.set(x, y, z); m.scale.set(sx, sy, sz); m.rotation.z = rz; m.castShadow = true; m.receiveShadow = true; parent.add(m); return m; } // ───────────────────────────── Puerta de puzzle ───────────────────────────── interface DoorProps extends Props { id: string; requires: string; w?: number; h?: number; style?: 'wood' | 'castle'; hint?: string } export class PuzzleDoor extends Entity { private readonly o: DoorProps; private readonly reqs: string[]; private col!: Collider; private readonly gate = new THREE.Group(); private readonly lamps: THREE.Mesh[] = []; private open = false; private openK = 0; private hintT = 0; private readonly w: number; private readonly h: number; constructor(props: DoorProps) { super(); this.o = props; this.reqs = String(props.requires ?? '').split(',').map((s) => s.trim()).filter(Boolean); this.w = props.w ?? 1.4; this.h = props.h ?? 4; this.team = 'neutral'; this.buildArt(); } private buildArt(): void { const { w, h } = this; const castle = this.o.style === 'castle'; // marco fijo (no sube) const frame = new THREE.Group(); part(frame, GEO.box, castle ? MAT.stone : MAT.woodDark, -w / 2 - 0.18, h / 2, 0, 0.36, h + 0.2, 1.4); part(frame, GEO.box, castle ? MAT.stone : MAT.woodDark, w / 2 + 0.18, h / 2, 0, 0.36, h + 0.2, 1.4); part(frame, GEO.box, castle ? MAT.stone : MAT.woodDark, 0, h + 0.2, 0, w + 0.8, 0.4, 1.5); this.view.add(frame); // reja / portón que sube if (castle) { const bars = Math.max(3, Math.round(w / 0.32)); for (let i = 0; i < bars; i++) { const x = -w / 2 + (i + 0.5) * (w / bars); part(this.gate, GEO.cyl, MAT.iron, x, h / 2, 0, 0.06, h, 0.06); part(this.gate, GEO.cone, MAT.iron, x, 0.05, 0, 0.08, 0.2, 0.08, Math.PI); } for (const y of [h * 0.25, h * 0.6, h * 0.9]) part(this.gate, GEO.box, MAT.ironDark, 0, y, 0, w, 0.12, 0.14); } else { const planks = Math.max(2, Math.round(w / 0.45)); for (let i = 0; i < planks; i++) { const x = -w / 2 + (i + 0.5) * (w / planks); part(this.gate, GEO.box, i % 2 ? MAT.wood : MAT.woodDark, x, h / 2, 0, w / planks - 0.04, h - 0.1, 0.3); } part(this.gate, GEO.box, MAT.woodDark, 0, h * 0.3, 0.18, w, 0.18, 0.1, 0.25); part(this.gate, GEO.box, MAT.woodDark, 0, h * 0.72, 0.18, w, 0.18, 0.1, -0.25); for (let i = 0; i < 3; i++) part(this.gate, GEO.sph, MAT.leaf, -w / 2 + i * (w / 2), h - 0.2, 0.25, 0.3, 0.22, 0.2); } this.view.add(this.gate); // luces de requisitos sobre el dintel const n = this.reqs.length; for (let i = 0; i < n; i++) { const m = part(this.view, GEO.sph, MAT.lampOff, (i - (n - 1) / 2) * 0.42, h + 0.62, 0.55, 0.14, 0.14, 0.14); this.lamps.push(m); } if (this.o.hint) { const s = emojiSprite(this.o.hint, 0.7); s.position.set(0, h + 1.2, 0.6); this.view.add(s); } } private done(f: string): boolean { return this.game.state.has(f); } onAdded(): void { this.col = this.game.physics.box(this.x, this.y + this.h / 2, this.w, this.h, { material: 'stone', tag: 'puzzle-door', owner: this }); this.open = this.game.state.has(`door.${this.o.id}`) || (this.reqs.length > 0 && this.reqs.every((f) => this.done(f))); if (this.open) { this.openK = 1; this.col.enabled = false; this.game.state.set(`door.${this.o.id}`, true); } this.applyLamps(); } onRemoved(): void { this.game.physics.remove(this.col); } private applyLamps(): void { for (let i = 0; i < this.lamps.length; i++) this.lamps[i].material = this.done(this.reqs[i]) ? MAT.lampOn : MAT.lampOff; } update(dt: number): void { const g = this.game; if (!this.open) { this.hintT -= dt; if (this.hintT <= 0) { this.hintT = 0.5; this.applyLamps(); if (this.reqs.length && this.reqs.every((f) => this.done(f))) { this.open = true; this.col.enabled = false; g.state.set(`door.${this.o.id}`, true); g.state.save(); g.audio.sfx('door', { x: this.x }); g.gfx.cam.shake(0.2, 0.4); g.vfx.burst('dust', this.x, this.y + 0.2, { count: 14, scale: 1.2 }); g.ui.toast('¡Se ha abierto un paso!', '🚪'); } } } else if (this.openK < 1) { this.openK = Math.min(1, this.openK + dt * 0.9); } } render(alpha: number, dt: number): void { super.render(alpha, dt); const k = this.openK * this.openK * (3 - 2 * this.openK); this.gate.position.y = k * (this.h - 0.3); this.gate.scale.y = 1 - k * 0.15; } } // ───────────────────────────── Cofre del tesoro (depósito) ───────────────────────────── interface DepositProps extends Props { id: string; coins: number; diamonds: number } export class DepositChest extends Entity implements Interactable { readonly interactRange = { x: 1.7, y: 1.8 }; private readonly o: DepositProps; private readonly lid = new THREE.Group(); private readonly label: THREE.Sprite[] = []; private filled = false; private openK = 0; constructor(props: DepositProps) { super(); this.o = props; this.team = 'neutral'; this.tags.add('item'); // cofre real: madera con herrajes dorados y terciopelo dentro part(this.view, GEO.box, MAT.chest, 0, 0.42, 0, 1.6, 0.84, 1.0); part(this.view, GEO.box, MAT.chestDark, 0, 0.1, 0, 1.66, 0.16, 1.06); for (const x of [-0.62, 0.62]) part(this.view, GEO.box, MAT.gold, x, 0.45, 0, 0.12, 0.9, 1.04); part(this.view, GEO.box, MAT.velvet, 0, 0.82, 0, 1.44, 0.04, 0.86); const lidBox = new THREE.Group(); part(lidBox, GEO.cyl, MAT.chest, 0, 0, 0, 0.5, 1.6, 0.5, Math.PI / 2); for (const x of [-0.62, 0.62]) part(lidBox, GEO.cyl, MAT.gold, x, 0, 0, 0.52, 0.12, 0.52, Math.PI / 2); part(lidBox, GEO.box, MAT.gold, 0, -0.05, 0.5, 0.26, 0.3, 0.08); lidBox.scale.set(1, 0.6, 1); lidBox.position.set(0, 0, 0.5); this.lid.add(lidBox); this.lid.position.set(0, 0.84, -0.5); this.view.add(this.lid); } onAdded(): void { this.filled = this.game.state.has(`castle.deposited.${this.o.id}`); if (this.filled) this.openK = 1; // cartel con lo que pide const c = emojiSprite('🪙', 0.5); const d = emojiSprite('💎', 0.5); c.position.set(-0.45, 2.05, 0.6); d.position.set(0.45, 2.05, 0.6); if (!this.filled) this.view.add(c, d); this.label.push(c, d); } bounds() { return { x: this.x, y: this.y + 0.6, hw: 0.8, hh: 0.6 }; } interactText(): string | null { return this.filled ? null : `Devolver ${this.o.coins} 🪙 y ${this.o.diamonds} 💎`; } interactAnchor(): { x: number; y: number } { return { x: this.x, y: this.y + 2.6 }; } interact(): void { const g = this.game; const s = g.state; if (this.filled) return; const coins = s.data.coins ?? 0; const diamonds = s.data.diamonds ?? 0; if (coins < this.o.coins || diamonds < this.o.diamonds) { const fc = Math.max(0, this.o.coins - coins); const fd = Math.max(0, this.o.diamonds - diamonds); void g.ui.dialog([{ speaker: 'Cofre del tesoro', emoji: '🎁', text: `Este cofre guarda parte del tesoro del castillo. Para cerrarlo hacen falta ${this.o.coins} monedas 🪙 y ${this.o.diamonds} diamantes 💎. Te faltan ${fc} 🪙 y ${fd} 💎. Abre el MAPA 🗺️: te dice cuántas quedan por recoger en cada zona. ¡Vuelve a buscarlas por los portales!`, }]); g.audio.sfx('error'); return; } s.data.coins = coins - this.o.coins; s.data.diamonds = diamonds - this.o.diamonds; s.set(`castle.deposited.${this.o.id}`, true); s.changed(); s.save(); this.filled = true; for (const l of this.label) l.removeFromParent(); g.audio.sfx('chest', { x: this.x }); g.audio.sfx('quest', { volume: 0.6 }); g.vfx.burst('confetti', this.x, this.y + 1.2, { count: 22 }); g.vfx.burst('sparkle', this.x, this.y + 1, { count: 16, color: 0xffd955 }); g.ui.toast(`Tesoro devuelto: ${this.o.coins} 🪙 y ${this.o.diamonds} 💎`, '🏰'); } render(alpha: number, dt: number): void { super.render(alpha, dt); if (this.filled && this.openK < 1) this.openK = Math.min(1, this.openK + dt * 2.5); this.lid.rotation.x = -this.openK * 1.25; if (!this.filled) { const t = this.game.time; for (let i = 0; i < this.label.length; i++) this.label[i].position.y = 2.05 + Math.sin(t * 2.4 + i) * 0.06; } } } export function installPuzzle(game: Game): void { game.register('puzzle.door', (_g, _x, _y, p) => new PuzzleDoor(p as DoorProps)); game.register('puzzle.deposit', (_g, _x, _y, p) => new DepositChest(p as DepositProps)); }