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
|
|
@ -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