/** * 🍄 enemy.mushroom — seta andarina del Bosque Susurro. Camina despacio, gira en los bordes y * suelta esporas al recibir un golpe. Solo es burnable: el FUEGO la quema y el HIELO no la detiene. * Props: { facing?, range?, patrol?: [x0, x1] } */ import * as THREE from 'three'; import { createChibi } from '../../characters/chibi'; import type { Props } from '../../core/types'; import { toonMaterial } from '../../render'; import { Enemy, approach, clampPatrol, groundAhead, wallAhead } from './base'; const MUSH = { hp: 3, hw: 0.38, hh: 0.42, size: 0.82, speed: 1.7, range: 6, color: 0xf2452e, }; const clamp = (v: number, lo: number, hi: number): number => Math.max(lo, Math.min(hi, v)); let spotMat: THREE.Material | null = null; let stemMat: THREE.Material | null = null; let spotGeo: THREE.SphereGeometry | null = null; let stemGeo: THREE.CylinderGeometry | null = null; function sharedMats(): { spot: THREE.Material; stem: THREE.Material; spotGeo: THREE.SphereGeometry; stemGeo: THREE.CylinderGeometry } { if (!spotMat) { spotMat = toonMaterial({ color: 0xfdf6e8, gloss: 0.45, rim: 0.4 }); spotMat.userData.shared = true; stemMat = toonMaterial({ color: 0xf0e2c8, gloss: 0.35, rim: 0.3 }); stemMat.userData.shared = true; spotGeo = new THREE.SphereGeometry(1, 10, 8); spotGeo.userData.shared = true; stemGeo = new THREE.CylinderGeometry(0.16, 0.2, 0.34, 12); stemGeo.userData.shared = true; } return { spot: spotMat!, stem: stemMat!, spotGeo: spotGeo!, stemGeo: stemGeo! }; } export class Mushroom extends Enemy { private pause = 0; private seed = 0; constructor(props: Props) { super(props); this.hw = MUSH.hw; this.hh = MUSH.hh; this.maxHp = MUSH.hp; this.flying = false; this.freezable = false; this.burnable = true; this.contactDamage = 1; this.shadowRadius = 0.42; this.state = 'walk'; } protected build(): void { this.rig = createChibi({ color: MUSH.color, size: MUSH.size, expression: 'evil', feet: true, footColor: 0xf0e2c8, handColor: 0xf6c8b8, }); const m = sharedMats(); // motas blancas del sombrero (sin tapar la cara) const spots: Array<[number, number, number, number]> = [ [0.0, 0.36, -0.02, 0.1], [-0.24, 0.26, 0.04, 0.08], [0.24, 0.28, 0.02, 0.09], [0.0, 0.18, -0.34, 0.11], [-0.16, 0.32, -0.22, 0.07], [0.2, 0.14, -0.32, 0.075], ]; for (const [x, y, z, s] of spots) { const spot = new THREE.Mesh(m.spotGeo, m.spot); spot.scale.set(s, s * 0.42, s); spot.position.set(x, y, z); this.rig.root.add(spot); } // pie blanco de la seta const stem = new THREE.Mesh(m.stemGeo, m.stem); stem.position.set(0, -MUSH.hh + 0.06, -0.08); stem.castShadow = true; this.visual.add(stem); this.seed = Math.random() * Math.PI * 2; } protected think(dt: number): void { const b = this.body; const p = this.props; const d = this.heroDelta(); if (this.pause > 0) { this.pause -= dt; b.vx = approach(b.vx, 0, 22 * dt); return; } let dir: 1 | -1 = this.facing; let speed = MUSH.speed; if (d && Math.abs(d.dx) < (p.range ?? MUSH.range) && Math.abs(d.dy) < 3.2) { dir = (d.dx > 0 ? 1 : -1) as 1 | -1; speed *= 1.18; } else if (Math.random() < 0.0035) { dir = (this.facing === 1 ? -1 : 1) as 1 | -1; } if (clampPatrol(this, p.patrol, dir) || !groundAhead(this, dir, 0.28, 1.1) || wallAhead(this, dir, 0.22)) { dir = (dir === 1 ? -1 : 1) as 1 | -1; if (clampPatrol(this, p.patrol, dir)) speed = 0; } this.facing = (dir < 0 ? -1 : 1) as 1 | -1; b.vx = approach(b.vx, dir * speed, 11 * dt); if (Math.random() < 0.004) this.pause = 0.45 + Math.random() * 0.4; } protected onHurt(): void { this.rig?.play('hurt'); this.game.vfx.burst('dust', this.x, this.y + this.hh * 0.4, { count: 8, scale: 0.8, color: 0xf7d7a8 }); } protected onDeath(): void { this.game.vfx.burst('smoke', this.x, this.y, { count: 10, scale: 1.1, color: 0xf7d7a8 }); } protected animate(dt: number): void { const b = this.body; this.rig?.update(dt, { vx: b.vx, vy: b.vy, onGround: b.onGround, facing: this.facing }); const w = Math.sin(this.t * 5 + this.seed) * 0.05; this.visual.rotation.z = w; } }