54 lines
1.8 KiB
C#
54 lines
1.8 KiB
C#
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<AIComponent>();
|
|
foreach (var entity in entities)
|
|
{
|
|
if (world.GetComponent<PlayerComponent>(entity) != null)
|
|
{
|
|
world.Logger.Warn("AI entity has PlayerComponent; skipping AI aim processing.");
|
|
continue;
|
|
}
|
|
|
|
var ai = world.GetComponent<AIComponent>(entity);
|
|
var input = world.GetComponent<InputStateComponent>(entity);
|
|
var pos = world.GetComponent<PositionComponent>(entity);
|
|
var weapon = world.GetComponent<WeaponComponent>(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;
|
|
}
|
|
}
|
|
} |