/
RaskolnickOFF
/
3D
Обзор
Документация
Войти
/
RaskolnickOFF
/
3D
Код
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
js/source/systems/enemy/PerceptionSystem.js
66 строк
2 KB
RaskolnickOFF
Structure update
06 июн 2026, 03:18
06 июн 2026, 03:18
9bc1c25
Код
Авторство
О чём код?
// js/source/systems/enemy/PerceptionSystem.js — зрение/запах/слух врагов (симуляция) import { Components } from '../../core/Components.js'; export class PerceptionSystem { /** * @param {import('../core/SimulationContext.js').SimulationContext} ctx */ constructor(ctx) { this.visionRadius = ctx.settings.enemy.vision.radius; this.visionAngle = ctx.settings.enemy.vision.angle; this.smellRadius = ctx.settings.enemy.smell.radius; this.hearingRadius = ctx.settings.enemy.hearing.radius; this.hearingThreshold = ctx.settings.player.noise.run; } update(engine) { let player = null; for (const entity of engine.with(Components.PLAYER)) { player = entity; break; } if (!player) return; const playerHP = player.get(Components.HEALTH); if (playerHP <= 0) return; const playerTransform = player.get(Components.TRANSFORM); if (!playerTransform) return; const playerNoise = player.get(Components.NOISE) || 0; const enemies = engine.with(Components.ENEMY); for (const enemy of enemies) { const transform = enemy.get(Components.TRANSFORM); const dx = playerTransform.x - transform.x; const dz = playerTransform.z - transform.z; const dist = Math.sqrt(dx * dx + dz * dz); const canSee = dist <= this.visionRadius && this._isInCone(transform, playerTransform, this.visionAngle); const canSmell = dist <= this.smellRadius; const canHear = playerNoise >= this.hearingThreshold && dist <= this.hearingRadius; enemy.add(Components.CAN_SEE, canSee); enemy.add(Components.CAN_SMELL, canSmell); enemy.add(Components.CAN_HEAR, canHear); if (canSee || canSmell || canHear) { enemy.add(Components.KNOWN_POSITION, { x: playerTransform.x, z: playerTransform.z }); } } } _isInCone(transform, targetTransform, angle) { const dx = targetTransform.x - transform.x; const dz = targetTransform.z - transform.z; const dist = Math.sqrt(dx * dx + dz * dz); if (dist < 0.01) return true; const forwardX = Math.sin(transform.rotY); const forwardZ = Math.cos(transform.rotY); const dot = (dx * forwardX + dz * forwardZ) / dist; const halfAngle = (angle / 2) * (Math.PI / 180); return dot > Math.cos(halfAngle); } }