#if GLEY_TRAFFIC_SYSTEM
using Unity.Burst;
using Unity.Collections;
using Unity.Mathematics;
using UnityEngine.Jobs;
namespace Gley.TrafficSystem.Internal
{
///
/// Applies rotation and position to the vehicle wheels
///
[BurstCompile]
public struct UpdateWheelJob : IJobParallelForTransform
{
[ReadOnly] public NativeArray WheelsOrigin;
[ReadOnly] public NativeArray DownDirection;
[ReadOnly] public NativeArray WheelRotation;
[ReadOnly] public NativeArray TurnAngle;
[ReadOnly] public NativeArray WheelRadius;
[ReadOnly] public NativeArray RayCastDistance;
[ReadOnly] public NativeArray MaxSuspension;
[ReadOnly] public NativeArray VehicleIndex;
[ReadOnly] public NativeArray CanSteer;
[ReadOnly] public NativeArray ExcludedWheels;
[ReadOnly] public int NrOfVehicles;
public void Execute(int index, TransformAccess transform)
{
if (ExcludedWheels[index] == true)
return;
//apply suspension
if (RayCastDistance[index] != 0)
{
//wheel is on ground
transform.position = WheelsOrigin[index] + DownDirection[VehicleIndex[index]] * (RayCastDistance[index] - WheelRadius[index]);
}
else
{
//wheel is in the air
transform.position = WheelsOrigin[index] + (DownDirection[VehicleIndex[index]] * MaxSuspension[VehicleIndex[index]]);
}
//apply rotation
if (CanSteer[index])
{
transform.localRotation = quaternion.EulerXYZ(math.radians(WheelRotation[VehicleIndex[index]]), math.radians(TurnAngle[VehicleIndex[index]]), 0);
}
else
{
transform.localRotation = quaternion.EulerXYZ(math.radians(WheelRotation[VehicleIndex[index]]), 0, 0);
}
}
}
}
#endif