Entity Persistence Refactoring
Entities refactored to enable robust persistence Entities detect stale data references and reload behaviors
This commit is contained in:
parent
87acc75b96
commit
6aeb2ae280
29 changed files with 299 additions and 202 deletions
|
|
@ -7,25 +7,23 @@ using Random = Unity.Mathematics.Random;
|
|||
|
||||
public class AgentController
|
||||
{
|
||||
public Entity Entity { get; }
|
||||
public Guid Entity { get; }
|
||||
public EntityAgent EntityAgent { get; }
|
||||
public GameContext Context { get; }
|
||||
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 Guid _targetOrbit;
|
||||
|
||||
public AgentController(GameContext context, Guid zone, Entity entity)
|
||||
public AgentController(GameContext context, Guid zone, Guid entity)
|
||||
{
|
||||
Entity = entity;
|
||||
Zone = zone;
|
||||
Context = context;
|
||||
EntityAgent = new EntityAgent(context, zone, entity);
|
||||
_locomotion = new Locomotion(entity);
|
||||
EntityAgent = new EntityAgent(context, entity, zone, Guid.Empty);
|
||||
_locomotion = new Locomotion(context, entity);
|
||||
_velocityMatch = new VelocityMatch(context, entity);
|
||||
_velocityMatch.OnMatch += () =>
|
||||
{
|
||||
|
|
@ -44,6 +42,7 @@ public class AgentController
|
|||
// $"Agent Distance: {distance} " +
|
||||
// $"Turnaround Distance: {_stoppingDistance + _turnaroundDistance} " +
|
||||
// $"Objective: {_objective.Key.ID.ToString().Substring(0, 8)}");
|
||||
var entity = Context.Cache.Get<Entity>(Entity);
|
||||
|
||||
if (EntityAgent.CurrentBehavior == _locomotion)
|
||||
{
|
||||
|
|
@ -51,7 +50,7 @@ public class AgentController
|
|||
_velocityMatch.TargetOrbit = _targetOrbit;
|
||||
var matchDistanceTime = _velocityMatch.MatchDistanceTime;
|
||||
_locomotion.Objective = Context.GetOrbitPosition(_targetOrbit) + Context.GetOrbitVelocity(_targetOrbit) * matchDistanceTime.y;
|
||||
var distance = length(_locomotion.Objective - Entity.Position);
|
||||
var distance = length(_locomotion.Objective - entity.Position);
|
||||
if (distance < matchDistanceTime.x)
|
||||
EntityAgent.CurrentBehavior = _velocityMatch;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,13 +1,19 @@
|
|||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using JsonKnownTypes;
|
||||
using MessagePack;
|
||||
using Newtonsoft.Json;
|
||||
using UnityEngine;
|
||||
|
||||
[MessagePackObject,
|
||||
Union(0, typeof(StationTowing)),
|
||||
JsonObject(MemberSerialization.OptIn), JsonConverter(typeof(JsonKnownTypesConverter<DatabaseEntry>))]
|
||||
public abstract class AgentTask
|
||||
{
|
||||
public abstract AgentJob JobType { get; }
|
||||
public int Priority;
|
||||
public Guid Zone;
|
||||
[IgnoreMember] public abstract AgentJob JobType { get; }
|
||||
[JsonProperty("id"), Key(0)] public int Priority;
|
||||
[JsonProperty("zone"), Key(1)] public Guid Zone;
|
||||
}
|
||||
|
||||
public enum AgentJob
|
||||
|
|
|
|||
|
|
@ -1,24 +1,29 @@
|
|||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using JsonKnownTypes;
|
||||
using MessagePack;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
[MessagePackObject,
|
||||
JsonObject(MemberSerialization.OptIn), JsonConverter(typeof(JsonKnownTypesConverter<Entity>))]
|
||||
public class EntityAgent
|
||||
{
|
||||
public AgentBehavior CurrentBehavior;
|
||||
[IgnoreMember] public AgentBehavior CurrentBehavior;
|
||||
[IgnoreMember] public Guid Entity;
|
||||
[IgnoreMember] public GameContext Context;
|
||||
[JsonProperty("homeZone"), Key(0)] public Guid HomeZone;
|
||||
[JsonProperty("homeColony"), Key(0)] public Guid HomeColony;
|
||||
|
||||
public Entity Entity { get; }
|
||||
public GameContext Context { get; }
|
||||
public Guid Zone { get; set; }
|
||||
|
||||
public EntityAgent(GameContext context, Guid zone, Entity entity)
|
||||
public EntityAgent(GameContext context, Guid entity, Guid homeZone, Guid homeColony)
|
||||
{
|
||||
Entity = entity;
|
||||
Zone = zone;
|
||||
HomeZone = homeZone;
|
||||
Context = context;
|
||||
}
|
||||
|
||||
public void Update(float delta)
|
||||
{
|
||||
CurrentBehavior.Update(delta);
|
||||
CurrentBehavior?.Update(delta);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
using System.Collections;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Unity.Mathematics;
|
||||
|
|
@ -7,28 +8,33 @@ using static Unity.Mathematics.math;
|
|||
public class Locomotion : AgentBehavior
|
||||
{
|
||||
public float2 Objective;
|
||||
private Entity _entity;
|
||||
private IAnalogBehavior _thrust;
|
||||
private IAnalogBehavior _turning;
|
||||
private const float Sensitivity = 1;
|
||||
private const float TangentSensitivity = 2;
|
||||
|
||||
private GameContext _context;
|
||||
private Guid _entity;
|
||||
private IAnalogBehavior _thrust;
|
||||
private IAnalogBehavior _turning;
|
||||
|
||||
public Locomotion(Entity entity)
|
||||
public Locomotion(GameContext context, Guid entity)
|
||||
{
|
||||
_context = context;
|
||||
_entity = entity;
|
||||
_thrust = entity.Axes.Keys.FirstOrDefault(x => x is Thruster);
|
||||
_turning = entity.Axes.Keys.FirstOrDefault(x => x is Turning);
|
||||
var entityObject = _context.Cache.Get<Entity>(_entity);
|
||||
_thrust = entityObject.Axes.Keys.FirstOrDefault(x => x is Thruster);
|
||||
_turning = entityObject.Axes.Keys.FirstOrDefault(x => x is Turning);
|
||||
}
|
||||
|
||||
public override void Update(float delta)
|
||||
{
|
||||
var entity = _context.Cache.Get<Entity>(_entity);
|
||||
if (_thrust != null && _turning != null)
|
||||
{
|
||||
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);
|
||||
_entity.Axes[_thrust] = 1;
|
||||
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);
|
||||
entity.Axes[_thrust] = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,11 +1,16 @@
|
|||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using JsonKnownTypes;
|
||||
using MessagePack;
|
||||
using Newtonsoft.Json;
|
||||
using UnityEngine;
|
||||
|
||||
[MessagePackObject,
|
||||
JsonObject(MemberSerialization.OptIn), JsonConverter(typeof(JsonKnownTypesConverter<DatabaseEntry>))]
|
||||
public class StationTowing : AgentTask
|
||||
{
|
||||
public override AgentJob JobType => AgentJob.Tow;
|
||||
[IgnoreMember] public override AgentJob JobType => AgentJob.Tow;
|
||||
|
||||
private Guid _entity;
|
||||
private Guid _targetOrbit;
|
||||
|
|
|
|||
|
|
@ -9,33 +9,35 @@ public class VelocityMatch : AgentBehavior
|
|||
{
|
||||
public event Action OnMatch;
|
||||
public Guid TargetOrbit;
|
||||
private Entity _entity;
|
||||
private Guid _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(GameContext context, Entity entity)
|
||||
private GameContext _context;
|
||||
|
||||
public VelocityMatch(GameContext context, Guid entity)
|
||||
{
|
||||
Context = context;
|
||||
_context = context;
|
||||
_entity = entity;
|
||||
_thrust = entity.Axes.Keys.FirstOrDefault(x => x is Thruster);
|
||||
_turning = entity.Axes.Keys.FirstOrDefault(x => x is Turning);
|
||||
var entityObject = _context.Cache.Get<Entity>(_entity);
|
||||
_thrust = entityObject.Axes.Keys.FirstOrDefault(x => x is Thruster);
|
||||
_turning = entityObject.Axes.Keys.FirstOrDefault(x => x is Turning);
|
||||
}
|
||||
|
||||
public override void Update(float delta)
|
||||
{
|
||||
var entity = _context.Cache.Get<Entity>(_entity);
|
||||
if (TargetOrbit != Guid.Empty && _thrust != null && _turning != null)
|
||||
{
|
||||
var deltaV = Context.GetOrbitVelocity(TargetOrbit) - _entity.Velocity;
|
||||
var deltaV = _context.GetOrbitVelocity(TargetOrbit) - entity.Velocity;
|
||||
if(length(deltaV) < TargetThreshold)
|
||||
OnMatch?.Invoke();
|
||||
var angleDiff = _entity.Direction.AngleDiff(deltaV);
|
||||
_entity.Axes[_turning] = clamp(-angleDiff * TurningSensitivity, -1, 1);
|
||||
_entity.Axes[_thrust] = saturate(length(pow(saturate(dot(normalize(_entity.Direction),normalize(deltaV))), 8)*length(deltaV))*ThrustSensitivity);
|
||||
var angleDiff = entity.Direction.AngleDiff(deltaV);
|
||||
entity.Axes[_turning] = clamp(-angleDiff * TurningSensitivity, -1, 1);
|
||||
entity.Axes[_thrust] = saturate(length(pow(saturate(dot(normalize(entity.Direction),normalize(deltaV))), 8)*length(deltaV))*ThrustSensitivity);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -43,14 +45,15 @@ public class VelocityMatch : AgentBehavior
|
|||
{
|
||||
get
|
||||
{
|
||||
var velocity = length(_entity.Velocity);
|
||||
var deltaV = Context.GetOrbitVelocity(TargetOrbit) - _entity.Velocity;
|
||||
var entity = _context.Cache.Get<Entity>(_entity);
|
||||
var velocity = length(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 stoppingTime = length(deltaV) / (_context.Evaluate((_thrust.Data as ThrusterData).Thrust, _thrust.Item, entity) / entity.Mass);
|
||||
var stoppingDistance = stoppingTime * (velocity / 2);
|
||||
|
||||
var angleDiff = _entity.Direction.AngleDiff(deltaV);
|
||||
var turnaroundTime = angleDiff / (_entity.Context.Evaluate((_turning.Data as TurningData).Torque, _turning.Item, _entity) / _entity.Mass);
|
||||
var angleDiff = entity.Direction.AngleDiff(deltaV);
|
||||
var turnaroundTime = angleDiff / (_context.Evaluate((_turning.Data as TurningData).Torque, _turning.Item, entity) / entity.Mass);
|
||||
var turnaroundDistance = turnaroundTime * velocity;
|
||||
|
||||
return float2(stoppingDistance + turnaroundDistance, stoppingTime + turnaroundTime);
|
||||
|
|
|
|||
Loading…
Reference in a new issue