/** * Boca de la Cueva Cristal 🕳️ (orilla de la laguna): una ladera de roca con un arco natural que se abre * a la oscuridad, entibado de madera de mina con farol, raíles que entran y estalactitas. Sin puerta: * se entra caminando hacia la oscuridad (la salida de zona está dentro del arco). * * `x` = centro del arco, `y` = suelo. Solo visual (sin colisión): la montaña queda detrás del plano de * juego para que el héroe nunca quede tapado; el marco de madera, delante, enmarca la entrada. */ import * as THREE from 'three'; import { Entity } from '../core/entity'; import type { Game } from '../core/game'; import { kitMat } from './kit/util'; import { PooledLight } from '../render/lightPool'; const shared = }>(o: T): T => { o.userData.shared = true; return o; }; let res: { hill: THREE.BufferGeometry; tunnel: THREE.BufferGeometry; box: THREE.BufferGeometry; cone: THREE.BufferGeometry; sph: THREE.BufferGeometry; cyl: THREE.BufferGeometry; } | null = null; const AW = 1.75; // semiancho del arco const AH = 3.3; // alto del arco /** Ladera con la muesca del arco en la base (un solo contorno, sin agujeros). */ function hillShape(): THREE.Shape { const s = new THREE.Shape(); s.moveTo(-7.5, 0); s.lineTo(-AW, 0); // arco (media elipse) de izquierda a derecha por arriba for (let i = 0; i <= 20; i++) { const a = Math.PI - (i / 20) * Math.PI; s.lineTo(Math.cos(a) * AW, Math.sin(a) * AH); } // perfil rocoso irregular: repisas y dos picos (no una cúpula) const pts: Array<[number, number]> = [ [14, 0], [13.2, 1.7], [12.1, 3.3], [10.8, 4.2], [9.8, 6.0], [8.2, 6.6], [6.9, 8.4], [5.2, 9.1], [3.8, 8.3], [2.4, 8.7], [0.6, 7.5], [-1.2, 7.1], [-2.5, 5.7], [-4.0, 5.1], [-5.2, 3.4], [-6.5, 2.3], [-7.5, 0], ]; for (const [x, y] of pts) s.lineTo(x, y); return s; } /** Garganta del túnel: el mismo arco un poco más pequeño, hacia el fondo. */ function tunnelShape(): THREE.Shape { const s = new THREE.Shape(); s.moveTo(-AW - 0.6, 0); s.lineTo(-AW + 0.05, 0); for (let i = 0; i <= 16; i++) { const a = Math.PI - (i / 16) * Math.PI; s.lineTo(Math.cos(a) * (AW - 0.05), Math.sin(a) * (AH - 0.05)); } s.lineTo(AW + 0.6, 0); s.lineTo(AW + 0.6, AH + 0.8); s.lineTo(-AW - 0.6, AH + 0.8); s.closePath(); return s; } function resources() { if (!res) { const hill = new THREE.ExtrudeGeometry(hillShape(), { depth: 3.2, bevelEnabled: true, bevelThickness: 0.45, bevelSize: 0.45, bevelSegments: 3, curveSegments: 10, steps: 1, }); // relieve: aleja/acerca los vértices con un ruido barato para que no parezca un recorte plano const p = hill.attributes.position as THREE.BufferAttribute; for (let i = 0; i < p.count; i++) { const x = p.getX(i); const y = p.getY(i); const z = p.getZ(i); if (y < 0.05) continue; const n = Math.sin(x * 1.7 + y * 0.9) * 0.18 + Math.sin(x * 0.6 - y * 1.9) * 0.14; p.setZ(i, z + (z > 1.6 ? n : 0)); } hill.computeVertexNormals(); const tunnel = new THREE.ExtrudeGeometry(tunnelShape(), { depth: 2.6, bevelEnabled: false, curveSegments: 8, steps: 1 }); res = { hill: shared(hill), tunnel: shared(tunnel), box: shared(new THREE.BoxGeometry(1, 1, 1)), cone: shared(new THREE.ConeGeometry(1, 1, 8)), sph: shared(new THREE.SphereGeometry(1, 14, 10)), cyl: shared(new THREE.CylinderGeometry(1, 1, 1, 10)), }; } return res; } const MAT = { rock: kitMat('cmRock', { color: 0x7d7290, gloss: 0.2, rim: 0.45 }), rockDark: kitMat('cmRockDark', { color: 0x463e5a, gloss: 0.18, rim: 0.3 }), inside: kitMat('cmInside', { color: 0x15101f, gloss: 0.05, rim: 0.05 }), moss: kitMat('cmMoss', { color: 0x5bb84a, gloss: 0.35, rim: 0.45 }), wood: kitMat('cmWood', { color: 0x8a5a34, gloss: 0.3, rim: 0.35 }), woodDark: kitMat('cmWoodDark', { color: 0x5a3a20, gloss: 0.25, rim: 0.3 }), iron: kitMat('cmIron', { color: 0x5a5e70, gloss: 0.7, rim: 0.55 }), glow: kitMat('cmGlow', { color: 0xffe0a0, emissive: 0xffb84a, emissiveIntensity: 1.2, gloss: 0.6 }), crystal: kitMat('cmCrystal', { color: 0xb8a0ff, emissive: 0x7a4aff, emissiveIntensity: 0.55, gloss: 1, rim: 1 }), }; function mesh(parent: THREE.Object3D, geo: THREE.BufferGeometry, mat: THREE.Material, x: number, y: number, z: number, sx: number, sy: number, sz: number, rx = 0, ry = 0, 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.set(rx, ry, rz); m.castShadow = true; m.receiveShadow = true; parent.add(m); return m; } export class CaveMouth extends Entity { private lantern: THREE.Group | null = null; constructor() { super(); const r = resources(); const g = this.view; // ladera (detrás del plano de juego: cara frontal en z ≈ -0.35) const hill = new THREE.Mesh(r.hill, MAT.rock); hill.position.z = -4.0; hill.castShadow = true; hill.receiveShadow = true; g.add(hill); // garganta oscura que se adentra const tunnel = new THREE.Mesh(r.tunnel, MAT.inside); tunnel.position.z = -6.6; g.add(tunnel); mesh(g, r.box, MAT.inside, 0, AH / 2, -6.7, AW * 2 + 0.4, AH + 0.2, 0.2); // peñascos en la base y en la ladera for (const [x, y, s, z] of [[-6.2, 0.4, 1.3, -1.2], [-4.4, 0.3, 0.9, -0.6], [3.4, 0.4, 1.1, -0.7], [6.6, 0.5, 1.5, -1.1], [9.8, 0.6, 1.7, -1.4], [-3.3, 5.6, 1.1, -1.6], [4.6, 7.4, 1.3, -1.9]] as const) { mesh(g, r.sph, MAT.rockDark, x, y, z, s, s * 0.75, s * 0.8, 0, x * 0.3, 0); } // musgo en las repisas y los picos (pegado a la roca) for (const [x, y, s] of [[-4.0, 5.05, 0.9], [-1.0, 7.05, 1.0], [2.4, 8.62, 0.8], [5.2, 9.02, 1.0], [10.7, 4.15, 0.9], [8.3, 6.55, 0.7]] as const) { mesh(g, r.sph, MAT.moss, x, y, -1.4, s, s * 0.22, 1.3); } // vetas oscuras en la cara de la roca for (const [x, y, w, rot] of [[-3.6, 2.6, 2.2, 0.15], [4.8, 3.2, 3.0, -0.1], [7.8, 5.2, 2.0, 0.2], [1.8, 6.0, 2.4, -0.05]] as const) { mesh(g, r.box, MAT.rockDark, x, y, -0.32, w, 0.09, 0.06, 0, 0, rot); } // estalactitas en el borde del arco for (const [x, len] of [[-1.1, 0.6], [-0.4, 0.9], [0.35, 0.7], [1.05, 0.5]] as const) { const y = Math.sqrt(Math.max(0, 1 - (x / AW) ** 2)) * AH; mesh(g, r.cone, MAT.rockDark, x, y - len / 2, -0.6, 0.12, len, 0.12, Math.PI); } // cristales que asoman dentro (dan profundidad) for (const [x, y, s] of [[-1.2, 0.3, 0.32], [1.1, 0.4, 0.26], [0.6, 2.4, 0.2]] as const) { mesh(g, r.cone, MAT.crystal, x, y + s, -5.4, s * 0.5, s * 2.2, s * 0.5, 0, 0, x * 0.2); } // entibado de mina (delante, enmarca la boca sin taparla) const frame = new THREE.Group(); for (const side of [-1, 1]) { mesh(frame, r.box, MAT.wood, side * (AW - 0.05), 1.45, 0.75, 0.24, 2.9, 0.24); mesh(frame, r.box, MAT.woodDark, side * (AW - 0.4), 2.55, 0.75, 0.16, 0.9, 0.14, 0, 0, side * 0.75); } mesh(frame, r.box, MAT.wood, 0, 2.95, 0.75, AW * 2 + 0.5, 0.26, 0.3); mesh(frame, r.box, MAT.woodDark, 0, 3.12, 0.62, AW * 2 + 0.7, 0.08, 0.36); g.add(frame); // farol colgando del dintel const lantern = new THREE.Group(); mesh(lantern, r.cyl, MAT.iron, 0, 0.32, 0, 0.012, 0.6, 0.012); mesh(lantern, r.box, MAT.iron, 0, -0.04, 0, 0.28, 0.36, 0.28); mesh(lantern, r.box, MAT.glow, 0, -0.04, 0, 0.2, 0.28, 0.2); const light = new PooledLight(0xffc070, 1.4, 7, 1.8); light.position.set(0, -0.1, 0.4); lantern.add(light); lantern.position.set(0.85, 2.3, 0.85); g.add(lantern); this.lantern = lantern; // raíles que entran en la cueva for (const zz of [-0.32, 0.32]) mesh(g, r.box, MAT.iron, -1.5, 0.06, zz, 7, 0.07, 0.07); for (let i = 0; i < 11; i++) mesh(g, r.box, MAT.woodDark, -4.6 + i * 0.6, 0.03, 0, 0.16, 0.05, 1.0); } render(alpha: number, dt: number): void { super.render(alpha, dt); if (this.lantern) this.lantern.rotation.z = Math.sin(this.game.time * 1.3) * 0.05; } } export function installCaveMouth(game: Game): void { game.register('world.cavemouth', () => new CaveMouth()); }