/** * Entidades auxiliares del héroe: burbuja-plataforma (`tool.bubble`), bola de fuego (`hero.fireball`) * y chorro de agua (`hero.water`). */ import * as THREE from 'three'; import { Entity } from '../../core/entity'; import type { Body, Collider, Hit, Props } from '../../core/types'; import { isRainingAt } from '../../world/weather'; import { toonMaterial } from '../../render/materials'; import { strike } from './strike'; import { TUNING } from './hero'; import type { Hero } from './hero'; const shared = }>(o: T): T => { o.userData.shared = true; return o; }; let bubbleRes: { geo: THREE.SphereGeometry; shell: THREE.Material; shine: THREE.Material; rim: THREE.Material } | null = null; function bubbleResources() { if (!bubbleRes) { bubbleRes = { geo: shared(new THREE.SphereGeometry(1, 28, 20)), shell: shared(toonMaterial({ color: 0xbfeaff, emissive: 0x7fd6ff, emissiveIntensity: 0.18, gloss: 1, rim: 1, rimPower: 1.6, transparent: true, opacity: 0.32, depthWrite: false, })), shine: shared(toonMaterial({ color: 0xffffff, emissive: 0xffffff, emissiveIntensity: 0.8, transparent: true, opacity: 0.85 })), rim: shared(toonMaterial({ color: 0xf6b3ff, emissive: 0xc58bff, emissiveIntensity: 0.4, transparent: true, opacity: 0.35, depthWrite: false })), }; } return bubbleRes; } /** Burbuja-plataforma. `y` = CENTRO de la burbuja. Colisionador oneWay material 'bubble'. */ export class Bubble extends Entity { private col!: Collider; private t = 0; private bounces = 0; private wasOn = false; private sq = 0; private sqv = 0; private dir: number; private popping = false; private readonly ball = new THREE.Group(); constructor(props: Props) { super(); this.dir = props.dir ?? 1; this.tags.add('platform'); this.tags.add('herobubble'); const r = bubbleResources(); const R = TUNING.bubble.radius; const shell = new THREE.Mesh(r.geo, r.shell); shell.scale.setScalar(R); const inner = new THREE.Mesh(r.geo, r.rim); inner.scale.setScalar(R * 0.9); const shine = new THREE.Mesh(r.geo, r.shine); shine.scale.set(0.13, 0.09, 0.05); shine.position.set(-0.24, 0.27, R * 0.85); shine.rotation.z = 0.6; const dot = new THREE.Mesh(r.geo, r.shine); dot.scale.setScalar(0.04); dot.position.set(-0.08, 0.38, R * 0.8); this.ball.add(inner, shell, shine, dot); this.ball.scale.setScalar(0.2); this.view.add(this.ball); } onAdded(): void { const R = TUNING.bubble.radius; this.col = this.game.physics.add({ x: this.x, y: this.y + R * 0.55, hw: R * 0.9, hh: 0.14, oneWay: true, material: 'bubble', vx: 0, vy: 0, owner: this, tag: 'bubble', }); } onRemoved(): void { this.game.physics.remove(this.col); } pop(): void { if (this.popping) return; this.popping = true; const g = this.game; g.audio.sfx('pop', { x: this.x }); g.vfx.burst('bubble', this.x, this.y, { count: 10, scale: 1 }); g.vfx.ring(this.x, this.y, 0xbfeaff, 0.8); this.kill(); } update(dt: number): void { const B = TUNING.bubble; this.t += dt; const hero = this.game.hero; // El calor de la lava y de las llamas revienta la burbuja: hay que enfriar antes de cruzarla. const hot = this.game.physics.areasAt({ x: this.x, y: this.y - 1.8, hw: 0.42, hh: 2.2 }, 'lava').length > 0 || this.game.physics.areasAt({ x: this.x, y: this.y, hw: 0.42, hh: 0.42 }, 'hazard') .some((a) => a.data?.element === 'fire'); // la lluvia revienta las burbujas (no se pueden usar mientras llueve) const rained = isRainingAt(this.game, this.x, this.y); // los enemigos y llamas «calientes» (etiqueta hot) también las revientan al acercarse let nearHot = false; if (!hot && !rained) { for (const e of this.game.allByTag('hot')) { const r = (e as Entity & { heatRadius?: number }).heatRadius ?? 1.5; if (Math.abs(e.x - this.x) < r && Math.abs(e.y - this.y) < r) { nearHot = true; break; } } // espinos, chorros de vapor… (cada uno decide con pops(x, y)) if (!nearHot) { for (const e of this.game.allByTag('popper')) { const p = (e as Entity & { pops?: (x: number, y: number) => boolean }).pops; if (p && p.call(e, this.x, this.y)) { nearHot = true; break; } } } } if (rained) { this.game.vfx.burst('splash', this.x, this.y + 0.3, { count: 6, scale: 0.6, color: 0x9fdcff }); this.pop(); return; } if (hot || nearHot) { const g = this.game; g.vfx.burst('smoke', this.x, this.y + 0.2, { count: 8, scale: 0.7 }); g.vfx.burst('splash', this.x, this.y, { count: 6, scale: 0.6 }); g.audio.sfx('pop', { pitch: 0.8, x: this.x }); this.pop(); return; } const on = !!hero && hero.body.onGround && hero.body.ground === this.col; if (on && !this.wasOn) { this.bounces++; this.sqv -= 6; this.game.audio.sfx('bounce', { volume: 0.5, pitch: 1.2 + this.bounces * 0.1, x: this.x }); if (this.bounces >= B.maxBounces) { hero!.body.vy = B.popHop; this.pop(); return; } } this.wasOn = on; if (this.t > B.life) { this.pop(); return; } const vy = on ? -0.35 : 0.22 * Math.cos(this.t * 2.2) + 0.08; const vx = 0.3 * Math.sin(this.t * 0.9) * this.dir; this.x += vx * dt; this.y += vy * dt; this.col.x = this.x; this.col.y = this.y + B.radius * 0.55; this.col.vx = vx; this.col.vy = vy; // Muelle visual const acc = -220 * this.sq - 9 * this.sqv; this.sqv += acc * dt; this.sq += this.sqv * dt; } render(alpha: number, dt: number): void { super.render(alpha, dt); const grow = Math.min(1, this.t / 0.18); const s = 0.2 + 0.8 * (1 - Math.pow(1 - grow, 3)) * (grow < 1 ? 1.1 : 1); const end = TUNING.bubble.life - this.t; const wob = end < 1 ? Math.sin(this.t * 40) * 0.05 * (1 - end) : 0; const k = Math.max(-0.4, Math.min(0.4, this.sq * 0.08)); this.ball.scale.set(s * (1 - k * 0.5 + wob), s * (1 + k - wob), s); this.ball.rotation.z = Math.sin(this.t * 1.7) * 0.15; } } let fireRes: { geo: THREE.SphereGeometry; outer: THREE.Material; core: THREE.Material } | null = null; /** Bola de fuego: rebota unas veces, ilumina, quema (element 'fire'). */ export class Fireball extends Entity { declare body: Body; private t = 0; private bounces = 0; private hero: Hero | null; private readonly orb = new THREE.Group(); constructor(props: Props) { super(); this.team = 'hero'; this.tags.add('projectile'); this.tags.add('herofire'); this.hero = props.hero ?? null; const dir = props.dir ?? 1; if (!fireRes) { fireRes = { geo: shared(new THREE.SphereGeometry(1, 16, 12)), outer: shared(toonMaterial({ color: 0xff6a1a, emissive: 0xff4a00, emissiveIntensity: 1.6, transparent: true, opacity: 0.9 })), core: shared(toonMaterial({ color: 0xffe066, emissive: 0xffd000, emissiveIntensity: 2 })), }; } const o = new THREE.Mesh(fireRes.geo, fireRes.outer); o.scale.setScalar(0.26); const c = new THREE.Mesh(fireRes.geo, fireRes.core); c.scale.setScalar(0.15); c.position.z = 0.08; this.orb.add(o, c); this.view.add(this.orb); this.dirX = dir; } private dirX: number; onAdded(): void { const F = TUNING.fire; this.body = this.game.physics.body(this.x, this.y, 0.2, 0.2); this.body.vx = this.dirX * F.speed; this.body.vy = F.vy; if (this.hero) this.hero.fireLight.userData.busy = this.id; } onRemoved(): void { const l = this.hero?.fireLight; if (l && l.userData.busy === this.id) { l.userData.busy = 0; l.intensity = 0; } } private explode(): void { const g = this.game; g.vfx.burst('fire', this.x, this.y, { count: 12, scale: 1.1 }); g.vfx.burst('smoke', this.x, this.y, { count: 4 }); g.audio.sfx('fire', { x: this.x, volume: 0.7 }); this.kill(); } update(dt: number): void { const F = TUNING.fire; const b = this.body; const g = this.game; this.t += dt; b.vy = Math.max(-18, b.vy - F.gravity * dt); g.physics.move(b, dt); if (b.onGround) { const pad = b.ground?.material === 'bubble'; if (!pad) this.bounces++; if (!pad && this.bounces > F.bounces) return this.explode(); b.vy = pad ? F.bounceVy * 1.9 : F.bounceVy * Math.pow(0.85, this.bounces - 1); if (pad) { g.audio.sfx('bounce', { pitch: 0.8, volume: 0.65, x: b.x }); g.vfx.ring(b.x, b.y - 0.25, 0xffb040, 0.9); } g.vfx.burst('fire', b.x, b.y - 0.2, { count: 3, scale: 0.6 }); } // La lava o una llama apagan la bola con un siseo de vapor. const hot = g.physics.areasAt({ x: b.x, y: b.y, hw: 0.2, hh: 0.2 }, 'lava').length > 0 || g.physics.areasAt({ x: b.x, y: b.y, hw: 0.2, hh: 0.2 }, 'hazard') .some((a) => a.data?.element === 'fire'); // la lluvia también la apaga en pleno vuelo if (hot || isRainingAt(g, b.x, b.y)) { g.vfx.burst('smoke', b.x, b.y, { count: 10, scale: 0.8 }); g.audio.sfx('splash', { pitch: 1.4, volume: 0.4, x: b.x }); this.kill(); return; } // Primero el golpe: zarzas, telarañas y la puerta de hielo tienen su propio colisionador y // la bola choca con él antes de que la física la marque como pared. const hits = strike(g, b, { damage: F.damage, element: 'fire', dirX: Math.sign(b.vx) || 1, knock: 6, stun: 0.3, source: this }); if (hits.length) { g.hitstop(0.04); g.gfx.cam.shake(0.15, 0.1); return this.explode(); } if (b.onWall || b.onCeiling || this.t > F.life) return this.explode(); g.vfx.trail('fire', b.x, b.y, { scale: 0.7 }); const l = this.hero?.fireLight; if (l && l.userData.busy === this.id) { l.position.set(b.x, b.y, 1); l.intensity = 9; } } render(alpha: number, dt: number): void { super.render(alpha, dt); const p = 1 + Math.sin(this.t * 40) * 0.08; this.orb.scale.set(p, 1 / p, 1); this.orb.rotation.z += dt * 12; } } let waterRes: { geo: THREE.SphereGeometry; mat: THREE.Material } | null = null; /** Chorro de agua de la pistola (element 'water'). */ export class WaterShot extends Entity { declare body: Body; private t = 0; private readonly drop: THREE.Mesh; private dirX: number; private up: boolean; constructor(props: Props) { super(); this.team = 'hero'; this.tags.add('projectile'); this.dirX = props.dir ?? 1; this.up = !!props.up; if (!waterRes) { waterRes = { geo: shared(new THREE.SphereGeometry(1, 12, 8)), mat: shared(toonMaterial({ color: 0x7fd4ff, emissive: 0x2fa8f5, emissiveIntensity: 0.4, gloss: 1, transparent: true, opacity: 0.85 })), }; } this.drop = new THREE.Mesh(waterRes.geo, waterRes.mat); this.drop.scale.set(0.26, 0.12, 0.12); this.view.add(this.drop); } onAdded(): void { const W = TUNING.water; this.body = this.game.physics.body(this.x, this.y, 0.14, 0.12); let ang = this.up ? 0.75 : 0.06; const aim = this.aimAtFlyer(); if (aim !== null) ang = aim; this.body.vx = Math.cos(ang) * W.speed * this.dirX; this.body.vy = Math.sin(ang) * W.speed; } /** Ayuda de puntería: si hay un volador delante y al alcance, el chorro se dirige hacia él. */ private aimAtFlyer(): number | null { const W = TUNING.water; let best: number | null = null; let bestDist: number = W.aimRange; for (const e of this.game.allByTag('enemy')) { const f = e as Entity & { flying?: boolean; defeated?: boolean }; if (!f.flying || f.defeated || e.tags.has('boss')) continue; const dx = (e.x - this.x) * this.dirX; const dy = e.y - this.y; const dist = Math.hypot(dx, dy); if (dx <= 0.2 || dist >= bestDist) continue; const raw = Math.atan2(dy, dx); if (raw > W.aimUp || raw < -W.aimDown) continue; // compensa la caída del chorro const t = dist / W.speed; best = Math.atan2(dy + 0.5 * W.gravity * t * t, dx); bestDist = dist; } return best; } private splash(): void { this.game.vfx.burst('splash', this.x, this.y, { count: 5, scale: 0.6 }); this.kill(); } update(dt: number): void { const W = TUNING.water; const b = this.body; const g = this.game; this.t += dt; b.vy -= W.gravity * dt; g.physics.move(b, dt); // El golpe va antes que el choque de pared: las flores y las pozas tienen colisión propia. const hits = strike(g, b, { damage: W.damage, element: 'water', dirX: this.dirX, knock: 3, source: this }); if (hits.length) { g.audio.sfx('splash', { volume: 0.5, pitch: 1.3, x: b.x }); this.splash(); return; } if (b.onGround || b.onWall || b.onCeiling) return this.splash(); if (this.t > W.life) { g.vfx.burst('splash', b.x, b.y, { count: 2, scale: 0.4 }); this.kill(); return; } } render(alpha: number, dt: number): void { super.render(alpha, dt); const b = this.body; this.drop.rotation.z = Math.atan2(b.vy, b.vx); } } // ───────────────────────────── Bloque de hielo (HIELO + LLUVIA) ───────────────────────────── let iceBlockRes: { geo: THREE.BufferGeometry; body: THREE.Material; core: THREE.Material; shine: THREE.Material } | null = null; function iceBlockResources() { if (!iceBlockRes) { // cubito facetado: caja con las esquinas biseladas const g = new THREE.BoxGeometry(1, 1, 1, 2, 2, 2); const p = g.attributes.position as THREE.BufferAttribute; const v = new THREE.Vector3(); for (let i = 0; i < p.count; i++) { v.fromBufferAttribute(p, i); const k = 1 - 0.1 * (Math.abs(v.x) + Math.abs(v.y) + Math.abs(v.z) - 0.5); v.multiplyScalar(Math.min(1, k)); p.setXYZ(i, v.x, v.y, v.z); } g.computeVertexNormals(); iceBlockRes = { geo: shared(g), body: shared(toonMaterial({ color: 0xcff0ff, emissive: 0x5cc4ff, emissiveIntensity: 0.22, gloss: 1, rim: 1, transparent: true, opacity: 0.78, })), core: shared(toonMaterial({ color: 0xffffff, emissive: 0x9fdcff, emissiveIntensity: 0.35, gloss: 1, transparent: true, opacity: 0.45, depthWrite: false })), shine: shared(toonMaterial({ color: 0xffffff, emissive: 0xffffff, emissiveIntensity: 0.7, transparent: true, opacity: 0.85 })), }; } return iceBlockRes; } export const ICE_BLOCK = { size: 1, life: 14, max: 5 }; /** * Bloque de hielo creado por el aliento de HIELO con la LLUVIA activa (las gotas se congelan). * Sólido: sirve de escalón para trepar, de puente corto y de BARRERA contra proyectiles. * Se derrite con el tiempo, con FUEGO o sobre la lava. `y` = centro del bloque. */ export class IceBlock extends Entity { private col!: Collider; private t = 0; private melting = false; private meltT = 0; private readonly art = new THREE.Group(); private grow = 0; constructor(_props: Props) { super(); this.team = 'neutral'; this.tags.add('iceblock'); this.tags.add('platform'); const r = iceBlockResources(); const body = new THREE.Mesh(r.geo, r.body); body.castShadow = true; body.receiveShadow = true; const core = new THREE.Mesh(r.geo, r.core); core.scale.setScalar(0.55); core.rotation.set(0.5, 0.7, 0.2); const s1 = new THREE.Mesh(r.geo, r.shine); s1.scale.set(0.06, 0.42, 0.04); s1.position.set(-0.28, 0.08, 0.5); s1.rotation.z = 0.35; const s2 = new THREE.Mesh(r.geo, r.shine); s2.scale.set(0.05, 0.16, 0.04); s2.position.set(-0.12, 0.3, 0.5); s2.rotation.z = 0.35; this.art.add(body, core, s1, s2); this.art.scale.setScalar(0.3); this.view.add(this.art); } onAdded(): void { const h = ICE_BLOCK.size / 2; this.col = this.game.physics.add({ x: this.x, y: this.y, hw: h, hh: h, oneWay: false, material: 'ice', owner: this, tag: 'iceblock', }); this.game.audio.sfx('freeze', { x: this.x, pitch: 1.1 }); this.game.vfx.burst('ice', this.x, this.y, { count: 12, scale: 1 }); } onRemoved(): void { this.game.physics.remove(this.col); } /** El fuego lo derrite al instante; el resto de golpes no le hace nada. */ takeHit(hit: Hit): boolean { if (hit.element === 'fire') { this.melt(); return true; } return false; } melt(): void { if (this.melting) return; this.melting = true; this.meltT = 0.35; this.col.enabled = false; this.game.audio.sfx('splash', { x: this.x, volume: 0.5, pitch: 1.3 }); this.game.vfx.burst('splash', this.x, this.y, { count: 10, color: 0xbfeaff, scale: 0.9 }); this.game.vfx.burst('smoke', this.x, this.y + 0.3, { count: 5, scale: 0.6 }); } update(dt: number): void { this.t += dt; if (this.melting) { this.meltT -= dt; if (this.meltT <= 0) this.kill(); return; } const g = this.game; const box = { x: this.x, y: this.y, hw: 0.5, hh: 0.5 }; if (this.t > ICE_BLOCK.life || g.physics.areasAt(box, 'lava').length) { this.melt(); return; } for (const e of g.allByTag('hot')) { if (Math.abs(e.x - this.x) < 1.2 && Math.abs(e.y - this.y) < 1.2) { this.melt(); return; } } } render(alpha: number, dt: number): void { super.render(alpha, dt); this.grow = Math.min(1, this.grow + dt * 7); const pop = this.grow < 1 ? 1 + Math.sin(this.grow * Math.PI) * 0.18 : 1; // en los últimos segundos se encoge un poco (aviso de que se derrite) const left = ICE_BLOCK.life - this.t; const fade = left < 2.5 ? 0.9 + 0.1 * Math.max(0, left / 2.5) : 1; const k = this.melting ? Math.max(0.05, this.meltT / 0.35) : (0.3 + 0.7 * this.grow) * pop * fade; this.art.scale.set(k, this.melting ? k * 0.6 : k, k); } }