/** * Base de enemigos de Emoji Heroes. * * `Enemy` resuelve lo común: cuerpo físico, vida, reacción a golpes por elemento, daño por contacto, * pisotón/picado del héroe, congelación (bloque de hielo sólido), quemadura, mareo (COOL), empuje de agua, * muerte con premio y sombra de contacto. Cada subclase implementa `build()` (vista) y `think(dt)` (IA). * * Convención de posición: `y` del spawn = pies (base del cuerpo), salvo que el tipo diga otra cosa. * Props comunes: `{ facing?: 1|-1, range?: number, patrol?: [x0, x1], aggro?: boolean }`. */ import * as THREE from 'three'; import { Entity } from '../../core/entity'; import type { Body, Collider, Element, Hit, Props } from '../../core/types'; import type { ChibiRig } from '../../characters/types'; import { blobShadow, emojiSprite, toonMaterial } from '../../render'; // ───────────────────────────── Tuning común ───────────────────────────── export const ENEMY = { gravity: 34, maxFall: 20, /** Fricción horizontal mientras está aturdido / retrocediendo. */ friction: 14, hurt: { minTime: 0.22, knock: 6, knockY: 4.5, hitstop: 0.035, flash: 0.16 }, contact: { damage: 1, knock: 8, shrink: 0.12 }, /** Pisotón (caer sobre él sin picado). */ stomp: { damage: 1, bounce: 12, stun: 0.6 }, /** Si el héroe cae más rápido que esto está en picado: lo resuelve hero.ts (strike + rebote). */ diveVy: -15.5, ice: { time: 3.2, pad: 0.16, strongDamage: 2, shatterBonus: 2, cracksToBreak: 2 }, fire: { bonus: 1, burnTime: 1.6 }, cool: { defaultStun: 3 }, water: { push: 9, pushY: 2.5, time: 0.35, /** Los voladores reciben menos empuje (así no se escapan del chorro). */ flyPush: 2.2, /** Chorros necesarios por cada punto de daño. */ flyShots: 1, groundShots: 3, }, death: { time: 0.22, heartChance: 0.35 }, }; const _tmpBox = { x: 0, y: 0, hw: 0, hh: 0 }; // ───────────────────────────── Recursos compartidos ───────────────────────────── let iceGeo: THREE.BufferGeometry | null = null; let iceMat: THREE.Material | null = null; let iceShineMat: THREE.Material | null = null; function iceResources(): { geo: THREE.BufferGeometry; mat: THREE.Material; shine: THREE.Material } { if (!iceGeo) { // caja redondeada barata: icosaedro achatado facetado = bloque de hielo cristalino 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); // bisela las esquinas hacia una forma de "cubito" const k = 1 - 0.12 * (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(); g.userData.shared = true; iceGeo = g; const m = toonMaterial({ color: 0xbfeaff, emissive: 0x4aa8e8, emissiveIntensity: 0.25, gloss: 1, rim: 1, transparent: true, opacity: 0.5, depthWrite: false, }); m.userData.shared = true; iceMat = m; const s = toonMaterial({ color: 0xffffff, emissive: 0xffffff, emissiveIntensity: 0.9, transparent: true, opacity: 0.8 }); s.userData.shared = true; iceShineMat = s; } return { geo: iceGeo, mat: iceMat!, shine: iceShineMat! }; } let shineGeo: THREE.BufferGeometry | null = null; function sharedShineGeo(): THREE.BufferGeometry { if (!shineGeo) { shineGeo = new THREE.CapsuleGeometry(0.035, 0.32, 3, 6); shineGeo.userData.shared = true; } return shineGeo; } /** Mapa de profundidad para sprites. */ const DIZZY_STARS = 3; // ───────────────────────────── Enemy ───────────────────────────── export type EnemyState = string; export abstract class Enemy extends Entity { declare body: Body; readonly props: Props; facing: 1 | -1 = 1; // ── configuración (las subclases la fijan en el constructor) ── /** Semiancho / semialto del cuerpo físico. */ hw = 0.42; hh = 0.45; maxHp = 2; freezable = true; burnable = true; /** El agua le hace daño (por defecto solo empuja). */ waterWeak = false; /** Sin gravedad (voladores). */ flying = false; /** Atraviesa paredes y suelos. */ ghostly = false; contactDamage = ENEMY.contact.damage; contactElement: Element = 'none'; /** El pisotón (caer encima sin picado) le hace daño. */ stompable = true; /** Radio de la sombra de contacto (0 = sin sombra). */ shadowRadius = 0.45; /** Premio al morir: 'star' = moneda 🪙 (por defecto), 'none'. */ drop: 'star' | 'none' = 'star'; /** Altura extra de las estrellitas de mareo sobre la cabeza. */ dizzyHeight = 0.25; // ── estado ── rig: ChibiRig | null = null; /** Contenedor de la vista del personaje (escala, giros propios). */ readonly visual = new THREE.Group(); state: EnemyState = 'idle'; stateT = 0; t = 0; /** true desde que muere (aunque siga en escena la animación). */ defeated = false; frozenT = 0; stunT = 0; hurtT = 0; burnT = 0; waterT = 0; private waterHits = 0; private dyingT = -1; private lastHitBy: Element = 'none'; private iceCracks = 0; private iceCol: Collider | null = null; private iceMesh: THREE.Group | null = null; private dizzy: THREE.Group | null = null; private shadow: THREE.Mesh | null = null; private groundY: number | null = null; private fxT = 0; private stompLock = 0; private iceGrow = 0; constructor(props: Props = {}) { super(); this.props = props; this.team = 'enemy'; this.tags.add('enemy'); if (props.facing === -1 || props.facing === 1) this.facing = props.facing; this.view.add(this.visual); } // ───────── a implementar ───────── /** Construye la vista dentro de `this.visual` (y `this.rig` si usa chibi). */ protected abstract build(): void; /** IA: escribe body.vx/vy. Solo se llama cuando no está congelado, aturdido ni retrocediendo. */ protected abstract think(dt: number): void; // ───────── ciclo ───────── onAdded(): void { this.hp = this.maxHp; // y del spawn = pies → centro del cuerpo const cy = this.spawnCenterY(this.y); this.body = this.game.physics.body(this.x, cy, this.hw, this.hh); this.body.noCollide = this.ghostly; this.x = this.px = this.body.x; this.y = this.py = this.body.y; for (const tag of ['freezable', 'burnable'] as const) if (this[tag]) this.tags.add(tag); this.build(); if (this.rig) this.visual.add(this.rig.root); if (this.shadowRadius > 0) { this.shadow = blobShadow(this.shadowRadius, 0.34); this.view.add(this.shadow); } this.setPos(this.x, this.y); this.afterAdded(); } /** Convierte la y del spawn en la y del centro del cuerpo. */ protected spawnCenterY(y: number): number { return y + this.hh; } protected afterAdded(): void {} onRemoved(): void { this.removeIce(); this.rig?.dispose(); } /** ¿Puede actuar la IA? */ get active(): boolean { return !this.defeated && this.frozenT <= 0 && this.stunT <= 0 && this.hurtT <= 0; } get frozen(): boolean { return this.frozenT > 0; } get hero() { const h = this.game.hero; return h && h.alive ? h : null; } /** Distancia con signo al héroe (dx > 0: el héroe está a la derecha). */ heroDelta(): { dx: number; dy: number; dist: number } | null { const h = this.hero; if (!h) return null; const dx = h.x - this.x, dy = h.y - this.y; return { dx, dy, dist: Math.hypot(dx, dy) }; } setState(s: EnemyState): void { this.state = s; this.stateT = 0; } update(dt: number): void { const b = this.body; this.t += dt; this.stateT += dt; this.stompLock = Math.max(0, this.stompLock - dt); if (this.dyingT >= 0) { this.dyingT -= dt; if (this.dyingT < 0) this.finishDeath(); return; } // ── congelado: bloque quieto, sólido ── if (this.frozenT > 0) { this.frozenT -= dt; b.vx = 0; b.vy = 0; this.tryEnableIce(); if (this.frozenT <= 0) this.thaw(false); return; } if (this.stunT > 0) this.stunT -= dt; if (this.hurtT > 0) this.hurtT -= dt; if (this.waterT > 0) this.waterT -= dt; if (this.burnT > 0) { this.burnT -= dt; this.fxT -= dt; if (this.fxT <= 0) { this.fxT = 0.07; this.game.vfx.trail('fire', this.x + (Math.random() - 0.5) * this.hw, this.y + this.hh * 0.6, { scale: 0.8 }); } } if (this.active) { this.think(dt); } else { // retroceso / mareo: frena const f = Math.min(1, ENEMY.friction * dt); b.vx -= b.vx * f; if (this.flying && !this.stunFalls()) b.vy -= b.vy * f; } const grav = !this.flying || ((this.stunT > 0 || this.hurtT > 0) && this.stunFalls()); if (grav) b.vy = Math.max(-ENEMY.maxFall, b.vy - ENEMY.gravity * dt); this.game.physics.move(b, dt); this.afterMove(dt); this.checkContact(); } /** Voladores: ¿cae al suelo cuando está aturdido? */ protected stunFalls(): boolean { return false; } protected afterMove(_dt: number): void {} // ───────── contacto con el héroe ───────── /** Caja de daño por contacto (por defecto el cuerpo encogido). */ protected contactBox(): { x: number; y: number; hw: number; hh: number } { const s = ENEMY.contact.shrink; _tmpBox.x = this.body.x; _tmpBox.y = this.body.y; _tmpBox.hw = Math.max(0.1, this.hw - s); _tmpBox.hh = Math.max(0.1, this.hh - s); return _tmpBox; } /** ¿Hace daño ahora al tocarlo? */ protected harmful(): boolean { return this.active; } private checkContact(): void { const h = this.hero; if (!h || this.defeated || this.frozenT > 0) return; const hb = h.body; const box = this.contactBox(); if (Math.abs(hb.x - box.x) > hb.hw + box.hw || Math.abs(hb.y - box.y) > hb.hh + box.hh) return; const feet = hb.y - hb.hh; const fromAbove = hb.vy < 0 && feet > box.y + box.hh * 0.2; if (fromAbove) { // El picado (vy muy negativa) lo resuelve hero.ts con strike(): no interferir. if (hb.vy <= ENEMY.diveVy) return; if (this.stompable && this.stompLock <= 0) { this.stompLock = 0.2; this.onStomp(); return; } } if (!this.harmful() || this.contactDamage <= 0) return; const dir = hb.x < this.x ? -1 : 1; h.takeHit({ damage: this.contactDamage, element: this.contactElement, dirX: dir, knock: ENEMY.contact.knock, source: this }); } /** El héroe cae encima (sin picado): daño leve y rebote. */ protected onStomp(): void { const h = this.game.hero; if (!h) return; h.body.vy = ENEMY.stomp.bounce; this.game.audio.sfx('bounce', { x: this.x }); this.game.vfx.ring(this.x, this.y + this.hh, 0xffffff, 0.6); this.takeHit({ damage: ENEMY.stomp.damage, element: 'none', dirX: h.facing, knock: 1.5, stun: ENEMY.stomp.stun, source: h }); } // ───────── golpes ───────── takeHit(hit: Hit): boolean { if (this.defeated || this.dyingT >= 0) return false; const g = this.game; const el = hit.element; const dir = (hit.dirX || (hit.source ? (this.x < hit.source.x ? -1 : 1) : -this.facing)) as number; this.lastHitBy = el; // ── congelado: el hielo absorbe; un golpe fuerte lo rompe ── if (this.frozenT > 0) { if (el === 'ice') { this.frozenT = Math.max(this.frozenT, hit.stun ?? ENEMY.ice.time); return true; } if (el === 'fire') { this.thaw(false); this.damage(hit.damage + ENEMY.fire.bonus, dir, '#ff8a2a'); this.burnT = ENEMY.fire.burnTime; return true; } if (el === 'cool' && hit.damage <= 0) return false; const strong = hit.damage >= ENEMY.ice.strongDamage || el === 'fury'; this.iceCracks++; if (strong || this.iceCracks >= ENEMY.ice.cracksToBreak) { this.thaw(true); this.damage(hit.damage + ENEMY.ice.shatterBonus, dir, '#bfeaff'); if (!this.defeated) this.knockback(dir, hit.knock ?? ENEMY.hurt.knock, ENEMY.hurt.minTime); } else { g.audio.sfx('freeze', { pitch: 1.6, volume: 0.6, x: this.x }); g.vfx.burst('ice', this.x + dir * this.hw, this.y, { count: 5, dirX: dir, scale: 0.8 }); g.vfx.floatText('¡Crac!', this.x, this.y + this.hh + 0.4, '#bfeaff'); this.frozenT = Math.max(0.6, this.frozenT * 0.6); } return true; } switch (el) { case 'ice': { if (this.freezable) { if (hit.damage > 0) this.damage(hit.damage, dir, '#bfeaff'); if (!this.defeated) this.freeze(hit.stun ?? ENEMY.ice.time); return true; } break; } case 'cool': { this.daze(hit.stun ?? ENEMY.cool.defaultStun); if (hit.damage > 0) this.damage(hit.damage, dir); return true; } case 'water': { const b = this.body; const boss = this.tags.has('boss'); b.vx = this.flying && !boss ? dir * ENEMY.water.flyPush : dir * (hit.knock && hit.knock > ENEMY.water.push ? hit.knock : ENEMY.water.push); if (!this.flying) b.vy = Math.max(b.vy, ENEMY.water.pushY); this.hurtT = Math.max(this.hurtT, ENEMY.water.time); this.waterT = ENEMY.water.time; g.vfx.burst('splash', this.x, this.y, { count: 6, dirX: dir, scale: 0.8 }); // la pistola de agua daña a todos menos a los jefes; a los voladores, con cada chorro const need = this.waterWeak || this.flying ? ENEMY.water.flyShots : ENEMY.water.groundShots; if (hit.damage > 0 && (this.waterWeak || !boss) && ++this.waterHits >= need) { this.waterHits = 0; this.damage(hit.damage, dir, '#7fd0ff'); } else this.rig?.flash(0x7fd0ff, 0.12); this.onWater(dir); return true; } case 'fire': { if (this.burnable) { this.burnT = ENEMY.fire.burnTime; g.vfx.burst('fire', this.x, this.y, { count: 8, scale: 0.9 }); g.audio.sfx('fire', { x: this.x, volume: 0.6 }); this.damage(hit.damage + ENEMY.fire.bonus, dir, '#ff8a2a'); if (!this.defeated) this.knockback(dir, hit.knock ?? ENEMY.hurt.knock, hit.stun ?? ENEMY.hurt.minTime); return true; } break; } } if (hit.damage <= 0) return false; this.damage(hit.damage, dir, el === 'fury' ? '#ff6a4a' : '#ffffff'); if (!this.defeated) this.knockback(dir, hit.knock ?? ENEMY.hurt.knock, hit.stun ?? ENEMY.hurt.minTime); return true; } /** Gancho: el agua le alcanzó. */ protected onWater(_dir: number): void {} /** Resta vida con indicador; muere si llega a 0. */ protected damage(n: number, dir: number, color = '#ffffff'): void { if (n <= 0 || this.defeated) return; const g = this.game; this.hp -= n; g.vfx.floatText(`-${n}`, this.x + dir * 0.2, this.y + this.hh + 0.35, color); g.audio.sfx('enemy_hit', { x: this.x }); g.hitstop(ENEMY.hurt.hitstop); this.rig?.flash(0xffffff, ENEMY.hurt.flash); this.rig?.squash(-0.3); this.onHurt(dir); if (this.hp <= 0) this.die(this.lastHitBy); } protected onHurt(_dir: number): void { this.rig?.play('hurt'); } /** Empujón con aturdimiento corto. */ knockback(dir: number, knock: number, time: number): void { const b = this.body; b.vx = dir * knock; if (!this.flying || this.stunFalls()) b.vy = Math.max(b.vy, ENEMY.hurt.knockY * Math.min(1, knock / ENEMY.hurt.knock)); this.hurtT = Math.max(this.hurtT, Math.max(ENEMY.hurt.minTime, time)); } /** Mareo (COOL): quieto con estrellitas. */ daze(seconds: number): void { this.stunT = Math.max(this.stunT, seconds); this.body.vx *= 0.2; this.rig?.setExpression('hurt'); this.game.vfx.burst('sparkle', this.x, this.y + this.hh + 0.3, { count: 6, color: 0x9fe8ff }); this.showDizzy(true); this.onDaze(); } protected onDaze(): void {} // ───────── hielo ───────── freeze(seconds: number): void { const g = this.game; this.frozenT = seconds; this.iceCracks = 0; this.hurtT = 0; this.stunT = 0; this.burnT = 0; this.body.vx = this.body.vy = 0; this.showDizzy(false); g.audio.sfx('freeze', { x: this.x }); g.vfx.burst('freeze', this.x, this.y, { scale: Math.max(this.hw, this.hh) * 2 }); this.rig?.play('frozen', { loop: true }); this.rig?.setExpression('cold', 0); this.rig?.setColor(this.frozenTint()); const { geo, mat, shine } = iceResources(); const grp = new THREE.Group(); const m = new THREE.Mesh(geo, mat); const w = (this.hw + ENEMY.ice.pad) * 2, h = (this.hh + ENEMY.ice.pad) * 2; m.scale.set(w, h, Math.max(w, 1.1)); m.renderOrder = 2; grp.add(m); const s1 = new THREE.Mesh(sharedShineGeo(), shine); s1.position.set(-w * 0.28, h * 0.12, Math.max(w, 1.1) * 0.5 + 0.01); s1.rotation.z = -0.5; s1.scale.setScalar(h * 0.8); const s2 = s1.clone(); s2.position.x += 0.12; s2.position.y -= h * 0.12; s2.scale.setScalar(h * 0.35); grp.add(s1, s2); grp.scale.setScalar(0.2); this.iceGrow = 0; this.view.add(grp); this.iceMesh = grp; this.onFreeze(); } protected onFreeze(): void {} protected onThaw(): void {} /** Color que toma el cuerpo congelado. */ protected frozenTint(): THREE.ColorRepresentation { return 0x9fd8ff; } /** Color normal (para devolverlo tras congelar). Las subclases con chibi lo fijan. */ protected baseColor: THREE.ColorRepresentation = 0xffffff; private tryEnableIce(): void { if (this.iceCol || this.defeated) return; const h = this.game.hero; const w = this.hw + ENEMY.ice.pad, hh = this.hh + ENEMY.ice.pad; if (h) { const hb = h.body; if (Math.abs(hb.x - this.body.x) < hb.hw + w && Math.abs(hb.y - this.body.y) < hb.hh + hh) return; } this.iceCol = this.game.physics.box(this.body.x, this.body.y, w * 2, hh * 2, { material: 'ice', owner: this, tag: 'ice' }); } private removeIce(): void { if (this.iceCol) { this.game.physics.remove(this.iceCol); this.iceCol = null; } if (this.iceMesh) { this.view.remove(this.iceMesh); this.iceMesh = null; } } /** Sale del hielo; `shatter` = roto por un golpe. */ thaw(shatter: boolean): void { const g = this.game; this.frozenT = 0; this.removeIce(); if (shatter) { g.audio.sfx('shatter', { x: this.x }); g.vfx.burst('shatter', this.x, this.y, { scale: Math.max(this.hw, this.hh) * 2, color: 0xbfeaff }); g.gfx.cam.shake(0.25, 0.15); } else { g.vfx.burst('ice', this.x, this.y, { count: 8 }); g.audio.sfx('splash', { volume: 0.3, pitch: 1.5, x: this.x }); } this.rig?.setColor(this.baseColor); this.rig?.play('idle'); this.restoreExpression(); this.onThaw(); } /** Expresión normal del enemigo. */ protected restoreExpression(): void { this.rig?.setExpression('angry'); } // ───────── mareo ───────── private showDizzy(on: boolean): void { if (on && !this.dizzy) { const grp = new THREE.Group(); for (let i = 0; i < DIZZY_STARS; i++) { const s = emojiSprite('⭐', 0.32, { glow: 0.3 }); grp.add(s); } this.view.add(grp); this.dizzy = grp; } else if (!on && this.dizzy) { this.view.remove(this.dizzy); for (const c of this.dizzy.children) ((c as THREE.Sprite).material as THREE.Material).dispose(); this.dizzy = null; } } // ───────── muerte ───────── die(by: Element = 'none'): void { if (this.defeated) return; this.defeated = true; this.removeIce(); this.showDizzy(false); this.dyingT = ENEMY.death.time; this.body.vx = 0; this.body.vy = 0; this.lastHitBy = by; this.rig?.setExpression('hurt', 0); this.rig?.play('hurt'); this.rig?.squash(0.4); this.game.audio.sfx('enemy_die', { x: this.x }); } private finishDeath(): void { const g = this.game; g.vfx.burst('poof', this.x, this.y, { scale: Math.max(1, this.hw * 2.4) }); g.vfx.burst('star', this.x, this.y + 0.2, { count: 6 }); g.events.emit('entity:killed', { type: this.type, x: this.x, y: this.y, by: this.lastHitBy }); this.dropLoot(); this.onDeath(); this.kill(); } protected onDeath(): void {} protected dropLoot(): void { if (this.drop === 'none') return; const g = this.game; const st = g.state.data as { hp?: number; maxHp?: number }; const hurt = (st.hp ?? 0) < (st.maxHp ?? 0); // Los enemigos ya no sueltan monedas (el tesoro está repartido por el mundo y se cuenta por zonas): // solo, a veces, un corazón si el héroe está herido. if (!hurt || Math.random() >= ENEMY.death.heartChance) return; const y = this.feetY() + 0.3; if (g.hasType('item.heart')) g.spawn('item.heart', this.x, y, { pop: true }); } /** Y de los pies (o del suelo bajo él si vuela). */ feetY(): number { if (this.flying) { const gy = this.game.physics.groundBelow(this.x, this.y, 8); if (gy !== null) return gy; } return this.body.y - this.hh; } // ───────── render ───────── render(alpha: number, dt: number): void { super.render(alpha, dt); const rx = this.view.position.x, ry = this.view.position.y; this.animate(dt); if (this.dyingT >= 0) { const u = 1 - Math.max(0, this.dyingT) / ENEMY.death.time; const s = 1 + 0.35 * Math.sin(u * Math.PI) - 0.6 * u * u; this.visual.scale.setScalar(Math.max(0.05, s)); } if (this.iceMesh) { this.iceGrow = Math.min(1, this.iceGrow + dt * 7); const s = 0.2 + 0.8 * this.iceGrow + 0.08 * Math.sin(this.iceGrow * Math.PI); const wob = this.frozenT < 0.8 ? 0.04 * Math.sin(this.t * 40) : 0; this.iceMesh.scale.set(s + wob, s - wob, s); } if (this.dizzy) { if (this.stunT <= 0 || this.defeated) this.showDizzy(false); else { const n = this.dizzy.children.length; const top = this.hh + this.dizzyHeight; for (let i = 0; i < n; i++) { const a = this.t * 5 + (i / n) * Math.PI * 2; this.dizzy.children[i].position.set(Math.cos(a) * 0.45, top + Math.sin(a * 2) * 0.05, Math.sin(a) * 0.3); } } } if (this.shadow) { if (this.groundY === null || (this.t * 60) % 6 < 1) this.groundY = this.game.physics.groundBelow(rx, ry, 12); if (this.groundY === null) this.shadow.visible = false; else { const h = ry - this.groundY; this.shadow.visible = true; this.shadow.position.set(0, this.groundY - ry + 0.03, 0); const k = Math.max(0.25, 1 - Math.max(0, h - this.hh) * 0.12); this.shadow.scale.setScalar(k); } } } /** Animación propia (rig.update, alas, ojos…). Por defecto alimenta el chibi con el movimiento. */ protected animate(dt: number): void { const b = this.body; this.rig?.update(dt, { vx: b.vx, vy: b.vy, onGround: this.flying ? true : b.onGround, facing: this.facing }); } } // ───────────────────────────── utilidades ───────────────────────────── /** ¿Hay suelo delante (para girar en bordes)? */ export function groundAhead(e: Enemy, dir: number, look = 0.15, drop = 0.7): boolean { const b = e.body; const x = b.x + dir * (b.hw + look); const feet = b.y - b.hh; const gy = e.game.physics.groundBelow(x, feet + 0.2, drop + 0.2); return gy !== null && gy > feet - drop; } /** ¿Pared delante? */ export function wallAhead(e: Enemy, dir: number, look = 0.12): boolean { const b = e.body; if (b.onWall === dir) return true; const x = b.x + dir * (b.hw + look); return e.game.physics.solidAt(x, b.y) || e.game.physics.solidAt(x, b.y - b.hh * 0.6); } export function clampPatrol(e: Enemy, patrol: [number, number] | undefined, dir: number): boolean { if (!patrol) return false; const x = e.body.x; return (dir < 0 && x <= Math.min(patrol[0], patrol[1])) || (dir > 0 && x >= Math.max(patrol[0], patrol[1])); } export const approach = (v: number, target: number, step: number): number => v < target ? Math.min(target, v + step) : Math.max(target, v - step);