Aetheria/Assets/Scripts/ServerShared/Agents/Locomotion.cs
hyperion51 b589a6422c Ship Locomotion & Tech Tree Panel
Ship locomotion now accounts for tangential velocity, slows down properly with terminal phase separated into velocity matching behavior
Tech tree properties panel visualizes parents and children correctly
2020-04-25 00:01:13 +01:00

35 lines
1.2 KiB
C#

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Unity.Mathematics;
using static Unity.Mathematics.math;
public class Locomotion : AgentBehavior
{
public Entity Objective;
public float LeadTime;
private Entity _entity;
private IAnalogBehavior _thrust;
private IAnalogBehavior _turning;
private const float Sensitivity = 1;
private const float TangentSensitivity = 2;
public Locomotion(Entity entity)
{
_entity = entity;
_thrust = entity.Axes.Keys.FirstOrDefault(x => x is Thruster);
_turning = entity.Axes.Keys.FirstOrDefault(x => x is Turning);
}
public override void Update(float delta)
{
if (Objective != null && _thrust != null && _turning != null)
{
float2 diff = Objective.Position + Objective.Velocity * LeadTime - _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;
}
}
}