| | | 1 | | using Chronicler; |
| | | 2 | | using FixedMathSharp; |
| | | 3 | | using Trailblazer.Support; |
| | | 4 | | |
| | | 5 | | namespace Trailblazer.Navigation.Motor; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Stores climb locomotion configuration and runtime attachment state. |
| | | 9 | | /// </summary> |
| | | 10 | | public class ClimbLocomotion : ILocomotion |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// Default maximum speed while climbing. |
| | | 14 | | /// </summary> |
| | 1 | 15 | | public static readonly Fixed64 DefaultMaxClimbSpeed = Fixed64.One; |
| | | 16 | | |
| | | 17 | | /// <summary> |
| | | 18 | | /// Default maximum acceleration while climbing. |
| | | 19 | | /// </summary> |
| | 1 | 20 | | public static readonly Fixed64 DefaultMaxClimbAcceleration = (Fixed64)8; |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// Default gravity compensation applied while attached to a climb affordance. |
| | | 24 | | /// </summary> |
| | 1 | 25 | | public static readonly Fixed64 DefaultGravityCompensationWhileClimbing = Fixed64.One; |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// Default positional tolerance used when validating attachment continuity without a stable affordance id. |
| | | 29 | | /// </summary> |
| | 1 | 30 | | public static readonly Fixed64 DefaultClimbStartTolerance = Fixed64.Half; |
| | | 31 | | |
| | 790 | 32 | | private bool _isEnabled = true; |
| | | 33 | | |
| | | 34 | | /// <inheritdoc cref="ILocomotion.IsEnabled"/> |
| | | 35 | | public bool IsEnabled |
| | | 36 | | { |
| | 4205 | 37 | | get => _isEnabled; |
| | | 38 | | set |
| | | 39 | | { |
| | 1 | 40 | | _isEnabled = value; |
| | 1 | 41 | | if (!_isEnabled) |
| | 1 | 42 | | this.ClearTransientState(); |
| | 1 | 43 | | } |
| | | 44 | | } |
| | | 45 | | |
| | | 46 | | /// <summary> |
| | | 47 | | /// Determines whether this locomotion may engage with climb affordances. |
| | | 48 | | /// </summary> |
| | 790 | 49 | | public bool CanClimb = true; |
| | | 50 | | |
| | | 51 | | /// <summary> |
| | | 52 | | /// Maximum climb speed while attached. |
| | | 53 | | /// </summary> |
| | 790 | 54 | | public Fixed64 MaxClimbSpeed = DefaultMaxClimbSpeed; |
| | | 55 | | |
| | | 56 | | /// <summary> |
| | | 57 | | /// Maximum acceleration while attached. |
| | | 58 | | /// </summary> |
| | 790 | 59 | | public Fixed64 MaxClimbAcceleration = DefaultMaxClimbAcceleration; |
| | | 60 | | |
| | | 61 | | /// <summary> |
| | | 62 | | /// Amount of gravity canceled while actively climbing. |
| | | 63 | | /// </summary> |
| | 790 | 64 | | public Fixed64 GravityCompensationWhileClimbing = DefaultGravityCompensationWhileClimbing; |
| | | 65 | | |
| | | 66 | | /// <summary> |
| | | 67 | | /// Positional tolerance used for initial attach and attachment continuity checks. |
| | | 68 | | /// </summary> |
| | 790 | 69 | | public Fixed64 ClimbStartTolerance = DefaultClimbStartTolerance; |
| | | 70 | | |
| | | 71 | | /// <summary> |
| | | 72 | | /// Whether lateral traverse across a climb surface is allowed. |
| | | 73 | | /// </summary> |
| | 790 | 74 | | public bool AllowLateralTraverse = true; |
| | | 75 | | |
| | | 76 | | /// <summary> |
| | | 77 | | /// Whether active mantle should query an optional host validator each frame. |
| | | 78 | | /// </summary> |
| | | 79 | | public bool ValidateActiveMantleWithHost; |
| | | 80 | | |
| | | 81 | | /// <summary> |
| | | 82 | | /// Optional host-owned resolver that supplies deterministic climb affordance snapshots. |
| | | 83 | | /// </summary> |
| | | 84 | | public IClimbAffordanceResolver? ClimbResolver; |
| | | 85 | | |
| | | 86 | | /// <summary> |
| | | 87 | | /// Whether active climb movement is currently attached to an affordance. |
| | | 88 | | /// </summary> |
| | | 89 | | [Transient] |
| | | 90 | | public bool IsClimbing { get; set; } |
| | | 91 | | |
| | | 92 | | /// <summary> |
| | | 93 | | /// Whether the locomotion is currently in a mantle/top-out phase. |
| | | 94 | | /// </summary> |
| | | 95 | | [Transient] |
| | | 96 | | public bool IsMantling { get; set; } |
| | | 97 | | |
| | | 98 | | /// <summary> |
| | | 99 | | /// Kind of affordance currently attached, when active. |
| | | 100 | | /// </summary> |
| | | 101 | | [Transient] |
| | | 102 | | public ClimbAffordanceKind ActiveClimbKind { get; set; } |
| | | 103 | | |
| | | 104 | | /// <summary> |
| | | 105 | | /// Stable host affordance identity when available. |
| | | 106 | | /// </summary> |
| | | 107 | | [Transient] |
| | | 108 | | public int? AttachmentId { get; set; } |
| | | 109 | | |
| | | 110 | | /// <summary> |
| | | 111 | | /// Stored attachment point for the current climb interaction. |
| | | 112 | | /// </summary> |
| | | 113 | | [Transient] |
| | | 114 | | public Vector3d AttachmentPoint { get; set; } |
| | | 115 | | |
| | | 116 | | /// <summary> |
| | | 117 | | /// Stored attachment surface normal for the current climb interaction. |
| | | 118 | | /// </summary> |
| | | 119 | | [Transient] |
| | | 120 | | public Vector3d AttachedSurfaceNormal { get; set; } |
| | | 121 | | |
| | | 122 | | /// <summary> |
| | | 123 | | /// Stored climb up direction for the current climb interaction. |
| | | 124 | | /// </summary> |
| | | 125 | | [Transient] |
| | | 126 | | public Vector3d AttachedUpDirection { get; set; } |
| | | 127 | | |
| | | 128 | | /// <summary> |
| | | 129 | | /// Whether lateral movement is allowed for the active frame snapshot. |
| | | 130 | | /// </summary> |
| | | 131 | | [Transient] |
| | | 132 | | public bool ActiveAllowLateralTraverse { get; set; } |
| | | 133 | | |
| | | 134 | | /// <summary> |
| | | 135 | | /// Whether descent is allowed for the active frame snapshot. |
| | | 136 | | /// </summary> |
| | | 137 | | [Transient] |
| | | 138 | | public bool ActiveAllowDescent { get; set; } |
| | | 139 | | |
| | | 140 | | /// <summary> |
| | | 141 | | /// Whether detaching via jump is allowed for the active frame snapshot. |
| | | 142 | | /// </summary> |
| | | 143 | | [Transient] |
| | | 144 | | public bool ActiveAllowDetachJump { get; set; } |
| | | 145 | | |
| | | 146 | | /// <summary> |
| | | 147 | | /// Whether mantling is allowed for the active frame snapshot. |
| | | 148 | | /// </summary> |
| | | 149 | | [Transient] |
| | | 150 | | public bool ActiveAllowMantle { get; set; } |
| | | 151 | | |
| | | 152 | | /// <summary> |
| | | 153 | | /// Current mantle target when a ledge affordance supplies one. |
| | | 154 | | /// </summary> |
| | | 155 | | [Transient] |
| | | 156 | | public Vector3d? MantleTargetPosition { get; set; } |
| | | 157 | | |
| | | 158 | | /// <summary> |
| | | 159 | | /// Applies a climb affordance snapshot to update the active attachment state for the current frame. |
| | | 160 | | /// </summary> |
| | | 161 | | /// <param name="snapshot"></param> |
| | | 162 | | public void ApplyClimbSnapshot(ClimbAffordanceSnapshot snapshot) |
| | | 163 | | { |
| | 38 | 164 | | ActiveClimbKind = snapshot.Kind; |
| | 38 | 165 | | AttachmentId = snapshot.AffordanceId; |
| | 38 | 166 | | AttachmentPoint = snapshot.AttachmentPoint; |
| | 38 | 167 | | AttachedSurfaceNormal = snapshot.SurfaceNormal; |
| | 38 | 168 | | AttachedUpDirection = snapshot.UpDirection; |
| | 38 | 169 | | ActiveAllowLateralTraverse = snapshot.AllowLateralTraverse && AllowLateralTraverse; |
| | 38 | 170 | | ActiveAllowDescent = snapshot.AllowDescent; |
| | 38 | 171 | | ActiveAllowDetachJump = snapshot.AllowDetachJump; |
| | 38 | 172 | | ActiveAllowMantle = snapshot.AllowMantle && snapshot.MantleTargetPosition.HasValue; |
| | 38 | 173 | | MantleTargetPosition = snapshot.MantleTargetPosition; |
| | 38 | 174 | | } |
| | | 175 | | |
| | | 176 | | /// <summary> |
| | | 177 | | /// Creates a readonly snapshot for optional active mantle validation. |
| | | 178 | | /// </summary> |
| | | 179 | | public ActiveMantleState CreateActiveMantleState() |
| | | 180 | | { |
| | 4 | 181 | | return new ActiveMantleState( |
| | 4 | 182 | | ActiveClimbKind, |
| | 4 | 183 | | AttachmentId, |
| | 4 | 184 | | AttachmentPoint, |
| | 4 | 185 | | AttachedSurfaceNormal, |
| | 4 | 186 | | AttachedUpDirection, |
| | 4 | 187 | | MantleTargetPosition ?? AttachmentPoint); |
| | | 188 | | } |
| | | 189 | | |
| | | 190 | | /// <inheritdoc /> |
| | | 191 | | public void RecordData(IChronicler chronicler) |
| | | 192 | | { |
| | 104 | 193 | | RecordValues.Look(chronicler, ref _isEnabled, "IsEnabled", true); |
| | 104 | 194 | | RecordValues.Look(chronicler, ref CanClimb, "CanClimb", true); |
| | 104 | 195 | | RecordValues.Look(chronicler, ref MaxClimbSpeed, "MaxClimbSpeed", DefaultMaxClimbSpeed); |
| | 104 | 196 | | RecordValues.Look(chronicler, ref MaxClimbAcceleration, "MaxClimbAcceleration", DefaultMaxClimbAcceleration); |
| | 104 | 197 | | RecordValues.Look(chronicler, ref GravityCompensationWhileClimbing, "GravityCompensationWhileClimbing", DefaultG |
| | 104 | 198 | | RecordValues.Look(chronicler, ref ClimbStartTolerance, "ClimbStartTolerance", DefaultClimbStartTolerance); |
| | 104 | 199 | | RecordValues.Look(chronicler, ref AllowLateralTraverse, "AllowLateralTraverse", true); |
| | 104 | 200 | | RecordValues.Look(chronicler, ref ValidateActiveMantleWithHost, "ValidateActiveMantleWithHost", false); |
| | | 201 | | |
| | 104 | 202 | | bool isClimbing = IsClimbing; |
| | 104 | 203 | | bool isMantling = IsMantling; |
| | 104 | 204 | | ClimbAffordanceKind activeClimbKind = ActiveClimbKind; |
| | 104 | 205 | | int? attachmentId = AttachmentId; |
| | 104 | 206 | | Vector3d attachmentPoint = AttachmentPoint; |
| | 104 | 207 | | Vector3d attachedSurfaceNormal = AttachedSurfaceNormal; |
| | 104 | 208 | | Vector3d attachedUpDirection = AttachedUpDirection; |
| | 104 | 209 | | bool activeAllowLateralTraverse = ActiveAllowLateralTraverse; |
| | 104 | 210 | | bool activeAllowDescent = ActiveAllowDescent; |
| | 104 | 211 | | bool activeAllowDetachJump = ActiveAllowDetachJump; |
| | 104 | 212 | | bool activeAllowMantle = ActiveAllowMantle; |
| | 104 | 213 | | Vector3d? mantleTargetPosition = MantleTargetPosition; |
| | | 214 | | |
| | 104 | 215 | | RecordValues.Look(chronicler, ref isClimbing, "IsClimbing", false); |
| | 104 | 216 | | RecordValues.Look(chronicler, ref isMantling, "IsMantling", false); |
| | 104 | 217 | | RecordValues.Look(chronicler, ref activeClimbKind, "ActiveClimbKind", ClimbAffordanceKind.None); |
| | 104 | 218 | | RecordValues.Look(chronicler, ref attachmentId, "AttachmentId", null); |
| | 104 | 219 | | RecordValues.Look(chronicler, ref attachmentPoint, "AttachmentPoint", Vector3d.Zero); |
| | 104 | 220 | | RecordValues.Look(chronicler, ref attachedSurfaceNormal, "AttachedSurfaceNormal", Vector3d.Zero); |
| | 104 | 221 | | RecordValues.Look(chronicler, ref attachedUpDirection, "AttachedUpDirection", Vector3d.Zero); |
| | 104 | 222 | | RecordValues.Look(chronicler, ref activeAllowLateralTraverse, "ActiveAllowLateralTraverse", false); |
| | 104 | 223 | | RecordValues.Look(chronicler, ref activeAllowDescent, "ActiveAllowDescent", true); |
| | 104 | 224 | | RecordValues.Look(chronicler, ref activeAllowDetachJump, "ActiveAllowDetachJump", true); |
| | 104 | 225 | | RecordValues.Look(chronicler, ref activeAllowMantle, "ActiveAllowMantle", false); |
| | 104 | 226 | | RecordValues.Look(chronicler, ref mantleTargetPosition, "MantleTargetPosition", null); |
| | | 227 | | |
| | 104 | 228 | | if (chronicler.Mode == SerializationMode.Loading) |
| | | 229 | | { |
| | 52 | 230 | | IsClimbing = isClimbing; |
| | 52 | 231 | | IsMantling = isMantling; |
| | 52 | 232 | | ActiveClimbKind = activeClimbKind; |
| | 52 | 233 | | AttachmentId = attachmentId; |
| | 52 | 234 | | AttachmentPoint = attachmentPoint; |
| | 52 | 235 | | AttachedSurfaceNormal = attachedSurfaceNormal; |
| | 52 | 236 | | AttachedUpDirection = attachedUpDirection; |
| | 52 | 237 | | ActiveAllowLateralTraverse = activeAllowLateralTraverse; |
| | 52 | 238 | | ActiveAllowDescent = activeAllowDescent; |
| | 52 | 239 | | ActiveAllowDetachJump = activeAllowDetachJump; |
| | 52 | 240 | | ActiveAllowMantle = activeAllowMantle; |
| | 52 | 241 | | MantleTargetPosition = mantleTargetPosition; |
| | | 242 | | |
| | 52 | 243 | | if (!_isEnabled) |
| | 0 | 244 | | this.ClearTransientState(); |
| | | 245 | | } |
| | 104 | 246 | | } |
| | | 247 | | } |