2020-04-23 03:31:17 +01:00
|
|
|
|
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;
|
2020-04-25 00:01:13 +01:00
|
|
|
|
public float LeadTime;
|
2020-04-23 03:31:17 +01:00
|
|
|
|
private Entity _entity;
|
|
|
|
|
|
private IAnalogBehavior _thrust;
|
|
|
|
|
|
private IAnalogBehavior _turning;
|
|
|
|
|
|
private const float Sensitivity = 1;
|
2020-04-25 00:01:13 +01:00
|
|
|
|
private const float TangentSensitivity = 2;
|
2020-04-23 03:31:17 +01:00
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
{
|
2020-04-25 00:01:13 +01:00
|
|
|
|
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);
|
2020-04-23 03:31:17 +01:00
|
|
|
|
_entity.Axes[_turning] = clamp(-angleDiff * Sensitivity, -1, 1);
|
|
|
|
|
|
_entity.Axes[_thrust] = 1;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|