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