/** * Piezas compartidas por las zonas del tramo 1 (ciudad y laguna): * - Barrier: barrera temporal (colisionador que se activa/desactiva con animaciΓ³n y 🚧). * - SinkPad: nenΓΊfar/tronco que se hunde un poco al pisarlo (o del todo si es frΓ‘gil). * - awning / roofLedge / shop: toldos y tejados que sirven de plataforma. * - spawnWaveLocal: oleada simple (respaldo si enemies no exporta spawnWave). * - invisibleWall. */ import * as THREE from 'three'; import { Entity } from '../../core/entity'; import type { Game } from '../../core/game'; import type { Collider, Props, ZoneContext } from '../../core/types'; import { emojiMesh, emojiSprite } from '../../render/emoji'; import { toonMaterial } from '../../render/materials'; // ───────────────────────────── Utilidades ───────────────────────────── export function invisibleWall(game: Game, x: number, y0: number, h: number, w = 1): Collider { return game.physics.box(x, y0 + h / 2, w, h, { material: 'stone', tag: 'wall' }); } const box = new THREE.BoxGeometry(1, 1, 1); box.userData.shared = true; const sph = new THREE.SphereGeometry(1, 12, 8); sph.userData.shared = true; const cyl = new THREE.CylinderGeometry(1, 1, 1, 18); cyl.userData.shared = true; function mesh(geo: THREE.BufferGeometry, mat: THREE.Material, sx: number, sy: number, sz: number, x = 0, y = 0, z = 0): THREE.Mesh { const m = new THREE.Mesh(geo, mat); m.scale.set(sx, sy, sz); m.position.set(x, y, z); m.castShadow = true; m.receiveShadow = true; return m; } // ───────────────────────────── Barrera ───────────────────────────── export interface BarrierOpts { /** Altura del colisionador (por defecto 14: no se puede saltar). */ height?: number; color?: number; /** Emoji del letrero flotante sobre la barrera (por defecto 🚧). */ emoji?: string; closed?: boolean; } /** * Barrera temporal: vallas 🚧 apiladas + cortina de energΓ­a translΓΊcida. `setClosed(bool)` la levanta o * la retira con animaciΓ³n (y activa/desactiva su colisionador con `enabled`). */ export class Barrier extends Entity { collider!: Collider; closed: boolean; private k: number; private h: number; private curtain!: THREE.Mesh; private boards: THREE.Object3D[] = []; private icon!: THREE.Sprite; private color: number; constructor(private bx: number, private floor: number, private opts: BarrierOpts = {}) { super(); this.h = opts.height ?? 14; this.closed = opts.closed ?? false; this.k = this.closed ? 1 : 0; this.color = opts.color ?? 0xffc928; } onAdded(): void { this.setPos(this.bx, this.floor); this.collider = this.game.physics.box(this.bx, this.floor + this.h / 2, 0.9, this.h, { material: 'metal', tag: 'barrier', owner: this }); this.collider.enabled = this.closed; const curtainMat = toonMaterial({ color: this.color, emissive: this.color, emissiveIntensity: 0.55, transparent: true, opacity: 0.32, depthWrite: false, rim: 0.9 }); this.curtain = mesh(box, curtainMat, 0.9, this.h, 2.6, 0, this.h / 2, 0); this.curtain.castShadow = false; this.view.add(this.curtain); const postMat = toonMaterial({ color: 0x3b3f5c, gloss: 0.5 }); for (const z of [-1.2, 1.2]) { const p = mesh(cyl, postMat, 0.12, 1, 0.12, 0, 0.5, z); this.boards.push(p); this.view.add(p); } for (let i = 0; i < 3; i++) { const e = emojiMesh(this.opts.emoji ?? '🚧', 1.25); e.position.set(0, 0.6 + i * 1.15, 0.75); this.boards.push(e); this.view.add(e); } this.icon = emojiSprite(this.opts.emoji ?? '🚧', 0.9, { glow: 0.2 }); this.icon.position.set(0, 4.4, 0.8); this.view.add(this.icon); this.apply(); } setClosed(closed: boolean, instant = false): void { if (closed === this.closed && !instant) return; const changed = closed !== this.closed; this.closed = closed; this.collider.enabled = closed; if (instant) this.k = closed ? 1 : 0; else if (changed) { const g = this.game; g.vfx.burst(closed ? 'spark' : 'poof', this.bx, this.floor + 1.5, { count: 14, color: this.color }); g.audio.sfx(closed ? 'door' : 'whoosh', { x: this.bx }); g.gfx.cam.shake(0.15, 0.25); } this.apply(); } update(dt: number): void { const t = this.closed ? 1 : 0; if (this.k !== t) { this.k += Math.sign(t - this.k) * Math.min(Math.abs(t - this.k), dt * 2.5); this.apply(); } } private apply(): void { const k = this.k; const e = 1 - Math.pow(1 - k, 3); this.view.visible = k > 0.001; this.curtain.scale.y = this.h * e; this.curtain.position.y = (this.h * e) / 2; for (const p of this.boards.slice(0, 2)) { p.scale.y = this.h * e; p.position.y = (this.h * e) / 2; } for (const b of this.boards.slice(2)) b.scale.setScalar(1.25 * e); this.icon.visible = k > 0.95; } render(alpha: number, dt: number): void { super.render(alpha, dt); const t = this.game.time; const m = this.curtain.material as THREE.MeshStandardMaterial; m.opacity = 0.22 + Math.sin(t * 3 + this.bx) * 0.08; this.icon.position.y = 4.4 + Math.abs(Math.sin(t * 2.5)) * 0.25; } } // ───────────────────────────── NenΓΊfar que se hunde ───────────────────────────── export interface PadOpts { /** Ancho del colisionador (por defecto 1.8). */ w?: number; /** Se hunde del todo si se queda encima mΓ‘s de `fragileTime` s. */ fragile?: boolean; fragileTime?: number; flower?: boolean; /** Etiqueta del colisionador (para los guiones). */ tag?: string; } const padGeo = new THREE.CylinderGeometry(1, 1, 1, 26, 1, false, 0.35, Math.PI * 2 - 0.7); padGeo.userData.shared = true; /** NenΓΊfar flotante: superficie en `top`; baja ~0.35 al pisarlo. Los frΓ‘giles se hunden al rato. */ export class SinkPad extends Entity { c!: Collider; private off = 0; private standT = 0; private wob = 0; private visual = new THREE.Group(); private w: number; constructor(private px0: number, private top: number, private o: PadOpts = {}) { super(); this.w = o.w ?? 1.8; this.tags.add('platform'); } onAdded(): void { this.setPos(this.px0, this.top); this.c = this.game.physics.box(this.px0, this.top - 0.15, this.w, 0.3, { oneWay: true, material: 'grass', owner: this, tag: this.o.tag ?? 'pad', vx: 0, vy: 0, }); const color = this.o.fragile ? 0xc7d94a : 0x48c24a; const mat = toonMaterial({ color, gloss: 0.45, rim: 0.4 }); const r = this.w / 2 + 0.2; const leaf = mesh(padGeo, mat, r, 0.22, r * 0.8, 0, -0.11, 0.15); leaf.rotation.y = Math.random() * 0.6 - 0.3; this.visual.add(leaf); const vein = toonMaterial({ color: this.o.fragile ? 0xa8b836 : 0x3aa83c, gloss: 0.3 }); for (let i = 0; i < 5; i++) { const a = (i / 5) * Math.PI * 2 + 0.6; const v = mesh(box, vein, r * 0.8, 0.02, 0.05, Math.cos(a) * r * 0.4, 0.01, Math.sin(a) * r * 0.3); v.rotation.y = -a; this.visual.add(v); } if (this.o.flower) { const pet = toonMaterial({ color: 0xff9ad0, gloss: 0.5 }); for (let i = 0; i < 6; i++) { const a = (i / 6) * Math.PI * 2; const p = mesh(sph, pet, 0.13, 0.07, 0.06, 0.35 + Math.cos(a) * 0.14, 0.12, -0.2 + Math.sin(a) * 0.14); p.rotation.y = -a; p.rotation.z = 0.6; this.visual.add(p); } this.visual.add(mesh(sph, toonMaterial({ color: 0xffc928, gloss: 0.6 }), 0.08, 0.07, 0.08, 0.35, 0.15, -0.2)); } if (this.o.fragile) { // grietas: marquitas oscuras para avisar de que es frΓ‘gil const crack = toonMaterial({ color: 0x8a7a2a, gloss: 0.1 }); for (let i = 0; i < 3; i++) { const c = mesh(box, crack, 0.35, 0.02, 0.04, -0.3 + i * 0.3, 0.02, 0.15 - i * 0.12); c.rotation.y = 0.5 + i; this.visual.add(c); } } this.view.add(this.visual); } update(dt: number): void { const h = this.game.hero; const on = !!h && h.body.onGround && h.body.ground === this.c; const fragile = !!this.o.fragile; if (on) this.standT += dt; else this.standT = Math.max(0, this.standT - dt * 2); const ft = this.o.fragileTime ?? 0.8; let target = on ? 0.35 : 0; if (fragile && this.standT > ft) target = 1.6; if (on && this.standT < dt * 1.5) { this.wob = 1; this.game.audio.sfx('splash', { volume: 0.25, pitch: 1.6, x: this.px0 }); } const rate = target > 0.5 ? 1.8 : on ? 1.6 : 0.8; const prev = this.off; this.off += Math.sign(target - this.off) * Math.min(Math.abs(target - this.off), rate * dt); // ligera flotaciΓ³n cuando nadie lo pisa const bob = on ? 0 : Math.sin(this.game.time * 1.6 + this.px0) * 0.03; const y = this.top - this.off + bob - 0.15; this.c.vy = (y - this.c.y) / dt; this.c.y = y; if (prev < 1 && this.off >= 1) this.game.vfx.burst('bubble', this.px0, this.top, { count: 8 }); this.wob = Math.max(0, this.wob - dt * 3); this.y = this.c.y + 0.15; } render(alpha: number, dt: number): void { super.render(alpha, dt); const shake = this.o.fragile && this.standT > 0.25 ? Math.sin(this.game.time * 50) * 0.04 : 0; this.visual.position.x = shake; this.visual.rotation.z = Math.sin(this.game.time * 12) * 0.06 * this.wob; } } // ───────────────────────────── Toldos y tejados ───────────────────────────── /** Toldo a rayas (plataforma oneWay) con superficie en y. */ export function awning(z: ZoneContext, x: number, y: number, w: number, colorA: number, colorB = 0xf4f1ff, depth = 1.6): Collider { const g = new THREE.Group(); const mA = toonMaterial({ color: colorA, gloss: 0.4 }); const mB = toonMaterial({ color: colorB, gloss: 0.4 }); const n = Math.max(4, Math.round(w / 0.45)); const sw = w / n; for (let i = 0; i < n; i++) { const s = mesh(box, i % 2 ? mB : mA, sw + 0.01, 0.16, depth, -w / 2 + sw * (i + 0.5), -0.08, 0); s.rotation.x = 0.18; g.add(s); // festΓ³n const f = mesh(sph, i % 2 ? mB : mA, sw * 0.5, 0.2, 0.08, -w / 2 + sw * (i + 0.5), -0.22, depth / 2 * Math.cos(0.18) + 0.02); g.add(f); } const iron = toonMaterial({ color: 0x3b3f5c, gloss: 0.5 }); for (const sx of [-w / 2 + 0.1, w / 2 - 0.1]) g.add(mesh(box, iron, 0.06, 0.06, depth, sx, -0.12, -0.1)); g.position.set(x, y, -0.3); z.group.add(g); return z.game.physics.box(x, y - 0.12, w, 0.24, { oneWay: true, material: 'cloud', tag: 'awning' }); } /** Repisa invisible sobre un tejado (la cubierta la dibuja el prop). */ export function roofLedge(game: Game, x: number, top: number, w: number): Collider { return game.physics.box(x, top - 0.15, w, 0.3, { oneWay: true, material: 'stone', tag: 'roof' }); } /** Altura de la cubierta del prop 'building' segΓΊn plantas (ver kit/props.ts). */ export function buildingTop(floors: number): number { return floors * 1.3 + 0.3 + 0.225; } /** * Edificio jugable: prop 'building' casi en el plano de juego (z=-1.25: la fachada queda justo detrΓ‘s * del hΓ©roe) con cubierta pisable. Devuelve la y de la cubierta. */ export function playableBuilding(z: ZoneContext, x: number, floors: number, w: number, color: number, extra: Props = {}): number { // la fachada queda a zβ‰ˆ-0.42: las monedas y diamantes que giran delante no se meten en la pared z.prop('building', x, 0, { floors, width: w, color, z: -1.62, ...extra }); const top = buildingTop(floors); roofLedge(z.game, x, top, w + 0.2); return top; } // ───────────────────────────── Oleadas ───────────────────────────── export interface WaveSpec { type: string; x: number; y: number; props?: Props; delay?: number } /** * Oleada simple: aparece cada enemigo (con humo) tras su `delay`. Devuelve la lista de entidades * (se va llenando). Los tipos no registrados se omiten para no bloquear el guion. */ export function spawnWaveLocal(game: Game, specs: WaveSpec[]): { list: Entity[]; pending: () => number } { const list: Entity[] = []; let pending = 0; for (const s of specs) { if (!game.hasType(s.type)) continue; pending++; game.after(s.delay ?? 0, () => { pending--; if ((list as { cancelled?: boolean }).cancelled) return; game.vfx.burst('poof', s.x, s.y + 0.6, { count: 12, scale: 1.2 }); game.vfx.burst('spark', s.x, s.y + 0.6, { count: 6 }); game.audio.sfx('teleport', { volume: 0.4, pitch: 0.8, x: s.x }); list.push(game.spawn(s.type, s.x, s.y, s.props ?? {})); }); } return { list, pending: () => pending }; }