/** * ObstΓ‘culos de poderes compartidos por el bosque y el castillo. * * Β· hazard.thorns β€” Zarzas espinosas 🌿: franja de espinos (en el suelo, colgando del techo o sobre un * abismo). Pinchan al hΓ©roe y REVIENTAN LAS BURBUJAS (no se puede pasar flotando por * encima). Solo el FUEGO las quema (queda guardado). * Props: { w, flag, hang?: true (cuelgan del techo; y = techo), h?, wall?: true (matorral que bloquea el paso) } * Β· hazard.vent β€” Chorro de vapor ♨️: rejilla que expulsa vapor ardiente a intervalos. Quema y revienta * burbujas. Un BLOQUE DE HIELO (HIELO + LLUVIA) encima la tapa mientras dure. * Props: { h? (altura del chorro, 4), period? (2.6), offset?, quench? (bandera que lo apaga, p. ej. poza llena) } * * Las burbujas consultan a las entidades con etiqueta 'popper' mediante `pops(x, y)`. */ import * as THREE from 'three'; import { Entity } from '../core/entity'; import type { Collider, Hit, Props } from '../core/types'; import type { Game } from '../core/game'; import { kitMat } from './kit/util'; const shared = }>(o: T): T => { o.userData.shared = true; return o; }; const GEO = { cyl: shared(new THREE.CylinderGeometry(1, 1, 1, 10)), cone: shared(new THREE.ConeGeometry(1, 1, 8)), sph: shared(new THREE.SphereGeometry(1, 12, 9)), box: shared(new THREE.BoxGeometry(1, 1, 1)), }; const MAT = { vine: kitMat('hzVine', { color: 0x3c6b2a, gloss: 0.35, rim: 0.35 }), vineDark: kitMat('hzVineDark', { color: 0x2a4d1f, gloss: 0.3, rim: 0.3 }), thorn: kitMat('hzThorn', { color: 0x8a3a2a, gloss: 0.5, rim: 0.4 }), berry: kitMat('hzBerry', { color: 0xc02a5a, gloss: 0.8, rim: 0.6 }), ash: kitMat('hzAsh', { color: 0x3a3236, gloss: 0.1, rim: 0.1 }), iron: kitMat('hzIron', { color: 0x4a4e5c, gloss: 0.6, rim: 0.5 }), ironDark: kitMat('hzIronDark', { color: 0x262836, gloss: 0.4, rim: 0.3 }), ember: kitMat('hzEmber', { color: 0xffb040, emissive: 0xff6a10, emissiveIntensity: 0.7, gloss: 0.5 }), snow: kitMat('hzSnow', { color: 0xffffff, emissive: 0xdfefff, emissiveIntensity: 0.4, gloss: 0.3 }), steam: kitMat('hzSteam', { color: 0xffffff, emissive: 0xdfe8ff, emissiveIntensity: 0.25, transparent: true, opacity: 0.42, depthWrite: false, gloss: 0.05, rim: 0.6 }), }; function mesh(parent: THREE.Object3D, geo: THREE.BufferGeometry, mat: THREE.Material, x: number, y: number, z: number, sx: number, sy: number, sz: number, rz = 0, rx = 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, 0, rz); m.castShadow = true; m.receiveShadow = true; parent.add(m); return m; } // ───────────────────────────── Zarzas espinosas ───────────────────────────── interface ThornProps extends Props { w: number; flag?: string; hang?: boolean; h?: number; wall?: boolean } export class Thorns extends Entity { private readonly o: ThornProps; private readonly art = new THREE.Group(); private burnt = false; private burnT = -1; private hurtCd = 0; private wall: Collider | null = null; private readonly h: number; constructor(props: ThornProps) { super(); this.o = props; this.h = props.h ?? 0.9; this.team = 'neutral'; this.tags.add('popper'); this.tags.add('burnable'); this.buildArt(); this.view.add(this.art); } /** Caja de la franja de espinos (centro y semitamaΓ±os). */ private box(): { x: number; y: number; hw: number; hh: number } { const hh = this.h / 2; return { x: this.x, y: this.o.hang ? this.y - hh : this.y + hh, hw: this.o.w / 2, hh }; } private buildArt(): void { const w = this.o.w; const h = this.h; const dir = this.o.hang ? -1 : 1; let seed = Math.abs(Math.floor(w * 977 + h * 131 + (this.o.hang ? 13 : 0))) + 1; const rnd = () => ((seed = (seed * 16807) % 2147483647) / 2147483647); // Matorral espinoso: matas redondas de hoja oscura apiladas, con espinas rojizas que asoman por fuera // y alguna baya. Se lee como Β«arbusto con pinchosΒ» (no como palos ni telaraΓ±as). const cols = Math.max(1, Math.round(w / 0.8)); const rows = Math.max(1, Math.round(h / 0.75)); for (let r = 0; r < rows; r++) { for (let c = 0; c < cols; c++) { const x = -w / 2 + (c + 0.5) * (w / cols) + (rnd() - 0.5) * 0.18; const y = dir * ((r + 0.5) * (h / rows)); const rad = Math.min(w / cols, h / rows) * (0.62 + rnd() * 0.16); const z = (rnd() - 0.5) * 0.3; mesh(this.art, GEO.sph, (r + c) % 2 ? MAT.vine : MAT.vineDark, x, y, z, rad, rad * 0.9, rad * 0.85); // espinas radiales hacia fuera (las de delante y de los lados) const spikes = 5; for (let k = 0; k < spikes; k++) { const ang = (k / spikes) * Math.PI * 2 + rnd() * 0.6; const ox = Math.cos(ang) * rad * 0.95; const oy = Math.sin(ang) * rad * 0.85; const t = mesh(this.art, GEO.cone, MAT.thorn, x + ox, y + oy, z + rad * 0.45, 0.06, 0.26, 0.06, ang - Math.PI / 2); t.rotation.x = 0.35; } if (rnd() < 0.35) mesh(this.art, GEO.sph, MAT.berry, x + rad * 0.3, y + rad * 0.2, z + rad * 0.8, 0.08, 0.08, 0.08); } } // ramitas entrelazadas por delante para unir las matas mesh(this.art, GEO.cyl, MAT.vineDark, 0, dir * h * 0.35, 0.45, 0.05, w * 0.9, 0.05, Math.PI / 2 - 0.08); if (h > 1.2) mesh(this.art, GEO.cyl, MAT.vineDark, 0, dir * h * 0.7, 0.4, 0.05, w * 0.85, 0.05, Math.PI / 2 + 0.1); } onAdded(): void { if (this.o.flag && this.game.state.has(this.o.flag)) { this.burnt = true; this.art.visible = false; return; } // como matorral-muro (wall) bloquea el paso hasta que se quema if (this.o.wall) { const b = this.box(); this.wall = this.game.physics.box(b.x, b.y, b.hw * 2 - 0.1, b.hh * 2, { material: 'wood', tag: 'thorns', owner: this }); } } onRemoved(): void { if (this.wall) this.game.physics.remove(this.wall); } bounds(): { x: number; y: number; hw: number; hh: number } { return this.burnt ? { x: this.x, y: -999, hw: 0, hh: 0 } : this.box(); } /** ΒΏRevienta una burbuja en (x, y)? */ pops(x: number, y: number): boolean { if (this.burnt) return false; const b = this.box(); return Math.abs(x - b.x) < b.hw + 0.55 && Math.abs(y - b.y) < b.hh + 0.55; } takeHit(hit: Hit): boolean { if (this.burnt) return false; if (hit.element === 'fire') { if (this.wall) this.wall.enabled = false; this.burn(); return true; } this.game.vfx.floatText('Β‘Pinchan! QuΓ©malas con FUEGO', this.x, this.y + (this.o.hang ? -1.4 : 1.6), '#ffb15a'); return false; } private burn(): void { if (this.burnt || this.burnT >= 0) return; this.burnT = 0; const g = this.game; g.audio.sfx('fire', { x: this.x }); if (this.o.flag) { g.state.set(this.o.flag, true); g.state.save(); } } update(dt: number): void { if (this.burnt) return; const g = this.game; if (this.burnT >= 0) { this.burnT += dt; if (Math.random() < 0.6) { const b = this.box(); g.vfx.trail('fire', this.x + (Math.random() - 0.5) * this.o.w, b.y + (Math.random() - 0.5) * b.hh * 2, { scale: 1 }); } if (this.burnT > 0.9) { this.burnt = true; if (this.wall) this.wall.enabled = false; this.art.visible = false; const b = this.box(); g.vfx.burst('smoke', this.x, b.y, { count: 14, scale: 1.2 }); g.vfx.burst('dust', this.x, b.y, { count: 10, color: 0x3a3236 }); } return; } // pinchan al hΓ©roe this.hurtCd = Math.max(0, this.hurtCd - dt); const h = g.hero; if (!h || this.hurtCd > 0) return; const b = this.box(); if (Math.abs(h.x - b.x) < b.hw + h.body.hw - 0.1 && Math.abs(h.y - b.y) < b.hh + h.body.hh - 0.1) { this.hurtCd = 0.8; const dir = this.o.hang ? 0 : h.x < this.x ? -1 : 1; if (h.takeHit({ damage: 1, element: 'none', dirX: dir || h.facing, knock: 6, source: this })) { h.body.vy = this.o.hang ? -4 : 9; } } } render(alpha: number, dt: number): void { super.render(alpha, dt); if (this.burnT >= 0 && !this.burnt) { const k = 1 - Math.min(1, this.burnT / 0.9); this.art.scale.set(1, Math.max(0.05, k), 1); } else if (!this.burnt) { this.art.rotation.z = Math.sin(this.game.time * 1.3 + this.x) * 0.015; } } } // ───────────────────────────── Chorro de vapor ───────────────────────────── interface VentProps extends Props { h?: number; period?: number; offset?: number; quench?: string } const VENT = { warn: 0.6, on: 1.3 }; export class SteamVent extends Entity { private readonly o: VentProps; private readonly art = new THREE.Group(); private readonly jet = new THREE.Group(); private t: number; private hurtCd = 0; private puffT = 0; private sealed = false; private readonly hgt: number; private readonly period: number; constructor(props: VentProps) { super(); this.o = props; this.hgt = props.h ?? 4; this.period = props.period ?? 2.6; this.t = props.offset ?? 0; this.team = 'neutral'; this.tags.add('popper'); this.tags.add('vent'); // rejilla de hierro con brasas mesh(this.art, GEO.cyl, MAT.ironDark, 0, 0.06, 0, 0.62, 0.12, 0.62); for (let i = -1; i <= 1; i++) mesh(this.art, GEO.box, MAT.iron, i * 0.22, 0.14, 0, 0.08, 0.06, 0.9); mesh(this.art, GEO.cyl, MAT.ember, 0, 0.09, 0, 0.45, 0.04, 0.45); // columna de vapor: varias nubecillas apiladas for (let i = 0; i < 9; i++) { const s = 0.38 + i * 0.07; mesh(this.jet, GEO.sph, MAT.steam, 0, (i + 0.5) * (this.hgt / 9), 0, s, s * 0.9, s); } this.jet.visible = false; this.art.add(this.jet); this.view.add(this.art); } private get phase(): 'idle' | 'warn' | 'on' { const u = ((this.t % this.period) + this.period) % this.period; const onStart = this.period - VENT.on; if (u >= onStart) return 'on'; if (u >= onStart - VENT.warn) return 'warn'; return 'idle'; } private get active(): boolean { return !this.sealed && this.phase === 'on'; } pops(x: number, y: number): boolean { return this.active && Math.abs(x - this.x) < 0.9 && y > this.y - 0.2 && y < this.y + this.hgt + 0.4; } update(dt: number): void { const g = this.game; this.t += dt; // un bloque de hielo encima la tapa this.sealed = g.allByTag('iceblock').some((e) => !e.dead && Math.abs(e.x - this.x) < 0.8 && e.y > this.y && e.y < this.y + 1.3) // anegado (la lluvia llenΓ³ la poza): ya no sale vapor || (!!this.o.quench && g.state.has(this.o.quench)); const ph = this.phase; this.puffT -= dt; if (this.sealed) { if (this.puffT <= 0) { this.puffT = 0.4; g.vfx.trail('smoke', this.x + (Math.random() < 0.5 ? -0.6 : 0.6), this.y + 0.2, { scale: 0.5, color: 0xf4f6ff }); } } else if (ph === 'warn' && this.puffT <= 0) { this.puffT = 0.12; g.vfx.trail('smoke', this.x + (Math.random() - 0.5) * 0.4, this.y + 0.3, { scale: 0.6, color: 0xf4f6ff }); } this.jet.visible = this.active; if (!this.active) return; this.hurtCd = Math.max(0, this.hurtCd - dt); const h = g.hero; if (h && this.hurtCd <= 0 && Math.abs(h.x - this.x) < 0.55 + h.body.hw && h.y - h.body.hh < this.y + this.hgt && h.y + h.body.hh > this.y) { this.hurtCd = 0.9; if (h.takeHit({ damage: 1, element: 'fire', dirX: h.x < this.x ? -1 : 1, knock: 7, source: this })) h.body.vy = 8; } } render(alpha: number, dt: number): void { super.render(alpha, dt); if (!this.jet.visible) return; const t = this.game.time; const n = this.jet.children.length; this.jet.children.forEach((c, i) => { // cada nubecilla sube y se agranda; al llegar arriba vuelve abajo const u = ((i / n) + t * 0.9) % 1; c.position.y = u * this.hgt; c.position.x = Math.sin(t * 5 + i * 1.7) * 0.12 * u; const s = 0.35 + u * 0.55; c.scale.set(s, s * 0.85, s); }); } } // ───────────────────────────── Ventisca ───────────────────────────── interface BlizzardProps extends Props { w: number; h: number } /** * Ventisca ❄️: corriente de aire helado (solo visual + 'popper'). Las burbujas se congelan y estallan * dentro; el hΓ©roe y los bloques de hielo no se ven afectados. Obliga a usar bloques de HIELO + LLUVIA. * `x, y` = centro. */ export class Blizzard extends Entity { private readonly o: BlizzardProps; private readonly flakes: THREE.Mesh[] = []; private readonly seeds: number[] = []; constructor(props: BlizzardProps) { super(); this.o = props; this.team = 'neutral'; this.tags.add('popper'); const n = Math.round(props.w * props.h * 0.9); for (let i = 0; i < n; i++) { const m = new THREE.Mesh(GEO.sph, MAT.snow); m.scale.setScalar(0.05 + (i % 3) * 0.025); this.flakes.push(m); this.seeds.push(i * 0.6180339 % 1); this.view.add(m); } } pops(x: number, y: number): boolean { return Math.abs(x - this.x) < this.o.w / 2 + 0.3 && Math.abs(y - this.y) < this.o.h / 2 + 0.3; } render(alpha: number, dt: number): void { super.render(alpha, dt); const t = this.game.time; const { w, h } = this.o; for (let i = 0; i < this.flakes.length; i++) { const s = this.seeds[i]; // remolino que sube en diagonal y vuelve a empezar const u = (s + t * (0.18 + (i % 4) * 0.03)) % 1; const f = this.flakes[i]; f.position.set(((s * 7.3 + u * 0.6) % 1 - 0.5) * w + Math.sin(t * 2 + i) * 0.25, (u - 0.5) * h, ((i % 5) - 2) * 0.25); } } } export function installHazards(game: Game): void { game.register('hazard.thorns', (_g, _x, _y, p) => new Thorns(p as ThornProps)); game.register('hazard.vent', (_g, _x, _y, p) => new SteamVent(p as VentProps)); game.register('hazard.blizzard', (_g, _x, _y, p) => new Blizzard(p as BlizzardProps)); }