| | | 1 | | //======================================================================= |
| | | 2 | | // RagdollRuntime3D.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 System.Runtime.CompilerServices; |
| | | 10 | | |
| | | 11 | | namespace Gravitas.Constraints; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Runtime handle for a context-owned 3D ragdoll articulation. |
| | | 15 | | /// </summary> |
| | | 16 | | public sealed class RagdollRuntime3D : IRecordable |
| | | 17 | | { |
| | | 18 | | private readonly GravitasConstraint3DService _service; |
| | | 19 | | private readonly SolidBody[] _links; |
| | | 20 | | private readonly Joint3D[] _joints; |
| | | 21 | | private bool _isActive; |
| | | 22 | | |
| | 37 | 23 | | internal RagdollRuntime3D( |
| | 37 | 24 | | GravitasConstraint3DService service, |
| | 37 | 25 | | int id, |
| | 37 | 26 | | SolidBody[] links, |
| | 37 | 27 | | Joint3D[] joints, |
| | 37 | 28 | | RagdollSelfCollisionPolicy selfCollisionPolicy, |
| | 37 | 29 | | bool startsActive) |
| | | 30 | | { |
| | 37 | 31 | | _service = service; |
| | 37 | 32 | | Id = id; |
| | 37 | 33 | | _links = links; |
| | 37 | 34 | | _joints = joints; |
| | 37 | 35 | | SelfCollisionPolicy = selfCollisionPolicy; |
| | 37 | 36 | | IsRegistered = true; |
| | 37 | 37 | | ApplyActivationState(startsActive, emitDiagnostics: false); |
| | 37 | 38 | | } |
| | | 39 | | |
| | 4 | 40 | | internal GravitasConstraint3DService Service => _service; |
| | | 41 | | |
| | | 42 | | internal RagdollRuntime3D? PreviousRegistered { get; set; } |
| | | 43 | | |
| | | 44 | | internal RagdollRuntime3D? NextRegistered { get; set; } |
| | | 45 | | |
| | | 46 | | /// <summary> |
| | | 47 | | /// Gets this context-local ragdoll ID. |
| | | 48 | | /// </summary> |
| | | 49 | | public int Id { get; } |
| | | 50 | | |
| | | 51 | | /// <summary> |
| | | 52 | | /// Gets ragdoll-internal collision filtering behavior. |
| | | 53 | | /// </summary> |
| | | 54 | | public RagdollSelfCollisionPolicy SelfCollisionPolicy { get; } |
| | | 55 | | |
| | | 56 | | /// <summary> |
| | | 57 | | /// Gets whether this articulation remains registered with its owning constraint service. |
| | | 58 | | /// </summary> |
| | | 59 | | public bool IsRegistered { get; private set; } |
| | | 60 | | |
| | | 61 | | /// <summary> |
| | | 62 | | /// Gets whether the ragdoll links are currently dynamic. |
| | | 63 | | /// </summary> |
| | 20 | 64 | | public bool IsActive => _isActive; |
| | | 65 | | |
| | | 66 | | /// <summary> |
| | | 67 | | /// Gets the number of links in this ragdoll. |
| | | 68 | | /// </summary> |
| | 55 | 69 | | public int LinkCount => _links.Length; |
| | | 70 | | |
| | | 71 | | /// <summary> |
| | | 72 | | /// Gets the number of joints in this ragdoll. |
| | | 73 | | /// </summary> |
| | 42 | 74 | | public int JointCount => _joints.Length; |
| | | 75 | | |
| | | 76 | | /// <summary> |
| | | 77 | | /// Gets a ragdoll link by runtime index. |
| | | 78 | | /// </summary> |
| | 25 | 79 | | public SolidBody GetLink(int index) => _links[index]; |
| | | 80 | | |
| | | 81 | | /// <summary> |
| | | 82 | | /// Gets a ragdoll joint by runtime index. |
| | | 83 | | /// </summary> |
| | 51 | 84 | | public Joint3D GetJoint(int index) => _joints[index]; |
| | | 85 | | |
| | | 86 | | /// <summary> |
| | | 87 | | /// Switches all links to dynamic simulation and enables ragdoll joints. |
| | | 88 | | /// </summary> |
| | | 89 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 6 | 90 | | public void ActivateDynamic() => ApplyActivationState(isActive: true, emitDiagnostics: true); |
| | | 91 | | |
| | | 92 | | /// <summary> |
| | | 93 | | /// Switches all links to kinematic host control and disables ragdoll joints. |
| | | 94 | | /// </summary> |
| | | 95 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 4 | 96 | | public void DeactivateToKinematic() => ApplyActivationState(isActive: false, emitDiagnostics: true); |
| | | 97 | | |
| | | 98 | | /// <summary> |
| | | 99 | | /// Records runtime activation state for deterministic continuation. |
| | | 100 | | /// </summary> |
| | | 101 | | public void RecordData(IChronicler chronicler) |
| | | 102 | | { |
| | 7 | 103 | | SwiftThrowHelper.ThrowIfTrue( |
| | 7 | 104 | | !IsRegistered, |
| | 7 | 105 | | nameof(RagdollRuntime3D), |
| | 7 | 106 | | "Removed ragdolls cannot participate in serialization."); |
| | 5 | 107 | | bool isActive = _isActive; |
| | 5 | 108 | | RecordValues.Look(chronicler, ref isActive, "IsActive", false); |
| | 5 | 109 | | if (chronicler.Mode != SerializationMode.Loading) |
| | 3 | 110 | | return; |
| | | 111 | | |
| | 2 | 112 | | ApplyActivationState(isActive, emitDiagnostics: true); |
| | 2 | 113 | | } |
| | | 114 | | |
| | | 115 | | private void ApplyActivationState(bool isActive, bool emitDiagnostics) |
| | | 116 | | { |
| | 49 | 117 | | SwiftThrowHelper.ThrowIfTrue( |
| | 49 | 118 | | !IsRegistered, |
| | 49 | 119 | | nameof(RagdollRuntime3D), |
| | 49 | 120 | | "Removed ragdolls cannot mutate simulation state."); |
| | | 121 | | |
| | 45 | 122 | | BodyMotionType targetMotionType = isActive |
| | 45 | 123 | | ? BodyMotionType.Dynamic |
| | 45 | 124 | | : BodyMotionType.Kinematic; |
| | | 125 | | |
| | | 126 | | // Validate every link and publish every required host pose before any |
| | | 127 | | // runtime role or joint state changes. This keeps the ragdoll atomic if |
| | | 128 | | // a host transform cannot represent one of the authoritative poses. |
| | 308 | 129 | | for (int i = 0; i < _links.Length; i++) |
| | 110 | 130 | | _links[i].PrepareMotionTypeTransition(targetMotionType); |
| | | 131 | | |
| | 304 | 132 | | for (int i = 0; i < _links.Length; i++) |
| | | 133 | | { |
| | 108 | 134 | | if (_links[i].MotionType != targetMotionType) |
| | 16 | 135 | | _links[i].CommitMotionTypeTransition(targetMotionType); |
| | | 136 | | |
| | 108 | 137 | | if (isActive) |
| | 91 | 138 | | _links[i].Wake(); |
| | | 139 | | } |
| | | 140 | | |
| | 44 | 141 | | if (isActive) |
| | | 142 | | { |
| | 182 | 143 | | for (int i = 0; i < _joints.Length; i++) |
| | 56 | 144 | | _joints[i].IsEnabled = true; |
| | | 145 | | } |
| | | 146 | | else |
| | | 147 | | { |
| | 34 | 148 | | for (int i = 0; i < _joints.Length; i++) |
| | 8 | 149 | | _joints[i].IsEnabled = false; |
| | | 150 | | } |
| | | 151 | | |
| | 44 | 152 | | _isActive = isActive; |
| | 44 | 153 | | if (emitDiagnostics) |
| | 7 | 154 | | _links[0].Context.Diagnostics.EmitRagdollActivated(Id, LinkCount, JointCount, isActive); |
| | 44 | 155 | | } |
| | | 156 | | |
| | | 157 | | internal void Invalidate() |
| | | 158 | | { |
| | 37 | 159 | | IsRegistered = false; |
| | 37 | 160 | | _isActive = false; |
| | 37 | 161 | | PreviousRegistered = null; |
| | 37 | 162 | | NextRegistered = null; |
| | 37 | 163 | | } |
| | | 164 | | } |