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

@ -10,21 +10,23 @@ public class AgentController
public Entity Entity { get; }
public EntityAgent EntityAgent { get; }
public GameContext Context { get; }
public ZoneData Zone { get; set; }
public Guid Zone { get; set; }
public Guid HomeZone;
private Locomotion _locomotion;
private VelocityMatch _velocityMatch;
private Random _random = new Random((uint) (DateTime.Now.Ticks%uint.MaxValue));
private KeyValuePair<DatabaseEntry, Entity> _objective;
private Guid _targetOrbit;
public AgentController(GameContext context, ZoneData zone, Entity entity)
public AgentController(GameContext context, Guid zone, Entity entity)
{
Entity = entity;
Zone = zone;
Context = context;
EntityAgent = new EntityAgent(context, zone, entity);
_locomotion = new Locomotion(entity);
_velocityMatch = new VelocityMatch(entity);
_velocityMatch = new VelocityMatch(context, entity);
_velocityMatch.OnMatch += () =>
{
EntityAgent.CurrentBehavior = _locomotion;
@ -37,7 +39,6 @@ public class AgentController
public void Update(float delta)
{
EntityAgent.Update(delta);
var distance = length(_locomotion.Objective.Position - Entity.Position);
// Context.Log(
// $"Agent Mode: {EntityAgent.CurrentBehavior.GetType()} " +
// $"Agent Distance: {distance} " +
@ -46,9 +47,11 @@ public class AgentController
if (EntityAgent.CurrentBehavior == _locomotion)
{
_velocityMatch.Objective = _locomotion.Objective;
var orbitData = Context.Cache.Get<OrbitData>(_targetOrbit);
_velocityMatch.TargetOrbit = _targetOrbit;
var matchDistanceTime = _velocityMatch.MatchDistanceTime;
_locomotion.LeadTime = matchDistanceTime.y;
_locomotion.Objective = Context.GetOrbitPosition(_targetOrbit) + Context.GetOrbitVelocity(_targetOrbit) * matchDistanceTime.y;
var distance = length(_locomotion.Objective - Entity.Position);
if (distance < matchDistanceTime.x)
EntityAgent.CurrentBehavior = _velocityMatch;
}
@ -56,8 +59,7 @@ public class AgentController
private void RandomTarget()
{
var entities = Context.ZoneContents[Zone].Where(kvp=>!(kvp.Key is ShipData)).ToArray();
_objective = entities[_random.NextInt(entities.Length)];
_locomotion.Objective = _objective.Value;
var entities = Context.ZonePlanets[Zone];
_targetOrbit = Context.Cache.Get<PlanetData>(entities[_random.NextInt(entities.Length)]).Orbit;
}
}

View file

@ -1,4 +1,5 @@
using System.Collections;
using System;
using System.Collections;
using System.Collections.Generic;
public class EntityAgent
@ -7,9 +8,9 @@ public class EntityAgent
public Entity Entity { get; }
public GameContext Context { get; }
public ZoneData Zone { get; set; }
public Guid Zone { get; set; }
public EntityAgent(GameContext context, ZoneData zone, Entity entity)
public EntityAgent(GameContext context, Guid zone, Entity entity)
{
Entity = entity;
Zone = zone;

View file

@ -6,8 +6,7 @@ using static Unity.Mathematics.math;
public class Locomotion : AgentBehavior
{
public Entity Objective;
public float LeadTime;
public float2 Objective;
private Entity _entity;
private IAnalogBehavior _thrust;
private IAnalogBehavior _turning;
@ -23,9 +22,9 @@ public class Locomotion : AgentBehavior
public override void Update(float delta)
{
if (Objective != null && _thrust != null && _turning != null)
if (_thrust != null && _turning != null)
{
float2 diff = Objective.Position + Objective.Velocity * LeadTime - _entity.Position;
float2 diff = Objective - _entity.Position;
float2 tangential = _entity.Velocity - dot(normalize(diff), normalize(_entity.Velocity)) * _entity.Velocity;
float angleDiff = _entity.Direction.AngleDiff(normalize(diff) - tangential * TangentSensitivity);
_entity.Axes[_turning] = clamp(-angleDiff * Sensitivity, -1, 1);

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);