namespace VeloX;

public partial class VeloXCar
{

	private void WheelThink( in float dt )
	{
		var maxRPM = GetTransmissionMaxRPM( Gear );


		var frontTorque = availableFrontTorque;
		var rearTorque = availableRearTorque;


		groundedCount = 0;
		float avgRPM = 0, totalSideSlip = 0, totalForwardSlip = 0;

		foreach ( var w in Wheels )
		{
			w.Update( this, dt );

			totalSideSlip += w.SideSlip;
			totalForwardSlip += w.ForwardSlip;
			var rpm = w.RPM;

			avgRPM += rpm * w.DistributionFactor;

			w.Torque = w.DistributionFactor * (w.IsFront ? frontTorque : rearTorque);
			w.Brake = w.IsFront ? frontBrake : rearBrake;

			if ( inputHandbrake && !w.IsFront )
				w.RPM = 0;

			if ( rpm > maxRPM )
				w.RPM = maxRPM;

			if ( w.IsOnGround )
				groundedCount++;

		}
		avgPoweredRPM = avgRPM;
		avgSideSlip = totalSideSlip / Wheels.Count;
		avgForwardSlip = totalForwardSlip / Wheels.Count;

	}
}
