71 lines
1.7 KiB
C#
71 lines
1.7 KiB
C#
using Sandbox;
|
|
using System.Linq;
|
|
namespace VeloX;
|
|
|
|
internal sealed class WheelManager : GameObjectSystem
|
|
{
|
|
|
|
|
|
public WheelManager( Scene scene ) : base( scene )
|
|
{
|
|
if ( Application.IsDedicatedServer )
|
|
return;
|
|
|
|
Listen( Stage.StartFixedUpdate, -99, UpdateWheels, "UpdateWheels" );
|
|
Listen( Stage.StartFixedUpdate, -100, UpdateEngine, "UpdateEngine" );
|
|
}
|
|
|
|
private void UpdateWheels()
|
|
{
|
|
if ( !Game.IsPlaying || Scene.IsEditor )
|
|
return;
|
|
//Stopwatch sw = Stopwatch.StartNew();
|
|
|
|
var wheels = Scene.GetAll<VeloXWheel>();
|
|
if ( !wheels.Any() ) return;
|
|
|
|
var timeDelta = Time.Delta;
|
|
//Sandbox.Utility.Parallel.ForEach( wheels, item =>
|
|
//{
|
|
// if ( !item.IsProxy && item.IsValid() )
|
|
// item.DoPhysics( timeDelta );
|
|
//} );
|
|
foreach ( var item in wheels )
|
|
if ( item.IsValid() && !item.IsProxy )
|
|
item.DoPhysics( timeDelta );
|
|
|
|
foreach ( var wheel in wheels )
|
|
if ( wheel.IsValid() && !wheel.IsProxy)
|
|
wheel.UpdateForce();
|
|
|
|
//sw.Stop();
|
|
|
|
//DebugOverlaySystem.Current.ScreenText( new Vector2( 120, 30 ), $"Wheel Sim: {sw.Elapsed.TotalMilliseconds,6:F2} ms", 24 );
|
|
}
|
|
private void UpdateEngine()
|
|
{
|
|
if ( !Game.IsPlaying || Scene.IsEditor )
|
|
return;
|
|
//Stopwatch sw = Stopwatch.StartNew();
|
|
|
|
var engines = Scene.GetAll<Engine>();
|
|
if ( !engines.Any() ) return;
|
|
|
|
var timeDelta = Time.Delta;
|
|
//Sandbox.Utility.Parallel.ForEach( engines, item =>
|
|
//{
|
|
// foreach ( var wheel in engines )
|
|
// if ( !wheel.IsProxy && wheel.IsValid() )
|
|
// wheel.UpdateEngine( timeDelta );
|
|
//} );
|
|
foreach ( var wheel in engines )
|
|
if ( wheel.IsValid() )
|
|
wheel.UpdateEngine( timeDelta );
|
|
|
|
|
|
//sw.Stop();
|
|
|
|
//DebugOverlaySystem.Current.ScreenText( new Vector2( 120, 54 ), $"Engine Sim: {sw.Elapsed.TotalMilliseconds,6:F2} ms", 24 );
|
|
}
|
|
}
|