/
RaskolnickOFF
/
3D
Обзор
Документация
Войти
/
RaskolnickOFF
/
3D
Код
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
js/source/systems/enemy/EnemyRotationSystem.js
79 строк
3 KB
RaskolnickOFF
Structure update
06 июн 2026, 03:18
06 июн 2026, 03:18
9bc1c25
Код
Авторство
О чём код?
// js/source/systems/enemy/EnemyRotationSystem.js — повороты врагов в зависимости от AI_STATE import { Components } from '../../core/Components.js'; export class EnemyRotationSystem { constructor() { this._lastKnownAngle = new Map(); } update(engine, dt) { const enemies = engine.with(Components.ENEMY); for (const enemy of enemies) { const transform = enemy.get(Components.TRANSFORM); if (!transform) continue; const state = enemy.get(Components.AI_STATE) || 'idle'; const desiredVel = enemy.get(Components.DESIRED_VELOCITY); let desiredRotY = null; switch (state) { case 'chase': case 'combat': { for (const e of engine.with(Components.PLAYER)) { const pt = e.get(Components.TRANSFORM); if (pt) { const tdx = pt.x - transform.x; const tdz = pt.z - transform.z; const targetAngle = Math.atan2(tdx, tdz); // Кратчайший поворот к цели let diff = targetAngle - transform.rotY; while (diff > Math.PI) diff -= Math.PI * 2; while (diff < -Math.PI) diff += Math.PI * 2; desiredRotY = transform.rotY + diff; break; } } break; } case 'search': case 'return': { if (desiredVel && (desiredVel.x !== 0 || desiredVel.z !== 0)) { desiredRotY = Math.atan2(desiredVel.x, desiredVel.z); } break; } case 'patrol': { const patrolDir = enemy.get(Components.PATROL_DIRECTION); if (patrolDir) { desiredRotY = Math.atan2(patrolDir.x, patrolDir.z); } break; } case 'idle': { const t = performance.now() / 1000; const swing = Math.sin(t * 0.7) * (Math.PI / 6); const baseAngle = this._lastKnownAngle.get(enemy.id) ?? transform.rotY; desiredRotY = baseAngle + swing; this._lastKnownAngle.set(enemy.id, baseAngle); break; } } if (desiredRotY !== null) { const current = transform.rotY; let diff = desiredRotY - current; while (diff > Math.PI) diff -= Math.PI * 2; while (diff < -Math.PI) diff += Math.PI * 2; const speed = (state === 'combat' || state === 'chase') ? 24 : 8; const newRotY = current + diff * Math.min(dt * speed, 1); enemy.add(Components.DESIRED_ROTATION, { rotY: newRotY }); } } } }