/** * 🟢 enemy.slime — gota de gelatina gruñona que avanza a saltitos (anticipación: se aplasta, salta, rebota al * caer). Si ve al héroe salta hacia él; si no, deambula sin caerse de los bordes. La grande (`big`) se parte en * dos pequeñas al morir. Congelable y quemable. Ideal para Ciudad (callejones) y orillas. * Props: { facing?, range? (8), patrol?: [x0, x1], aggro?, big?: boolean, color?: number } */ import { createChibi } from '../../characters/chibi'; import type { Props } from '../../core/types'; import { Enemy, approach, clampPatrol, groundAhead, wallAhead } from './base'; import { adoptIntoWaves } from './waves'; export const SLIME = { hp: 2, bigHp: 4, hw: 0.38, hh: 0.36, size: 0.85, bigScale: 1.6, range: 8, wait: [0.7, 1.3], crouch: 0.28, hopVx: 3.4, hopVy: 9.5, bigHopVy: 8, color: 0x66d99a, }; export class Slime extends Enemy { private waitT = 0.5 + Math.random() * 0.6; private readonly big: boolean; private readonly color: number; private air = false; constructor(props: Props) { super(props); this.big = !!props.big; const k = this.big ? SLIME.bigScale : 1; this.hw = SLIME.hw * k; this.hh = SLIME.hh * k; this.maxHp = this.big ? SLIME.bigHp : SLIME.hp; this.color = props.color ?? SLIME.color; this.baseColor = this.color; this.shadowRadius = 0.42 * k; this.contactDamage = 1; this.state = 'wait'; } protected build(): void { const k = this.big ? SLIME.bigScale : 1; this.rig = createChibi({ color: this.color, size: SLIME.size * k, shape: 'ball', expression: 'alien', // Invasor alienígena 👽: salta y, si es grande, se divide emissive: this.color, emissiveIntensity: 0.12, }); this.rig.root.position.y = -0.04 * k; } protected restoreExpression(): void { this.rig?.setExpression('alien'); } protected think(dt: number): void { const b = this.body; const P = this.props; const g = this.game; if (b.onGround) { if (this.air) { this.air = false; this.rig?.squash(-0.4); g.vfx.burst('land', this.x, this.y - this.hh, { scale: this.big ? 1.2 : 0.7 }); g.audio.sfx('land', { pitch: 1.6, volume: 0.35, x: this.x }); } b.vx = approach(b.vx, 0, 30 * dt); } else this.air = true; switch (this.state) { case 'wait': { if (!b.onGround) break; this.waitT -= dt; if (this.waitT <= 0) { const d = this.heroDelta(); const range = P.range ?? SLIME.range; if (d && (P.aggro || (Math.abs(d.dx) < range && Math.abs(d.dy) < 3))) this.facing = d.dx > 0 ? 1 : -1; else if (Math.random() < 0.3) this.facing = -this.facing as 1 | -1; // no saltar al vacío ni contra paredes if (!groundAhead(this, this.facing, 1.4, 1.2) || wallAhead(this, this.facing, 0.6) || clampPatrol(this, P.patrol, this.facing)) { this.facing = -this.facing as 1 | -1; } this.setState('crouch'); this.rig?.squash(-0.35); } break; } case 'crouch': { if (this.stateT >= SLIME.crouch) { b.vx = this.facing * SLIME.hopVx; b.vy = this.big ? SLIME.bigHopVy : SLIME.hopVy; this.air = true; this.rig?.squash(0.45); g.audio.sfx('bounce', { pitch: this.big ? 0.8 : 1.4, volume: 0.4, x: this.x }); this.setState('hop'); } break; } case 'hop': { b.vx = this.facing * SLIME.hopVx; if (b.onWall === this.facing) b.vx = 0; if (b.onGround && this.stateT > 0.1) { this.setState('wait'); this.waitT = SLIME.wait[0] + Math.random() * (SLIME.wait[1] - SLIME.wait[0]); } break; } default: this.setState('wait'); } } protected animate(dt: number): void { const b = this.body; // sin auto-locomoción: el bamboleo de gelatina lo hacemos nosotros this.rig?.update(dt, { vx: 0, vy: 0, onGround: true, facing: this.facing }); const crouch = this.state === 'crouch' ? Math.min(1, this.stateT / SLIME.crouch) : 0; const wob = Math.sin(this.t * 7) * 0.04; const stretch = b.onGround ? 0 : Math.max(-0.15, Math.min(0.2, b.vy * 0.02)); const sy = 1 + wob + stretch - crouch * 0.25; this.visual.scale.set(1 / Math.sqrt(sy), sy, 1 / Math.sqrt(sy)); this.visual.position.y = -(1 - sy) * this.hh; this.visual.position.x = crouch > 0 ? Math.sin(this.t * 60) * 0.02 : 0; } protected onDeath(): void { if (!this.big) return; const g = this.game; for (const s of [-1, 1]) { const e = g.spawn('enemy.slime', this.x + s * 0.5, this.y - this.hh, { color: this.color, aggro: true }); adoptIntoWaves(g, this, e); if (e.body) { e.body.vx = s * 4; e.body.vy = 8; } } } }