2020-04-23 03:31:17 +01:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using static Unity.Mathematics.math;
|
|
|
|
|
|
using Random = Unity.Mathematics.Random;
|
|
|
|
|
|
|
|
|
|
|
|
public class AgentController
|
|
|
|
|
|
{
|
|
|
|
|
|
public Entity Entity { get; }
|
|
|
|
|
|
public EntityAgent EntityAgent { get; }
|
|
|
|
|
|
public GameContext Context { get; }
|
|
|
|
|
|
public ZoneData Zone { get; set; }
|
|
|
|
|
|
|
|
|
|
|
|
private Locomotion _locomotion;
|
2020-04-25 00:01:13 +01:00
|
|
|
|
private VelocityMatch _velocityMatch;
|
2020-04-23 03:31:17 +01:00
|
|
|
|
private Random _random = new Random((uint) (DateTime.Now.Ticks%uint.MaxValue));
|
2020-04-25 00:01:13 +01:00
|
|
|
|
private float _stoppingTime;
|
|
|
|
|
|
private float _stoppingDistance;
|
|
|
|
|
|
private float _turnaroundDistance;
|
|
|
|
|
|
private float _turnaroundTime;
|
|
|
|
|
|
private IAnalogBehavior _thrust;
|
|
|
|
|
|
private IAnalogBehavior _turning;
|
|
|
|
|
|
private KeyValuePair<DatabaseEntry, Entity> _objective;
|
|
|
|
|
|
private float _velocityLimit;
|
|
|
|
|
|
|
2020-04-23 03:31:17 +01:00
|
|
|
|
public AgentController(GameContext context, ZoneData zone, Entity entity)
|
|
|
|
|
|
{
|
|
|
|
|
|
Entity = entity;
|
|
|
|
|
|
Zone = zone;
|
|
|
|
|
|
Context = context;
|
|
|
|
|
|
EntityAgent = new EntityAgent(context, zone, entity);
|
|
|
|
|
|
_locomotion = new Locomotion(entity);
|
2020-04-25 00:01:13 +01:00
|
|
|
|
_velocityMatch = new VelocityMatch(entity);
|
|
|
|
|
|
_velocityMatch.OnMatch += () =>
|
|
|
|
|
|
{
|
|
|
|
|
|
EntityAgent.CurrentBehavior = _locomotion;
|
|
|
|
|
|
RandomTarget();
|
|
|
|
|
|
};
|
2020-04-23 03:31:17 +01:00
|
|
|
|
EntityAgent.CurrentBehavior = _locomotion;
|
|
|
|
|
|
RandomTarget();
|
2020-04-25 00:01:13 +01:00
|
|
|
|
_thrust = entity.Axes.Keys.FirstOrDefault(x => x is Thruster);
|
|
|
|
|
|
_turning = entity.Axes.Keys.FirstOrDefault(x => x is Turning);
|
|
|
|
|
|
var velocityLimitData = entity.GetBehaviorData<VelocityLimitData>().FirstOrDefault();
|
|
|
|
|
|
_velocityLimit = Context.Evaluate(velocityLimitData.TopSpeed, entity.Hull, entity);
|
|
|
|
|
|
_stoppingTime = _velocityLimit / Context.Evaluate((_thrust.Data as ThrusterData).Thrust, _thrust.Item, Entity) * Entity.Mass;
|
|
|
|
|
|
_stoppingDistance = _stoppingTime * _velocityLimit / 2;
|
|
|
|
|
|
_turnaroundTime = PI / Context.Evaluate((_turning.Data as TurningData).Torque, _turning.Item, entity) * entity.Mass;
|
|
|
|
|
|
_turnaroundDistance = _turnaroundTime * _velocityLimit;
|
|
|
|
|
|
_locomotion.LeadTime = _stoppingTime;
|
2020-04-23 03:31:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void Update(float delta)
|
|
|
|
|
|
{
|
|
|
|
|
|
EntityAgent.Update(delta);
|
|
|
|
|
|
var distance = length(_locomotion.Objective.Position - Entity.Position);
|
2020-04-25 00:01:13 +01:00
|
|
|
|
Context.Log(
|
|
|
|
|
|
$"Agent Mode: {EntityAgent.CurrentBehavior.GetType()} " +
|
|
|
|
|
|
$"Agent Distance: {distance} " +
|
|
|
|
|
|
$"Turnaround Distance: {_stoppingDistance + _turnaroundDistance} " +
|
|
|
|
|
|
$"Objective: {_objective.Key.ID.ToString().Substring(0, 8)}");
|
|
|
|
|
|
|
|
|
|
|
|
if (EntityAgent.CurrentBehavior == _locomotion)
|
|
|
|
|
|
{
|
|
|
|
|
|
var velocity = length(Entity.Velocity);
|
|
|
|
|
|
_stoppingTime = velocity / Context.Evaluate((_thrust.Data as ThrusterData).Thrust, _thrust.Item, Entity) * Entity.Mass;
|
|
|
|
|
|
_stoppingDistance = _stoppingTime * velocity / 2;
|
|
|
|
|
|
if (distance < _stoppingDistance + _turnaroundDistance)
|
|
|
|
|
|
EntityAgent.CurrentBehavior = _velocityMatch;
|
|
|
|
|
|
_velocityMatch.Objective = _locomotion.Objective;
|
|
|
|
|
|
}
|
2020-04-23 03:31:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void RandomTarget()
|
|
|
|
|
|
{
|
2020-04-25 00:01:13 +01:00
|
|
|
|
var entities = Context.ZoneContents[Zone].ToArray();
|
|
|
|
|
|
_objective = entities[_random.NextInt(entities.Length)];
|
|
|
|
|
|
_locomotion.Objective = _objective.Value;
|
2020-04-23 03:31:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|