2020-04-25 00:01:13 +01:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using Unity.Mathematics;
|
|
|
|
|
|
using static Unity.Mathematics.math;
|
|
|
|
|
|
|
|
|
|
|
|
public class VelocityMatch : AgentBehavior
|
|
|
|
|
|
{
|
|
|
|
|
|
public event Action OnMatch;
|
2020-05-10 00:16:33 +01:00
|
|
|
|
public float2 TargetVelocity;
|
2020-05-07 03:53:54 +01:00
|
|
|
|
private Thruster _thrust;
|
|
|
|
|
|
private Turning _turning;
|
2020-04-25 00:01:13 +01:00
|
|
|
|
private const float TargetThreshold = .1f;
|
|
|
|
|
|
|
2020-05-06 04:56:36 +01:00
|
|
|
|
private GameContext _context;
|
|
|
|
|
|
|
2020-05-07 03:53:54 +01:00
|
|
|
|
public VelocityMatch(GameContext context, Entity entity, ControllerData controllerData) : base(context, entity, controllerData)
|
2020-04-25 00:01:13 +01:00
|
|
|
|
{
|
2020-05-06 04:56:36 +01:00
|
|
|
|
_context = context;
|
2020-06-05 04:16:33 +01:00
|
|
|
|
_thrust = Entity.GetBehavior<Thruster>();
|
|
|
|
|
|
_turning = Entity.GetBehavior<Turning>();
|
2020-04-25 00:01:13 +01:00
|
|
|
|
}
|
2020-05-10 00:16:33 +01:00
|
|
|
|
|
|
|
|
|
|
public void Clear()
|
|
|
|
|
|
{
|
|
|
|
|
|
OnMatch = null;
|
|
|
|
|
|
}
|
2020-04-25 00:01:13 +01:00
|
|
|
|
|
|
|
|
|
|
public override void Update(float delta)
|
|
|
|
|
|
{
|
2020-05-10 00:16:33 +01:00
|
|
|
|
if (_thrust != null && _turning != null)
|
2020-04-25 00:01:13 +01:00
|
|
|
|
{
|
2020-05-10 00:16:33 +01:00
|
|
|
|
var deltaV = TargetVelocity - Entity.Velocity;
|
2020-04-25 00:01:13 +01:00
|
|
|
|
if(length(deltaV) < TargetThreshold)
|
|
|
|
|
|
OnMatch?.Invoke();
|
2020-06-05 04:16:33 +01:00
|
|
|
|
_turning.Axis = TurningInput(deltaV);
|
|
|
|
|
|
_thrust.Axis = ThrustInput(deltaV);
|
2020-04-25 00:01:13 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2020-04-28 01:50:50 +01:00
|
|
|
|
|
|
|
|
|
|
public float2 MatchDistanceTime
|
|
|
|
|
|
{
|
|
|
|
|
|
get
|
|
|
|
|
|
{
|
2020-05-07 03:53:54 +01:00
|
|
|
|
var velocity = length(Entity.Velocity);
|
2020-05-10 00:16:33 +01:00
|
|
|
|
var deltaV = TargetVelocity - Entity.Velocity;
|
2020-04-28 01:50:50 +01:00
|
|
|
|
|
2020-05-07 03:53:54 +01:00
|
|
|
|
var stoppingTime = length(deltaV) / (_thrust.Thrust / Entity.Mass);
|
2020-04-29 11:27:55 +01:00
|
|
|
|
var stoppingDistance = stoppingTime * (velocity / 2);
|
2020-04-28 01:50:50 +01:00
|
|
|
|
|
2020-05-07 03:53:54 +01:00
|
|
|
|
var angleDiff = Entity.Direction.AngleDiff(deltaV);
|
|
|
|
|
|
var turnaroundTime = angleDiff / (_turning.Torque / Entity.Mass);
|
2020-04-28 01:50:50 +01:00
|
|
|
|
var turnaroundDistance = turnaroundTime * velocity;
|
|
|
|
|
|
|
|
|
|
|
|
return float2(stoppingDistance + turnaroundDistance, stoppingTime + turnaroundTime);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2020-04-25 00:01:13 +01:00
|
|
|
|
}
|