using System.Collections.Concurrent; namespace Sandbox; /// /// Simulates VerletRope components in parallel during PrePhysicsStep /// internal sealed class VerletRopeGameSystem : GameObjectSystem { private readonly List _ropes = new(); public VerletRopeGameSystem( Scene scene ) : base( scene ) { // Listen to StartFixedUpdate to run before physics Listen( Stage.StartFixedUpdate, -100, UpdateRopes, "UpdateRopes" ); } void UpdateRopes() { using var _ = PerformanceStats.Timings.Physics.Scope(); _ropes.Clear(); Scene.GetAll( _ropes ); if ( _ropes.Count == 0 ) return; var timeDelta = Time.Delta; System.Threading.Tasks.Parallel.ForEach( Partitioner.Create( _ropes, loadBalance: true ), rope => rope.Simulate( timeDelta ) ); } }