using GameCore.Combat; using GameCore.ECS; using GameCore.ECS.Interfaces; using GameCore.Input; using GameCore.Physics; using GameCore.Player; namespace GameCore.AI; public class AIAimSystem : ISystem { private const float MinAimDistance = 0.5f; public void Update(World world, float deltaTime) { var entities = world.GetEntitiesWith(); foreach (var entity in entities) { if (world.GetComponent(entity) != null) { world.Logger.Warn("AI entity has PlayerComponent; skipping AI aim processing."); continue; } var ai = world.GetComponent(entity); var input = world.GetComponent(entity); var pos = world.GetComponent(entity); var weapon = world.GetComponent(entity); if (ai == null || input == null || pos == null || weapon == null) { if (input != null) input.IsFiring = false; continue; } input.IsFiring = false; if (ai.CurrentState == AIState.Chase || ai.CurrentState == AIState.Attack) { var vectorToTarget = ai.LastKnownTargetPosition - input.MuzzlePosition; var distanceToTarget = vectorToTarget.Length(); if (distanceToTarget > MinAimDistance) { var directionToTarget = vectorToTarget.Normalize(); input.MuzzleDirection = directionToTarget; input.LookDirection = directionToTarget; } } if (ai.CurrentState == AIState.Attack) input.IsFiring = true; } } }