| | | 1 | | //======================================================================= |
| | | 2 | | // GravitasWorldContext.cs |
| | | 3 | | //======================================================================= |
| | | 4 | | // MIT License, Copyright (c) 2026–present David Oravsky (mrdav30) |
| | | 5 | | // See LICENSE file in the project root for full license information. |
| | | 6 | | //======================================================================= |
| | | 7 | | |
| | | 8 | | using Chronicler; |
| | | 9 | | using FixedMathSharp; |
| | | 10 | | using Gravitas.CollisionHandling; |
| | | 11 | | using Gravitas.Diagnostics; |
| | | 12 | | using Gravitas.Queries; |
| | | 13 | | using Gravitas.Support; |
| | | 14 | | using GridForge.Grids; |
| | | 15 | | using GridForge.Grids.Topology; |
| | | 16 | | using SwiftCollections; |
| | | 17 | | using System; |
| | | 18 | | |
| | | 19 | | namespace Gravitas; |
| | | 20 | | |
| | | 21 | | /// <summary> |
| | | 22 | | /// Owns Gravitas runtime state for one explicit <see cref="GridWorld"/>. |
| | | 23 | | /// </summary> |
| | | 24 | | /// <remarks> |
| | | 25 | | /// This is the context-first host API for multi-world Gravitas usage. Phase 1 owns |
| | | 26 | | /// world lifetime, deterministic clock state, and lifecycle hooks; later phases move |
| | | 27 | | /// physics registries, collision partitioning, query buffers, and coroutine state here. |
| | | 28 | | /// </remarks> |
| | | 29 | | public sealed class GravitasWorldContext : IDisposable |
| | | 30 | | { |
| | 1 | 31 | | private static readonly object _worldOwnershipLock = new(); |
| | | 32 | | |
| | 1 | 33 | | private static readonly SwiftDictionary<GridWorld, GravitasWorldContext> _worldOwners = new(); |
| | | 34 | | |
| | 5083 | 35 | | private readonly GravitasClock _clock = new(); |
| | | 36 | | |
| | 5083 | 37 | | private readonly GravitasLifecycleHooks _hooks = new(); |
| | | 38 | | |
| | | 39 | | private readonly bool _ownsWorld; |
| | | 40 | | |
| | | 41 | | private bool _disposed; |
| | | 42 | | |
| | | 43 | | private int _lateSimulateToken; |
| | | 44 | | |
| | | 45 | | private int _simulationPhaseDepth; |
| | | 46 | | |
| | | 47 | | private bool _fixedStepOpen; |
| | | 48 | | |
| | 7647 | 49 | | internal void EnterSimulationPhase() => _simulationPhaseDepth++; |
| | | 50 | | |
| | 7647 | 51 | | internal void ExitSimulationPhase() => _simulationPhaseDepth--; |
| | | 52 | | |
| | | 53 | | internal void ThrowIfFixedStepMutationNotAllowed() |
| | | 54 | | { |
| | 269 | 55 | | SwiftThrowHelper.ThrowIfTrue( |
| | 269 | 56 | | _fixedStepOpen || _simulationPhaseDepth > 0, |
| | 269 | 57 | | nameof(GravitasWorldContext), |
| | 269 | 58 | | "Authoritative body roles, static poses, and loaded state can change only outside the Simulate-to-LateSimula |
| | 256 | 59 | | } |
| | | 60 | | |
| | 5083 | 61 | | private GravitasWorldContext(GridWorld world, bool ownsWorld) |
| | | 62 | | { |
| | 5083 | 63 | | World = world; |
| | 5083 | 64 | | _ownsWorld = ownsWorld; |
| | 5083 | 65 | | Settings = PhysicsSettings.DefaultSettings(); |
| | 5083 | 66 | | Environment = PhysicsEnvironment.Default(Settings.FrameRate); |
| | 5083 | 67 | | CollisionScratch = new CollisionSatScratch(); |
| | 5083 | 68 | | Diagnostics = new GravitasDiagnosticSink(this); |
| | 5083 | 69 | | Constraints3D = new GravitasConstraint3DService(this); |
| | 5083 | 70 | | Constraints2D = new GravitasConstraint2DService(this); |
| | 5083 | 71 | | Collisions = new GravitasCollisionService(this); |
| | 5083 | 72 | | Collisions2D = new GravitasCollision2DService(this); |
| | 5083 | 73 | | Physics = new GravitasPhysicsService(this); |
| | 5083 | 74 | | Physics2D = new GravitasPhysics2DService(this); |
| | 5083 | 75 | | MixedCollisions = new GravitasMixedCollisionService(this); |
| | 5083 | 76 | | Query2D = new GravitasQuery2DService(this); |
| | 5083 | 77 | | Query3D = new GravitasQuery3DService(this); |
| | 5083 | 78 | | QueryMixed = new GravitasQueryMixedService(this); |
| | 5083 | 79 | | Coroutines = new GravitasCoroutineService(this); |
| | 5083 | 80 | | } |
| | | 81 | | |
| | | 82 | | /// <summary> |
| | | 83 | | /// Gets the explicit GridForge world owned or referenced by this context. |
| | | 84 | | /// </summary> |
| | | 85 | | public GridWorld World { get; } |
| | | 86 | | |
| | | 87 | | /// <summary> |
| | | 88 | | /// Gets this context's world-local physics settings. |
| | | 89 | | /// </summary> |
| | | 90 | | public PhysicsSettings Settings { get; private set; } |
| | | 91 | | |
| | | 92 | | /// <summary> |
| | | 93 | | /// Gets this context's world-local physical environment values. |
| | | 94 | | /// </summary> |
| | | 95 | | public PhysicsEnvironment Environment { get; } |
| | | 96 | | |
| | | 97 | | /// <summary> |
| | | 98 | | /// Gets this context's world-local collision partitioning service. |
| | | 99 | | /// </summary> |
| | | 100 | | public GravitasCollisionService Collisions { get; } |
| | | 101 | | |
| | | 102 | | /// <summary> |
| | | 103 | | /// Gets this context's world-local pure 2D collision partitioning service. |
| | | 104 | | /// </summary> |
| | | 105 | | public GravitasCollision2DService Collisions2D { get; } |
| | | 106 | | |
| | | 107 | | /// <summary> |
| | | 108 | | /// Gets this context's world-local physics registration and pair service. |
| | | 109 | | /// </summary> |
| | | 110 | | public GravitasPhysicsService Physics { get; } |
| | | 111 | | |
| | | 112 | | /// <summary> |
| | | 113 | | /// Gets this context's world-local pure 2D physics service. |
| | | 114 | | /// </summary> |
| | | 115 | | public GravitasPhysics2DService Physics2D { get; } |
| | | 116 | | |
| | | 117 | | internal GravitasMixedCollisionService MixedCollisions { get; } |
| | | 118 | | |
| | | 119 | | /// <summary> |
| | | 120 | | /// Gets this context's world-local pure 2D query service. |
| | | 121 | | /// </summary> |
| | | 122 | | public GravitasQuery2DService Query2D { get; } |
| | | 123 | | |
| | | 124 | | /// <summary> |
| | | 125 | | /// Gets this context's world-local 3D query service. |
| | | 126 | | /// </summary> |
| | | 127 | | public GravitasQuery3DService Query3D { get; } |
| | | 128 | | |
| | | 129 | | /// <summary> |
| | | 130 | | /// Gets this context's explicit mixed 3D/2D query service. |
| | | 131 | | /// </summary> |
| | | 132 | | public GravitasQueryMixedService QueryMixed { get; } |
| | | 133 | | |
| | | 134 | | /// <summary> |
| | | 135 | | /// Gets this context's world-local lockstep coroutine service. |
| | | 136 | | /// </summary> |
| | | 137 | | public GravitasCoroutineService Coroutines { get; } |
| | | 138 | | |
| | | 139 | | /// <summary> |
| | | 140 | | /// Gets this context's deterministic diagnostic sink. |
| | | 141 | | /// </summary> |
| | | 142 | | public GravitasDiagnosticSink Diagnostics { get; } |
| | | 143 | | |
| | | 144 | | /// <summary> |
| | | 145 | | /// Gets this context's world-local 3D constraint and ragdoll service. |
| | | 146 | | /// </summary> |
| | | 147 | | public GravitasConstraint3DService Constraints3D { get; } |
| | | 148 | | |
| | | 149 | | /// <summary> |
| | | 150 | | /// Gets this context's world-local pure 2D constraint and ragdoll service. |
| | | 151 | | /// </summary> |
| | | 152 | | public GravitasConstraint2DService Constraints2D { get; } |
| | | 153 | | |
| | | 154 | | internal CollisionSatScratch CollisionScratch { get; } |
| | | 155 | | |
| | | 156 | | /// <summary> |
| | | 157 | | /// Gets whether this context has been disposed. |
| | | 158 | | /// </summary> |
| | 80 | 159 | | public bool IsDisposed => _disposed; |
| | | 160 | | |
| | | 161 | | /// <summary> |
| | | 162 | | /// Gets a representative grid cell edge for this context's world. |
| | | 163 | | /// </summary> |
| | | 164 | | public Fixed64 VoxelSize |
| | | 165 | | { |
| | | 166 | | get |
| | | 167 | | { |
| | 6 | 168 | | ThrowIfDisposed(); |
| | 6 | 169 | | return GridTopologyMetricUtility.GetRepresentativeCellEdge(World); |
| | | 170 | | } |
| | | 171 | | } |
| | | 172 | | |
| | | 173 | | /// <summary> |
| | | 174 | | /// Gets this context's fixed simulation frame rate. |
| | | 175 | | /// </summary> |
| | | 176 | | public int FrameRate |
| | | 177 | | { |
| | | 178 | | get |
| | | 179 | | { |
| | 16090 | 180 | | ThrowIfDisposed(); |
| | 16090 | 181 | | return _clock.FrameRate; |
| | | 182 | | } |
| | | 183 | | } |
| | | 184 | | |
| | | 185 | | /// <summary> |
| | | 186 | | /// Gets this context's fixed simulation time step. |
| | | 187 | | /// </summary> |
| | | 188 | | public Fixed64 DeltaTime |
| | | 189 | | { |
| | | 190 | | get |
| | | 191 | | { |
| | 609870 | 192 | | ThrowIfDisposed(); |
| | 609870 | 193 | | return _clock.DeltaTime; |
| | | 194 | | } |
| | | 195 | | } |
| | | 196 | | |
| | | 197 | | /// <summary> |
| | | 198 | | /// Gets the reciprocal of this context's fixed simulation time step. |
| | | 199 | | /// </summary> |
| | | 200 | | public Fixed64 InvDeltaTime |
| | | 201 | | { |
| | | 202 | | get |
| | | 203 | | { |
| | 996 | 204 | | ThrowIfDisposed(); |
| | 996 | 205 | | return _clock.InvDeltaTime; |
| | | 206 | | } |
| | | 207 | | } |
| | | 208 | | |
| | | 209 | | /// <summary> |
| | | 210 | | /// Gets this context's simulated frame count. |
| | | 211 | | /// </summary> |
| | | 212 | | public int FrameCount |
| | | 213 | | { |
| | | 214 | | get |
| | | 215 | | { |
| | 388240 | 216 | | ThrowIfDisposed(); |
| | 388240 | 217 | | return _clock.FrameCount; |
| | | 218 | | } |
| | | 219 | | } |
| | | 220 | | |
| | | 221 | | /// <summary> |
| | | 222 | | /// Gets this context's total simulated time. |
| | | 223 | | /// </summary> |
| | | 224 | | public Fixed64 TotalTime |
| | | 225 | | { |
| | | 226 | | get |
| | | 227 | | { |
| | 1012 | 228 | | ThrowIfDisposed(); |
| | 1012 | 229 | | return _clock.TotalTime; |
| | | 230 | | } |
| | | 231 | | } |
| | | 232 | | |
| | | 233 | | /// <summary> |
| | | 234 | | /// Gets this context's accumulated visualization time. |
| | | 235 | | /// </summary> |
| | | 236 | | public Fixed64 AccumulatedTime |
| | | 237 | | { |
| | | 238 | | get |
| | | 239 | | { |
| | 1 | 240 | | ThrowIfDisposed(); |
| | 1 | 241 | | return _clock.AccumulatedTime; |
| | | 242 | | } |
| | | 243 | | } |
| | | 244 | | |
| | | 245 | | /// <summary> |
| | | 246 | | /// Gets whether this context's visualization accumulation will reset on the next visualize call. |
| | | 247 | | /// </summary> |
| | | 248 | | public bool ResetAccumulation |
| | | 249 | | { |
| | | 250 | | get |
| | | 251 | | { |
| | 3 | 252 | | ThrowIfDisposed(); |
| | 3 | 253 | | return _clock.ResetAccumulation; |
| | | 254 | | } |
| | | 255 | | } |
| | | 256 | | |
| | | 257 | | internal bool ResetAccumulationThisVisualize |
| | | 258 | | { |
| | | 259 | | get |
| | | 260 | | { |
| | 31 | 261 | | ThrowIfDisposed(); |
| | 31 | 262 | | return _clock.ResetAccumulationThisVisualize; |
| | | 263 | | } |
| | | 264 | | } |
| | | 265 | | |
| | 48364 | 266 | | internal int LateSimulateToken => _lateSimulateToken; |
| | | 267 | | |
| | | 268 | | /// <summary> |
| | | 269 | | /// Gets this context's visualization accumulation expressed in simulation frames. |
| | | 270 | | /// </summary> |
| | | 271 | | public Fixed64 ExpectedAccumulation |
| | | 272 | | { |
| | | 273 | | get |
| | | 274 | | { |
| | 4 | 275 | | ThrowIfDisposed(); |
| | 4 | 276 | | return _clock.ExpectedAccumulation; |
| | | 277 | | } |
| | | 278 | | } |
| | | 279 | | |
| | | 280 | | /// <summary> |
| | | 281 | | /// Computes a deterministic fixed-width hash of this context's replay-relevant physics state. |
| | | 282 | | /// </summary> |
| | | 283 | | /// <param name="mode">Selects whether diagnostic solver/cache state is included in addition to authoritative state. |
| | | 284 | | /// <returns>A deterministic hash suitable for lockstep replay conformance checks.</returns> |
| | | 285 | | /// <remarks> |
| | | 286 | | /// The hash includes context settings, environment values, body state, collider shape/filter state, |
| | | 287 | | /// retained pair/contact state, and continuation-affecting CCD handoff state. It excludes host object |
| | | 288 | | /// identity, delegates, diagnostics buffers, debug draw data, query scratch buffers, and visualization |
| | | 289 | | /// interpolation caches. |
| | | 290 | | /// </remarks> |
| | | 291 | | public ChronicleHash ComputeReplayHash( |
| | | 292 | | GravitasReplayHashMode mode = GravitasReplayHashMode.Authoritative) |
| | | 293 | | { |
| | 993 | 294 | | ThrowIfDisposed(); |
| | 993 | 295 | | return GravitasReplayHashService.Compute(this, mode); |
| | | 296 | | } |
| | | 297 | | |
| | | 298 | | /// <summary> |
| | | 299 | | /// Attaches a context to a host-owned <see cref="GridWorld"/>. |
| | | 300 | | /// </summary> |
| | | 301 | | /// <param name="world">The active world to bind.</param> |
| | | 302 | | /// <param name="takeOwnership">True when disposing this context should dispose the supplied world.</param> |
| | | 303 | | /// <returns>A context bound to <paramref name="world"/>.</returns> |
| | | 304 | | public static GravitasWorldContext Attach(GridWorld world, bool takeOwnership = false) |
| | | 305 | | { |
| | 8 | 306 | | SwiftThrowHelper.ThrowIfNull(world, nameof(world)); |
| | 7 | 307 | | return CreateRegistered(world, takeOwnership); |
| | | 308 | | } |
| | | 309 | | |
| | | 310 | | /// <summary> |
| | | 311 | | /// Creates a context with an owned <see cref="GridWorld"/>. |
| | | 312 | | /// </summary> |
| | | 313 | | /// <param name="spatialGridCellSize">Spatial hash cell size for the created world.</param> |
| | | 314 | | /// <returns>A context that owns its created world.</returns> |
| | | 315 | | public static GravitasWorldContext CreateOwned( |
| | | 316 | | int spatialGridCellSize = GridWorld.DefaultSpatialGridCellSize) |
| | | 317 | | { |
| | 5079 | 318 | | return CreateRegistered( |
| | 5079 | 319 | | new GridWorld(spatialGridCellSize), |
| | 5079 | 320 | | ownsWorld: true); |
| | | 321 | | } |
| | | 322 | | |
| | | 323 | | /// <summary> |
| | | 324 | | /// Advances this context's deterministic simulation clock and ordered simulate hooks. |
| | | 325 | | /// </summary> |
| | | 326 | | public void Simulate() |
| | | 327 | | { |
| | 2320 | 328 | | ThrowIfDisposed(); |
| | 2320 | 329 | | _fixedStepOpen = true; |
| | 2320 | 330 | | EnterSimulationPhase(); |
| | | 331 | | try |
| | | 332 | | { |
| | 2320 | 333 | | _clock.Simulate(); |
| | 2320 | 334 | | PhysicsRuntimeMode runtimeMode = Settings.RuntimeMode; |
| | 2320 | 335 | | if (runtimeMode.Runs3D()) |
| | 2001 | 336 | | Physics.Simulate(); |
| | 2320 | 337 | | if (runtimeMode.Runs2D()) |
| | 479 | 338 | | Physics2D.Simulate(); |
| | 2320 | 339 | | if (runtimeMode.RunsMixedContacts()) |
| | 153 | 340 | | MixedCollisions.Simulate(); |
| | | 341 | | |
| | 2320 | 342 | | Coroutines.Simulate(); |
| | 2320 | 343 | | _hooks.InvokeSimulate(); |
| | 2318 | 344 | | } |
| | 2 | 345 | | catch |
| | | 346 | | { |
| | 2 | 347 | | _fixedStepOpen = false; |
| | 2 | 348 | | throw; |
| | | 349 | | } |
| | | 350 | | finally |
| | | 351 | | { |
| | 2320 | 352 | | ExitSimulationPhase(); |
| | 2320 | 353 | | } |
| | 2318 | 354 | | } |
| | | 355 | | |
| | | 356 | | /// <summary> |
| | | 357 | | /// Runs this context's late-simulation step. |
| | | 358 | | /// </summary> |
| | | 359 | | public void LateSimulate() |
| | | 360 | | { |
| | 2832 | 361 | | ThrowIfDisposed(); |
| | 2832 | 362 | | _fixedStepOpen = true; |
| | 2832 | 363 | | EnterSimulationPhase(); |
| | | 364 | | try |
| | | 365 | | { |
| | 2832 | 366 | | _clock.LateSimulate(); |
| | 2832 | 367 | | PhysicsRuntimeMode runtimeMode = Settings.RuntimeMode; |
| | 2832 | 368 | | bool willRun3D = runtimeMode.Runs3D() && Physics.SimulatePhysics; |
| | 2832 | 369 | | bool willRun2D = runtimeMode.Runs2D() && Physics2D.SimulatePhysics; |
| | 2832 | 370 | | if (willRun3D || willRun2D) |
| | 2829 | 371 | | AdvanceLateSimulateToken(); |
| | 2832 | 372 | | if (willRun3D) |
| | 2180 | 373 | | Physics.PrepareContinuousCollisionFrame(); |
| | 2831 | 374 | | if (willRun2D) |
| | 1003 | 375 | | Physics2D.PrepareContinuousCollisionFrame(); |
| | 2830 | 376 | | bool ran3D = willRun3D |
| | 2830 | 377 | | && Physics.BeginLateSimulateBodies(continuousCollisionFramePrepared: true); |
| | 2829 | 378 | | bool ran2D = willRun2D |
| | 2829 | 379 | | && Physics2D.BeginLateSimulateBodies(continuousCollisionFramePrepared: true); |
| | 2828 | 380 | | ProcessQueuedContinuousCollisionHandoffs(ran3D, ran2D); |
| | 2827 | 381 | | if (ran3D) |
| | 2177 | 382 | | Physics.CompleteLateSimulatePhysicsStep(); |
| | 2825 | 383 | | if (ran2D) |
| | 1000 | 384 | | Physics2D.CompleteLateSimulatePhysicsStep(); |
| | 2825 | 385 | | if (runtimeMode.RunsMixedContacts()) |
| | 345 | 386 | | MixedCollisions.LateSimulate(); |
| | | 387 | | |
| | 2825 | 388 | | _hooks.InvokeLateSimulate(); |
| | 2824 | 389 | | } |
| | | 390 | | finally |
| | | 391 | | { |
| | 2832 | 392 | | ExitSimulationPhase(); |
| | 2832 | 393 | | _fixedStepOpen = false; |
| | 2832 | 394 | | } |
| | 2824 | 395 | | } |
| | | 396 | | |
| | | 397 | | private void ProcessQueuedContinuousCollisionHandoffs(bool runs3D, bool runs2D) |
| | | 398 | | { |
| | 2828 | 399 | | if (!runs3D && !runs2D) |
| | 3 | 400 | | return; |
| | | 401 | | |
| | | 402 | | try |
| | | 403 | | { |
| | 2825 | 404 | | int iterationLimit = Settings.ContinuousCollisionMaxToiIterations; |
| | 2825 | 405 | | int remainingIterations = iterationLimit; |
| | 5796 | 406 | | for (int iteration = 0; iteration < iterationLimit && remainingIterations > 0; iteration++) |
| | | 407 | | { |
| | 2891 | 408 | | int processedIterations = 0; |
| | 2891 | 409 | | if (runs3D) |
| | | 410 | | { |
| | 2237 | 411 | | int usedIterations = Physics.ProcessQueuedContinuousCollisionHandoffs(remainingIterations); |
| | 2236 | 412 | | processedIterations += usedIterations; |
| | 2236 | 413 | | remainingIterations -= usedIterations; |
| | | 414 | | } |
| | | 415 | | |
| | 2890 | 416 | | if (remainingIterations > 0 && runs2D) |
| | | 417 | | { |
| | 1050 | 418 | | int usedIterations = Physics2D.ProcessQueuedContinuousCollisionHandoffs(remainingIterations); |
| | 1050 | 419 | | processedIterations += usedIterations; |
| | 1050 | 420 | | remainingIterations -= usedIterations; |
| | | 421 | | } |
| | | 422 | | |
| | 2890 | 423 | | if (processedIterations == 0) |
| | 2817 | 424 | | return; |
| | | 425 | | } |
| | | 426 | | |
| | 7 | 427 | | if (runs3D) |
| | 6 | 428 | | Physics.ProcessQueuedContinuousCollisionHandoffs(iterationBudget: 0); |
| | 7 | 429 | | if (runs2D) |
| | 6 | 430 | | Physics2D.ProcessQueuedContinuousCollisionHandoffs(iterationBudget: 0); |
| | 7 | 431 | | } |
| | 1 | 432 | | catch |
| | | 433 | | { |
| | 1 | 434 | | if (runs3D) |
| | 1 | 435 | | Physics.AbortContinuousCollisionHandoffFrame(); |
| | 1 | 436 | | if (runs2D) |
| | 1 | 437 | | Physics2D.AbortContinuousCollisionHandoffFrame(); |
| | 1 | 438 | | throw; |
| | | 439 | | } |
| | 2824 | 440 | | } |
| | | 441 | | |
| | 3005 | 442 | | internal void AdvanceLateSimulateToken() => _lateSimulateToken++; |
| | | 443 | | |
| | | 444 | | /// <summary> |
| | | 445 | | /// Runs this context's visualization accumulation step. |
| | | 446 | | /// </summary> |
| | | 447 | | public void Visualize() |
| | | 448 | | { |
| | 29 | 449 | | ThrowIfDisposed(); |
| | 29 | 450 | | _clock.Visualize(); |
| | 29 | 451 | | PhysicsRuntimeMode runtimeMode = Settings.RuntimeMode; |
| | 29 | 452 | | if (runtimeMode.Runs3D()) |
| | 23 | 453 | | Physics.Visualize(); |
| | 29 | 454 | | if (runtimeMode.Runs2D()) |
| | 7 | 455 | | Physics2D.Visualize(); |
| | 29 | 456 | | if (runtimeMode.RunsMixedContacts()) |
| | 1 | 457 | | MixedCollisions.Visualize(); |
| | | 458 | | |
| | 29 | 459 | | _hooks.InvokeVisualize(); |
| | 29 | 460 | | } |
| | | 461 | | |
| | | 462 | | /// <summary> |
| | | 463 | | /// Runs this context's host-owned late-visualization hook phase. |
| | | 464 | | /// </summary> |
| | | 465 | | public void LateVisualize() |
| | | 466 | | { |
| | 15 | 467 | | ThrowIfDisposed(); |
| | 15 | 468 | | _hooks.InvokeLateVisualize(); |
| | 15 | 469 | | } |
| | | 470 | | |
| | | 471 | | /// <summary> |
| | | 472 | | /// Resets this context's deterministic clock and context-local lifecycle hooks. |
| | | 473 | | /// </summary> |
| | | 474 | | public void Reset() |
| | | 475 | | { |
| | 31 | 476 | | ThrowIfDisposed(); |
| | 31 | 477 | | _lateSimulateToken = 0; |
| | 31 | 478 | | _clock.Reset(); |
| | 31 | 479 | | Constraints3D.Reset(); |
| | 31 | 480 | | Constraints2D.Reset(); |
| | 31 | 481 | | Collisions.Reset(); |
| | 31 | 482 | | Collisions2D.Reset(); |
| | 31 | 483 | | Physics.Reset(); |
| | 31 | 484 | | Physics2D.Reset(); |
| | 31 | 485 | | MixedCollisions.Reset(); |
| | 31 | 486 | | Query2D.Reset(); |
| | 31 | 487 | | Query3D.Reset(); |
| | 31 | 488 | | QueryMixed.Reset(); |
| | 31 | 489 | | Coroutines.Reset(); |
| | 31 | 490 | | Diagnostics.Reset(); |
| | 31 | 491 | | _hooks.InvokeReset(); |
| | 31 | 492 | | } |
| | | 493 | | |
| | | 494 | | /// <summary> |
| | | 495 | | /// Updates this context's fixed simulation frame rate. |
| | | 496 | | /// </summary> |
| | | 497 | | /// <param name="frameRate">The new frame rate. Must be within the supported physics settings range.</param> |
| | | 498 | | public void SetFrameRate(int frameRate) |
| | | 499 | | { |
| | 1329 | 500 | | ThrowIfDisposed(); |
| | 1329 | 501 | | Settings.SetFrameRate(frameRate); |
| | 1328 | 502 | | _clock.SetFrameRate(frameRate); |
| | 1328 | 503 | | _hooks.InvokeFrameRateChanged(); |
| | 1328 | 504 | | } |
| | | 505 | | |
| | | 506 | | /// <summary> |
| | | 507 | | /// Applies context-local settings and synchronizes frame-derived clock state. |
| | | 508 | | /// </summary> |
| | | 509 | | /// <param name="settings">The settings instance to own.</param> |
| | | 510 | | public void ApplySettings(PhysicsSettings settings) |
| | | 511 | | { |
| | 535 | 512 | | ThrowIfDisposed(); |
| | 535 | 513 | | SwiftThrowHelper.ThrowIfNull(settings, nameof(settings)); |
| | | 514 | | |
| | 535 | 515 | | Settings = settings; |
| | 535 | 516 | | _clock.SetFrameRate(settings.FrameRate); |
| | 535 | 517 | | _hooks.InvokeFrameRateChanged(); |
| | 535 | 518 | | } |
| | | 519 | | |
| | | 520 | | /// <summary> |
| | | 521 | | /// Calculates the frame index containing the specified fixed-point timestamp. |
| | | 522 | | /// </summary> |
| | | 523 | | /// <param name="timestamp">The timestamp to resolve.</param> |
| | | 524 | | /// <returns>The zero-based frame index for the timestamp.</returns> |
| | | 525 | | public int GetFrameFromTime(Fixed64 timestamp) |
| | | 526 | | { |
| | 3 | 527 | | ThrowIfDisposed(); |
| | 3 | 528 | | return _clock.GetFrameFromTime(timestamp); |
| | | 529 | | } |
| | | 530 | | |
| | | 531 | | internal IDisposable RegisterOnSimulate(string owner, int order, Action callback) |
| | | 532 | | { |
| | 9 | 533 | | ThrowIfDisposed(); |
| | 9 | 534 | | return _hooks.RegisterOnSimulate(owner, order, callback); |
| | | 535 | | } |
| | | 536 | | |
| | | 537 | | internal IDisposable RegisterOnLateSimulate(string owner, int order, Action callback) |
| | | 538 | | { |
| | 3 | 539 | | ThrowIfDisposed(); |
| | 3 | 540 | | return _hooks.RegisterOnLateSimulate(owner, order, callback); |
| | | 541 | | } |
| | | 542 | | |
| | | 543 | | internal IDisposable RegisterOnVisualize(string owner, int order, Action callback) |
| | | 544 | | { |
| | 1 | 545 | | ThrowIfDisposed(); |
| | 1 | 546 | | return _hooks.RegisterOnVisualize(owner, order, callback); |
| | | 547 | | } |
| | | 548 | | |
| | | 549 | | internal IDisposable RegisterOnLateVisualize(string owner, int order, Action callback) |
| | | 550 | | { |
| | 1 | 551 | | ThrowIfDisposed(); |
| | 1 | 552 | | return _hooks.RegisterOnLateVisualize(owner, order, callback); |
| | | 553 | | } |
| | | 554 | | |
| | | 555 | | internal IDisposable RegisterOnReset(string owner, int order, Action callback) |
| | | 556 | | { |
| | 2 | 557 | | ThrowIfDisposed(); |
| | 2 | 558 | | return _hooks.RegisterOnReset(owner, order, callback); |
| | | 559 | | } |
| | | 560 | | |
| | | 561 | | internal IDisposable RegisterOnFrameRateChanged(string owner, int order, Action callback) |
| | | 562 | | { |
| | 2 | 563 | | ThrowIfDisposed(); |
| | 2 | 564 | | return _hooks.RegisterOnFrameRateChanged(owner, order, callback); |
| | | 565 | | } |
| | | 566 | | |
| | | 567 | | /// <inheritdoc/> |
| | | 568 | | public void Dispose() |
| | | 569 | | { |
| | 5089 | 570 | | lock (_worldOwnershipLock) |
| | | 571 | | { |
| | 5089 | 572 | | if (_disposed) |
| | 6 | 573 | | return; |
| | | 574 | | |
| | 5083 | 575 | | Constraints3D.Reset(); |
| | 5083 | 576 | | Constraints2D.Reset(); |
| | 5083 | 577 | | _disposed = true; |
| | | 578 | | try |
| | | 579 | | { |
| | 5083 | 580 | | Coroutines.Deactivate(); |
| | 5083 | 581 | | } |
| | | 582 | | finally |
| | | 583 | | { |
| | | 584 | | try |
| | | 585 | | { |
| | 5083 | 586 | | if (_ownsWorld && World.IsActive) |
| | 5080 | 587 | | World.Dispose(); |
| | 5083 | 588 | | } |
| | | 589 | | finally |
| | | 590 | | { |
| | 5083 | 591 | | ReleaseWorldOwnership(this); |
| | 5083 | 592 | | } |
| | 5083 | 593 | | } |
| | | 594 | | } |
| | 5089 | 595 | | } |
| | | 596 | | |
| | | 597 | | private static GravitasWorldContext CreateRegistered(GridWorld world, bool ownsWorld) |
| | | 598 | | { |
| | 5086 | 599 | | lock (_worldOwnershipLock) |
| | | 600 | | { |
| | 5086 | 601 | | SwiftThrowHelper.ThrowIfTrue( |
| | 5086 | 602 | | !world.IsActive, |
| | 5086 | 603 | | nameof(GravitasWorldContext), |
| | 5086 | 604 | | "GravitasWorldContext requires an active GridWorld."); |
| | 5085 | 605 | | ThrowIfWorldOwned(world); |
| | 5083 | 606 | | GravitasWorldContext context = new(world, ownsWorld); |
| | 5083 | 607 | | _worldOwners[world] = context; |
| | 5083 | 608 | | return context; |
| | | 609 | | } |
| | 5083 | 610 | | } |
| | | 611 | | |
| | | 612 | | private static void ThrowIfWorldOwned(GridWorld world) |
| | | 613 | | { |
| | 5085 | 614 | | SwiftThrowHelper.ThrowIfTrue( |
| | 5085 | 615 | | _worldOwners.ContainsKey(world), |
| | 5085 | 616 | | nameof(GravitasWorldContext), |
| | 5085 | 617 | | "GridWorld is already attached to an active GravitasWorldContext."); |
| | 5083 | 618 | | } |
| | | 619 | | |
| | | 620 | | private static void ReleaseWorldOwnership(GravitasWorldContext context) |
| | | 621 | | { |
| | 5083 | 622 | | _worldOwners.Remove(context.World); |
| | 5083 | 623 | | } |
| | | 624 | | |
| | | 625 | | internal void ThrowIfDisposed() |
| | | 626 | | { |
| | 1025019 | 627 | | SwiftThrowHelper.ThrowIfDisposed(_disposed, nameof(GravitasWorldContext)); |
| | 1025015 | 628 | | SwiftThrowHelper.ThrowIfTrue( |
| | 1025015 | 629 | | !World.IsActive, |
| | 1025015 | 630 | | nameof(GravitasWorldContext), |
| | 1025015 | 631 | | "GravitasWorldContext is bound to an inactive GridWorld."); |
| | 1025015 | 632 | | } |
| | | 633 | | } |