Refactored Ghost Entities

Game Context was creating an empty OrbitalEntity for every planet; this was an ugly hack so I refactored it, as well as refactoring some object references to GUIDs as they should be
This commit is contained in:
hyperion51 2020-05-04 00:07:36 +01:00
commit 4f29bf24d0
13 changed files with 199 additions and 191 deletions

View file

@ -8,16 +8,19 @@ using static Unity.Mathematics.math;
public class VelocityMatch : AgentBehavior
{
public event Action OnMatch;
public Entity Objective;
public Guid TargetOrbit;
private Entity _entity;
private IAnalogBehavior _thrust;
private IAnalogBehavior _turning;
private const float TurningSensitivity = 1;
private const float ThrustSensitivity = 1;
private const float TargetThreshold = .1f;
public GameContext Context { get; }
public VelocityMatch(Entity entity)
public VelocityMatch(GameContext context, Entity entity)
{
Context = context;
_entity = entity;
_thrust = entity.Axes.Keys.FirstOrDefault(x => x is Thruster);
_turning = entity.Axes.Keys.FirstOrDefault(x => x is Turning);
@ -25,9 +28,9 @@ public class VelocityMatch : AgentBehavior
public override void Update(float delta)
{
if (Objective != null && _thrust != null && _turning != null)
if (TargetOrbit != Guid.Empty && _thrust != null && _turning != null)
{
var deltaV = Objective.Velocity - _entity.Velocity;
var deltaV = Context.GetOrbitVelocity(TargetOrbit) - _entity.Velocity;
if(length(deltaV) < TargetThreshold)
OnMatch?.Invoke();
var angleDiff = _entity.Direction.AngleDiff(deltaV);
@ -41,7 +44,7 @@ public class VelocityMatch : AgentBehavior
get
{
var velocity = length(_entity.Velocity);
var deltaV = Objective.Velocity - _entity.Velocity;
var deltaV = Context.GetOrbitVelocity(TargetOrbit) - _entity.Velocity;
var stoppingTime = length(deltaV) / (_entity.Context.Evaluate((_thrust.Data as ThrusterData).Thrust, _thrust.Item, _entity) / _entity.Mass);
var stoppingDistance = stoppingTime * (velocity / 2);