| | | 1 | | using FixedMathSharp; |
| | | 2 | | using System; |
| | | 3 | | using System.Runtime.CompilerServices; |
| | | 4 | | |
| | | 5 | | namespace Trailblazer.Navigation.Motor; |
| | | 6 | | |
| | | 7 | | public partial class NavMotor |
| | | 8 | | { |
| | | 9 | | #region Phase 1 - Request Traversal |
| | | 10 | | |
| | | 11 | | /// <summary> |
| | | 12 | | /// Processes a movement request and applies necessary forces. |
| | | 13 | | /// </summary> |
| | | 14 | | /// <remarks> |
| | | 15 | | /// This method opens a traversal phase for the current frame so duplicate force accumulation is rejected |
| | | 16 | | /// until the host calls <see cref="FinalizeTraversal"/> or <see cref="AbortTraversalFrame"/>. |
| | | 17 | | /// Movement forces such as gravity, jump, and platform adjustments are applied. |
| | | 18 | | /// </remarks> |
| | | 19 | | /// <param name="request">The movement request containing desired movement parameters</param> |
| | | 20 | | /// <param name="velocityDelta">The resulting velocity change to apply to the object</param> |
| | | 21 | | /// <param name="positionDelta">The resulting position change from platform movement to apply to the object</param> |
| | | 22 | | /// <param name="rotationDelta">The resulting rotation change from platform movement to apply to the object</param> |
| | | 23 | | public bool TryTraversal( |
| | | 24 | | TrekRequest request, |
| | | 25 | | out Vector3d velocityDelta, |
| | | 26 | | out Vector3d positionDelta, |
| | | 27 | | out FixedQuaternion rotationDelta) |
| | | 28 | | { |
| | 2091 | 29 | | ResetTraversalOutputs(out velocityDelta, out positionDelta, out rotationDelta); |
| | 2091 | 30 | | if (!TryBeginTraversalFrame()) |
| | 2 | 31 | | return false; |
| | | 32 | | |
| | 2087 | 33 | | PrepareTraversalState(request); |
| | 2087 | 34 | | ResolveTraversalForces(request); |
| | | 35 | | |
| | 2087 | 36 | | velocityDelta = ResolveTraversalVelocityDelta(); |
| | 2087 | 37 | | ResolvePlatformTraversal(request, ref positionDelta, ref rotationDelta); |
| | 2087 | 38 | | return true; |
| | | 39 | | } |
| | | 40 | | |
| | | 41 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 42 | | private static void ResetTraversalOutputs( |
| | | 43 | | out Vector3d velocityDelta, |
| | | 44 | | out Vector3d positionDelta, |
| | | 45 | | out FixedQuaternion rotationDelta) |
| | | 46 | | { |
| | 2091 | 47 | | velocityDelta = Vector3d.Zero; |
| | 2091 | 48 | | positionDelta = Vector3d.Zero; |
| | 2091 | 49 | | rotationDelta = FixedQuaternion.Identity; |
| | 2091 | 50 | | } |
| | | 51 | | |
| | | 52 | | private bool TryBeginTraversalFrame() |
| | | 53 | | { |
| | 2091 | 54 | | if (!IsInitialized) |
| | 1 | 55 | | return false; |
| | | 56 | | |
| | 2090 | 57 | | if (TraversalInProgress) |
| | | 58 | | { |
| | 3 | 59 | | if (FrameCount != _pendingTraversalFrame) |
| | | 60 | | { |
| | 2 | 61 | | throw new InvalidOperationException( |
| | 2 | 62 | | $"NavMotor traversal from frame {_pendingTraversalFrame} was never finalized or aborted before frame |
| | | 63 | | } |
| | | 64 | | |
| | 1 | 65 | | return false; |
| | | 66 | | } |
| | | 67 | | |
| | 2087 | 68 | | TraversalInProgress = true; |
| | 2087 | 69 | | _pendingTraversalFrame = FrameCount; |
| | 2087 | 70 | | return true; |
| | | 71 | | } |
| | | 72 | | |
| | | 73 | | private void PrepareTraversalState(TrekRequest request) |
| | | 74 | | { |
| | 2087 | 75 | | TrailblazerLogger.DebugChannel.Info( |
| | 2087 | 76 | | $"NavMotor State: Grounded={IsOnSolid}, InAir={IsInGas}, InWater={IsInLiquid}, Flying={IsFlying}, InLimbo={I |
| | | 77 | | |
| | | 78 | | // Calculate the slope angle for the current frame based on the movement direction and surface normal. |
| | 2087 | 79 | | FrameSlopeAngle = CurrentState.GetSignedSlopeAngle(request.Direction); |
| | | 80 | | |
| | | 81 | | // Store the current velocity for manipulation. |
| | 2087 | 82 | | _forceOutput = Handler.Move.FrameVelocity; |
| | | 83 | | |
| | | 84 | | // Update platform velocity prior to applying jump force. |
| | 2087 | 85 | | PlatformModule.UpdatePlatformVelocity(); |
| | | 86 | | |
| | 2087 | 87 | | if (InLimbo) |
| | 31 | 88 | | Handler.IsInControl = false; |
| | | 89 | | |
| | 2087 | 90 | | if (JumpModule?.IsCoolingDown == true) |
| | 103 | 91 | | JumpModule.UpdateCooldown(); |
| | | 92 | | |
| | 2087 | 93 | | UpdateClimbState(request); |
| | 2087 | 94 | | UpdateFlightState(request); |
| | 2087 | 95 | | UpdateSwimState(request); |
| | 2087 | 96 | | } |
| | | 97 | | |
| | | 98 | | #region Traversal Force Resolution |
| | | 99 | | |
| | | 100 | | private void ResolveTraversalForces(TrekRequest request) |
| | | 101 | | { |
| | 2087 | 102 | | ComputeMovementForces(request); |
| | | 103 | | |
| | | 104 | | // Reset this before applying gravity. |
| | 2087 | 105 | | if (JumpModule != null && (!request.IsRequestingJump || !JumpModule.CanJump)) |
| | 2044 | 106 | | JumpModule.IsHoldingJump = false; |
| | | 107 | | |
| | 2087 | 108 | | ApplyEnvironmentalForces(); |
| | 2087 | 109 | | ApplyJumpForce(request); |
| | 2087 | 110 | | } |
| | | 111 | | |
| | | 112 | | /// <summary> |
| | | 113 | | /// Computes the movement forces based on the traversal state and applies them to the object. |
| | | 114 | | /// </summary> |
| | | 115 | | /// <remarks> |
| | | 116 | | /// This method determines whether the object is in control, calculates velocity adjustments, |
| | | 117 | | /// and applies constraints such as slope resistance and airborne drag. |
| | | 118 | | /// </remarks> |
| | | 119 | | private void ComputeMovementForces(TrekRequest frameRequest) |
| | | 120 | | { |
| | 2087 | 121 | | UpdateControlState(); |
| | | 122 | | |
| | 2087 | 123 | | if (InLimbo) |
| | 14 | 124 | | return; |
| | | 125 | | |
| | 2073 | 126 | | ResetDownwardMomentumIfNeeded(); |
| | | 127 | | |
| | 2073 | 128 | | Vector3d desiredVelocity = GetDesiredVelocity(frameRequest); |
| | | 129 | | |
| | 2073 | 130 | | if (TryApplyStationaryGroundFriction(frameRequest.Direction)) |
| | 5 | 131 | | return; |
| | | 132 | | |
| | 2068 | 133 | | ApplyDesiredVelocity(desiredVelocity); |
| | 2068 | 134 | | } |
| | | 135 | | |
| | | 136 | | private void UpdateControlState() |
| | | 137 | | { |
| | 2087 | 138 | | if (IsClimbing) |
| | | 139 | | { |
| | 36 | 140 | | SetSlidingState(false); |
| | 36 | 141 | | Handler.IsInControl = true; |
| | 36 | 142 | | return; |
| | | 143 | | } |
| | | 144 | | |
| | 2051 | 145 | | if (IsOnSolid) |
| | | 146 | | { |
| | 220 | 147 | | if (IsFlying) |
| | | 148 | | { |
| | 5 | 149 | | SetSlidingState(false); |
| | 5 | 150 | | Handler.IsInControl = true; |
| | | 151 | | } |
| | | 152 | | else |
| | | 153 | | { |
| | 215 | 154 | | bool isTooSteep = IsTooSteep(FrameSlopeAngle); |
| | 215 | 155 | | SetSlidingState(SlideModule?.IsEnabled == true && isTooSteep); |
| | 215 | 156 | | Handler.IsInControl = !isTooSteep; |
| | | 157 | | } |
| | | 158 | | |
| | 215 | 159 | | return; |
| | | 160 | | } |
| | | 161 | | |
| | 1831 | 162 | | Handler.IsInControl = IsFlying || !InLimbo; |
| | 1831 | 163 | | } |
| | | 164 | | |
| | | 165 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 166 | | private void SetSlidingState(bool isSliding) |
| | | 167 | | { |
| | 256 | 168 | | if (SlideModule != null) |
| | 254 | 169 | | SlideModule.IsSliding = isSliding; |
| | 256 | 170 | | } |
| | | 171 | | |
| | | 172 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 173 | | private void ResetDownwardMomentumIfNeeded() |
| | | 174 | | { |
| | 2073 | 175 | | if (IsClimbing) |
| | | 176 | | { |
| | 36 | 177 | | _forceOutput = Vector3d.Zero; |
| | 36 | 178 | | return; |
| | | 179 | | } |
| | | 180 | | |
| | 2037 | 181 | | if ((!IsOnSolid || WasInGas) && !IsFlying) |
| | 1815 | 182 | | _forceOutput.y = Fixed64.Zero; |
| | 2037 | 183 | | } |
| | | 184 | | |
| | | 185 | | /// <summary> |
| | | 186 | | /// Determines the desired velocity based on input direction, movement constraints, and traversal state. |
| | | 187 | | /// </summary> |
| | | 188 | | /// <returns>The computed velocity vector that the object should move toward.</returns> |
| | | 189 | | private Vector3d GetDesiredVelocity(TrekRequest frameRequest) |
| | | 190 | | { |
| | 2073 | 191 | | if (IsFlying) |
| | 10 | 192 | | return GetFlightVelocity(frameRequest); |
| | | 193 | | |
| | 2063 | 194 | | if (ClimbModule?.IsMantling == true) |
| | 12 | 195 | | return GetMantleVelocity(frameRequest.Origin); |
| | | 196 | | |
| | 2051 | 197 | | if (IsClimbing) |
| | 24 | 198 | | return GetClimbVelocity(frameRequest); |
| | | 199 | | |
| | 2027 | 200 | | Vector3d result = GetControlledSurfaceVelocity(frameRequest); |
| | 2027 | 201 | | if (IsInLiquid) |
| | 174 | 202 | | return ResolveLiquidVelocity(frameRequest, result); |
| | | 203 | | |
| | 1853 | 204 | | result = ApplyPlatformTransferVelocity(result); |
| | 1853 | 205 | | return ApplyGroundVelocityConstraints(result); |
| | | 206 | | } |
| | | 207 | | |
| | | 208 | | /// <summary> |
| | | 209 | | /// Computes the desired world-space velocity while controlled flight is active. |
| | | 210 | | /// </summary> |
| | | 211 | | private Vector3d GetFlightVelocity(TrekRequest frameRequest) |
| | | 212 | | { |
| | 14 | 213 | | if (FlyModule?.IsEnabled != true || !FlyModule.CanFly || frameRequest.Rate == TrekRate.Stationary) |
| | 3 | 214 | | return Vector3d.Zero; |
| | | 215 | | |
| | 11 | 216 | | Fixed64 speedMultiplier = GetFlightSpeedMultiplier(frameRequest.Rate); |
| | 11 | 217 | | if (speedMultiplier <= Fixed64.Zero) |
| | 1 | 218 | | return Vector3d.Zero; |
| | | 219 | | |
| | 10 | 220 | | Fixed3x3 transposedMatrix = frameRequest.Rotation.ToMatrix3x3(); |
| | 10 | 221 | | Vector3d desiredLocalDirection = Fixed3x3.InverseTransformDirection(transposedMatrix, frameRequest.Direction); |
| | 10 | 222 | | Vector3d desiredLocalVelocity = Vector3d.Zero; |
| | | 223 | | |
| | 10 | 224 | | Vector3d horizontalInput = new(desiredLocalDirection.x, Fixed64.Zero, desiredLocalDirection.z); |
| | 10 | 225 | | Fixed64 horizontalMagnitude = FixedMath.Clamp01(horizontalInput.Magnitude); |
| | 10 | 226 | | if (horizontalMagnitude > Fixed64.Zero) |
| | 3 | 227 | | desiredLocalVelocity += horizontalInput.Normal * (FlyModule.MaxFlySpeed * speedMultiplier * horizontalMagnit |
| | | 228 | | |
| | 10 | 229 | | Fixed64 verticalInput = FixedMath.Clamp(desiredLocalDirection.y, -Fixed64.One, Fixed64.One); |
| | 10 | 230 | | if (verticalInput > Fixed64.Zero) |
| | 7 | 231 | | desiredLocalVelocity.y = verticalInput * FlyModule.MaxAscendSpeed * speedMultiplier; |
| | 3 | 232 | | else if (verticalInput < Fixed64.Zero) |
| | 2 | 233 | | desiredLocalVelocity.y = verticalInput.Abs() * -FlyModule.MaxDescendSpeed * speedMultiplier; |
| | | 234 | | |
| | 10 | 235 | | return Fixed3x3.TransformDirection(transposedMatrix, desiredLocalVelocity); |
| | | 236 | | } |
| | | 237 | | |
| | | 238 | | private Vector3d GetClimbVelocity(TrekRequest frameRequest) |
| | | 239 | | { |
| | 24 | 240 | | if (ClimbModule?.IsEnabled != true |
| | 24 | 241 | | || !ClimbModule.CanClimb |
| | 24 | 242 | | || frameRequest.Rate == TrekRate.Stationary) |
| | | 243 | | { |
| | 1 | 244 | | return Vector3d.Zero; |
| | | 245 | | } |
| | | 246 | | |
| | 23 | 247 | | Vector3d upAxis = ClimbModule.AttachedUpDirection != Vector3d.Zero |
| | 23 | 248 | | ? ClimbModule.AttachedUpDirection.Normal |
| | 23 | 249 | | : Vector3d.Up; |
| | 23 | 250 | | Vector3d outwardNormal = ClimbModule.AttachedSurfaceNormal != Vector3d.Zero |
| | 23 | 251 | | ? ClimbModule.AttachedSurfaceNormal.Normal |
| | 23 | 252 | | : Vector3d.Backward; |
| | 23 | 253 | | Vector3d lateralAxis = Vector3d.Cross(upAxis, outwardNormal); |
| | 23 | 254 | | if (lateralAxis == Vector3d.Zero) |
| | 0 | 255 | | lateralAxis = Vector3d.Cross(Vector3d.Up, outwardNormal); |
| | 23 | 256 | | if (lateralAxis == Vector3d.Zero) |
| | 0 | 257 | | lateralAxis = Vector3d.Right; |
| | 23 | 258 | | lateralAxis = lateralAxis.Normal; |
| | | 259 | | |
| | 23 | 260 | | Fixed64 verticalAmount = Vector3d.Dot(frameRequest.Direction, upAxis); |
| | 23 | 261 | | if (!ClimbModule.ActiveAllowDescent && verticalAmount < Fixed64.Zero) |
| | 1 | 262 | | verticalAmount = Fixed64.Zero; |
| | | 263 | | |
| | 23 | 264 | | Fixed64 lateralAmount = ClimbModule.ActiveAllowLateralTraverse |
| | 23 | 265 | | ? Vector3d.Dot(frameRequest.Direction, lateralAxis) |
| | 23 | 266 | | : Fixed64.Zero; |
| | | 267 | | |
| | 23 | 268 | | Vector3d climbDirection = (upAxis * verticalAmount) + (lateralAxis * lateralAmount); |
| | 23 | 269 | | Fixed64 inputMagnitude = FixedMath.Clamp01(climbDirection.Magnitude); |
| | 23 | 270 | | if (inputMagnitude <= Fixed64.Zero) |
| | 4 | 271 | | return Vector3d.Zero; |
| | | 272 | | |
| | 19 | 273 | | return climbDirection.Normal * (ClimbModule.MaxClimbSpeed * inputMagnitude); |
| | | 274 | | } |
| | | 275 | | |
| | | 276 | | private Vector3d GetMantleVelocity(Vector3d origin) |
| | | 277 | | { |
| | 12 | 278 | | ClimbLocomotion? climbModule = ClimbModule; |
| | 12 | 279 | | if (climbModule?.MantleTargetPosition.HasValue != true) |
| | 0 | 280 | | return Vector3d.Zero; |
| | | 281 | | |
| | 12 | 282 | | Vector3d mantleTargetPosition = climbModule.MantleTargetPosition.GetValueOrDefault(); |
| | 12 | 283 | | Vector3d toTarget = mantleTargetPosition - origin; |
| | 12 | 284 | | if (toTarget == Vector3d.Zero) |
| | 0 | 285 | | return Vector3d.Zero; |
| | | 286 | | |
| | 12 | 287 | | Fixed64 distance = toTarget.Magnitude; |
| | 12 | 288 | | if (distance <= climbModule.ClimbStartTolerance) |
| | 0 | 289 | | return Vector3d.Zero; |
| | | 290 | | |
| | 12 | 291 | | return toTarget.Normal * climbModule.MaxClimbSpeed; |
| | | 292 | | } |
| | | 293 | | |
| | | 294 | | private Vector3d GetControlledSurfaceVelocity(TrekRequest frameRequest) |
| | | 295 | | { |
| | 2027 | 296 | | if (SlideModule?.IsSliding == true) |
| | 27 | 297 | | return GetSlidingVelocity(frameRequest); |
| | | 298 | | |
| | 2000 | 299 | | return Handler.IsInControl && frameRequest.Rate != TrekRate.Stationary |
| | 2000 | 300 | | ? GetHorizontalVelocity(frameRequest) |
| | 2000 | 301 | | : Vector3d.Zero; |
| | | 302 | | } |
| | | 303 | | |
| | | 304 | | private Vector3d GetSlidingVelocity(TrekRequest frameRequest) |
| | | 305 | | { |
| | 27 | 306 | | SlideLocomotion? slideModule = SlideModule; |
| | 27 | 307 | | if (slideModule == null) |
| | 0 | 308 | | return Vector3d.Zero; |
| | | 309 | | |
| | 27 | 310 | | Vector3d slideDirection = new Vector3d( |
| | 27 | 311 | | CurrentState.SurfaceNormal.x, |
| | 27 | 312 | | Fixed64.Zero, |
| | 27 | 313 | | CurrentState.SurfaceNormal.z).Normal; |
| | 27 | 314 | | Vector3d projectedMoveDir = Vector3d.Project(frameRequest.Direction, slideDirection); |
| | 27 | 315 | | Vector3d speedContribution = projectedMoveDir * slideModule.SpeedControl; |
| | 27 | 316 | | Vector3d sidewaysContribution = (frameRequest.Direction - projectedMoveDir) * slideModule.SidewaysControl; |
| | 27 | 317 | | return slideDirection + ((speedContribution + sidewaysContribution) * slideModule.SlidingSpeed); |
| | | 318 | | } |
| | | 319 | | |
| | | 320 | | private Vector3d GetHorizontalVelocity(TrekRequest frameRequest) |
| | | 321 | | { |
| | 138 | 322 | | Fixed3x3 transposedMatrix = frameRequest.Rotation.ToMatrix3x3(); |
| | 138 | 323 | | Vector3d desiredLocalDirection = Fixed3x3.InverseTransformDirection(transposedMatrix, frameRequest.Direction); |
| | 138 | 324 | | Fixed64 speed = MaxHoritzontalSpeedInDirection(desiredLocalDirection, frameRequest.Rate); |
| | | 325 | | |
| | 138 | 326 | | speed *= Handler.Move.MoveSpeedMultiplier; |
| | | 327 | | |
| | | 328 | | // Modify max speed on slopes based on slope speed multiplier curve |
| | 138 | 329 | | if (Handler.Move.ModifySpeedOnSlope && IsOnSlope(FrameSlopeAngle)) |
| | 16 | 330 | | speed *= Handler.Move.SlopeSpeedMultiplier.Evaluate(FrameSlopeAngle); |
| | | 331 | | |
| | 138 | 332 | | return Fixed3x3.TransformDirection(transposedMatrix, desiredLocalDirection * speed); |
| | | 333 | | } |
| | | 334 | | |
| | | 335 | | /// <summary> |
| | | 336 | | /// Calculates the maximum allowable speed in a given movement direction. |
| | | 337 | | /// </summary> |
| | | 338 | | /// <param name="desiredMovementDirection">The movement direction to evaluate.</param> |
| | | 339 | | /// <param name="rate">The rate that dictates the maximum speed allowed.</param> |
| | | 340 | | /// <returns>The maximum speed possible in the specified direction.</returns> |
| | | 341 | | public Fixed64 MaxHoritzontalSpeedInDirection(Vector3d desiredMovementDirection, TrekRate rate) |
| | | 342 | | { |
| | 157 | 343 | | if (desiredMovementDirection == Vector3d.Zero) |
| | 18 | 344 | | return Fixed64.Zero; |
| | | 345 | | |
| | 139 | 346 | | if (IsFlying) |
| | 9 | 347 | | return GetFlightHorizontalSpeed(desiredMovementDirection, rate); |
| | | 348 | | |
| | 130 | 349 | | if (IsInLiquid) |
| | 19 | 350 | | return GetLiquidHorizontalSpeed(desiredMovementDirection); |
| | | 351 | | |
| | 111 | 352 | | return GetGroundHorizontalSpeed(desiredMovementDirection, rate); |
| | | 353 | | } |
| | | 354 | | |
| | | 355 | | private Fixed64 GetFlightHorizontalSpeed(Vector3d desiredMovementDirection, TrekRate rate) |
| | | 356 | | { |
| | 12 | 357 | | if (FlyModule?.IsEnabled != true || !FlyModule.CanFly) |
| | 3 | 358 | | return Fixed64.Zero; |
| | | 359 | | |
| | 9 | 360 | | Fixed64 horizontalMagnitude = FixedMath.Clamp01(new Vector3d( |
| | 9 | 361 | | desiredMovementDirection.x, |
| | 9 | 362 | | Fixed64.Zero, |
| | 9 | 363 | | desiredMovementDirection.z).Magnitude); |
| | 9 | 364 | | return horizontalMagnitude * FlyModule.MaxFlySpeed * GetFlightSpeedMultiplier(rate); |
| | | 365 | | } |
| | | 366 | | |
| | | 367 | | private Fixed64 GetFlightSpeedMultiplier(TrekRate rate) |
| | | 368 | | { |
| | 20 | 369 | | if (rate == TrekRate.Stationary) |
| | 2 | 370 | | return Fixed64.Zero; |
| | | 371 | | |
| | 18 | 372 | | Fixed64 maxFastSpeed = Handler.Move.MaxFastSpeed; |
| | 18 | 373 | | if (rate == TrekRate.Fast || maxFastSpeed <= Fixed64.Zero) |
| | 12 | 374 | | return Fixed64.One; |
| | | 375 | | |
| | 6 | 376 | | return FixedMath.Clamp(GetScaledFlightSpeedMultiplier(rate, maxFastSpeed), Fixed64.Zero, Fixed64.One); |
| | | 377 | | } |
| | | 378 | | |
| | | 379 | | private Fixed64 GetLiquidHorizontalSpeed(Vector3d desiredMovementDirection) |
| | | 380 | | { |
| | 19 | 381 | | if (WaterModule?.IsEnabled != true |
| | 19 | 382 | | || !WaterModule.CanSwim |
| | 19 | 383 | | || !WaterModule.IsSwimming) |
| | | 384 | | { |
| | 1 | 385 | | return Fixed64.Zero; |
| | | 386 | | } |
| | | 387 | | |
| | 18 | 388 | | Fixed64 ellipseMultiplier = WaterModule.MaxSwimSpeed / WaterModule.MaxSwimSidewaysSpeed; |
| | 18 | 389 | | if (ellipseMultiplier <= Fixed64.Zero) |
| | 1 | 390 | | return Fixed64.Zero; |
| | | 391 | | |
| | 17 | 392 | | return GetEllipticalHorizontalSpeed( |
| | 17 | 393 | | desiredMovementDirection, |
| | 17 | 394 | | ellipseMultiplier, |
| | 17 | 395 | | WaterModule.MaxSwimSidewaysSpeed, |
| | 17 | 396 | | Fixed64.One); |
| | | 397 | | } |
| | | 398 | | |
| | | 399 | | private Fixed64 GetGroundHorizontalSpeed(Vector3d desiredMovementDirection, TrekRate rate) |
| | | 400 | | { |
| | 111 | 401 | | Fixed64 maxSpeed = GetGroundDirectionalMaxSpeed(desiredMovementDirection, rate); |
| | 111 | 402 | | Fixed64 ellipseMultiplier = maxSpeed / Handler.Move.MaxSidewaysSpeed; |
| | 111 | 403 | | if (ellipseMultiplier <= Fixed64.Zero) |
| | 1 | 404 | | return Fixed64.Zero; |
| | | 405 | | |
| | 110 | 406 | | return GetEllipticalHorizontalSpeed( |
| | 110 | 407 | | desiredMovementDirection, |
| | 110 | 408 | | ellipseMultiplier, |
| | 110 | 409 | | Handler.Move.MaxSidewaysSpeed, |
| | 110 | 410 | | GetAirborneControlMultiplier()); |
| | | 411 | | } |
| | | 412 | | |
| | | 413 | | private Fixed64 GetAirborneControlMultiplier() |
| | | 414 | | { |
| | 110 | 415 | | if (IsOnSolid) |
| | 86 | 416 | | return Fixed64.One; |
| | | 417 | | |
| | 24 | 418 | | if (IsJumping) |
| | 2 | 419 | | return JumpModule?.JumpControlMultiplier ?? Fixed64.One; |
| | | 420 | | |
| | 22 | 421 | | if (IsFalling) |
| | 21 | 422 | | return Handler.Fall.FallControlMultiplier; |
| | | 423 | | |
| | 1 | 424 | | return Fixed64.One; |
| | | 425 | | } |
| | | 426 | | |
| | | 427 | | private static Fixed64 GetEllipticalHorizontalSpeed( |
| | | 428 | | Vector3d desiredMovementDirection, |
| | | 429 | | Fixed64 zAxisEllipseMultiplier, |
| | | 430 | | Fixed64 sidewaysSpeed, |
| | | 431 | | Fixed64 controlMultiplier) |
| | | 432 | | { |
| | 127 | 433 | | Vector3d normalized = new Vector3d( |
| | 127 | 434 | | desiredMovementDirection.x, |
| | 127 | 435 | | Fixed64.Zero, |
| | 127 | 436 | | desiredMovementDirection.z / zAxisEllipseMultiplier).Normal; |
| | 127 | 437 | | Fixed64 length = new Vector3d( |
| | 127 | 438 | | normalized.x, |
| | 127 | 439 | | Fixed64.Zero, |
| | 127 | 440 | | normalized.z * zAxisEllipseMultiplier).Magnitude; |
| | 127 | 441 | | return length * sidewaysSpeed * controlMultiplier; |
| | | 442 | | } |
| | | 443 | | |
| | | 444 | | private Fixed64 GetGroundDirectionalMaxSpeed(Vector3d desiredMovementDirection, TrekRate rate) |
| | | 445 | | { |
| | 111 | 446 | | if (desiredMovementDirection.z < Fixed64.Zero) |
| | 2 | 447 | | return Handler.Move.MaxBackwardsSpeed; |
| | | 448 | | |
| | 109 | 449 | | return rate switch |
| | 109 | 450 | | { |
| | 35 | 451 | | TrekRate.Slow => Handler.Move.MaxSlowSpeed, |
| | 59 | 452 | | TrekRate.Moderate => Handler.Move.MaxModerateSpeed, |
| | 14 | 453 | | TrekRate.Fast => Handler.Move.MaxFastSpeed, |
| | 1 | 454 | | _ => Fixed64.Zero |
| | 109 | 455 | | }; |
| | | 456 | | } |
| | | 457 | | |
| | | 458 | | private Vector3d ResolveLiquidVelocity(TrekRequest frameRequest, Vector3d desiredVelocity) |
| | | 459 | | { |
| | 174 | 460 | | if (WaterModule?.IsEnabled != true |
| | 174 | 461 | | || !WaterModule.CanSwim |
| | 174 | 462 | | || !WaterModule.IsSwimming) |
| | | 463 | | { |
| | 154 | 464 | | desiredVelocity = Vector3d.Zero; |
| | | 465 | | } |
| | | 466 | | |
| | 174 | 467 | | if (WaterModule?.IsSwimming == true && frameRequest.Direction.y != Fixed64.Zero) |
| | 10 | 468 | | desiredVelocity.y = frameRequest.Direction.y * WaterModule.MaxSwimSpeed; |
| | | 469 | | |
| | 174 | 470 | | if (desiredVelocity != Vector3d.Zero) |
| | 16 | 471 | | desiredVelocity *= FixedMath.Clamp01(Fixed64.One - Handler.Move.WaterDragFactor); |
| | | 472 | | |
| | 174 | 473 | | return desiredVelocity; |
| | | 474 | | } |
| | | 475 | | |
| | | 476 | | private Vector3d ApplyPlatformTransferVelocity(Vector3d desiredVelocity) |
| | | 477 | | { |
| | 1854 | 478 | | PlatformLocomotion platformModule = PlatformModule; |
| | 1854 | 479 | | if (platformModule.IsEnabled |
| | 1854 | 480 | | && platformModule.MovementTransfer == MotionTransfer.PermaTransfer) |
| | | 481 | | { |
| | 1 | 482 | | desiredVelocity += platformModule.FramePlatformVelocity; |
| | 1 | 483 | | desiredVelocity.y = Fixed64.Zero; |
| | | 484 | | } |
| | | 485 | | |
| | 1854 | 486 | | return desiredVelocity; |
| | | 487 | | } |
| | | 488 | | |
| | | 489 | | private Vector3d ApplyGroundVelocityConstraints(Vector3d desiredVelocity) |
| | | 490 | | { |
| | 1856 | 491 | | if (!IsOnSolid || desiredVelocity == Vector3d.Zero) |
| | 1743 | 492 | | return desiredVelocity; |
| | | 493 | | |
| | 113 | 494 | | Fixed64 surfaceFriction = CurrentState.GroundState?.SurfaceFriction ?? Fixed64.Zero; |
| | 113 | 495 | | desiredVelocity *= Fixed64.One - surfaceFriction; |
| | | 496 | | |
| | | 497 | | // Flat or host-defined "solid but no sampled normal" surfaces should preserve the raw ground vector. |
| | | 498 | | // Sliding also already produces a slope-aware direction, so re-projecting it here distorts the result. |
| | 113 | 499 | | if (CurrentState.SurfaceNormal == Vector3d.Zero |
| | 113 | 500 | | || CurrentState.SlopeAngle == Fixed64.Zero |
| | 113 | 501 | | || SlideModule?.IsSliding == true) |
| | | 502 | | { |
| | 96 | 503 | | return desiredVelocity; |
| | | 504 | | } |
| | | 505 | | |
| | 17 | 506 | | Vector3d sideways = Vector3d.Cross(Vector3d.Up, desiredVelocity); |
| | 17 | 507 | | Vector3d adjustedVelocity = Vector3d.Cross(sideways, CurrentState.SurfaceNormal).Normal * desiredVelocity.Magnit |
| | 17 | 508 | | if (Fixed64.Sign(adjustedVelocity.y) != Fixed64.Sign(FrameSlopeAngle)) |
| | 17 | 509 | | adjustedVelocity.y *= -1; |
| | | 510 | | |
| | 17 | 511 | | return adjustedVelocity; |
| | | 512 | | } |
| | | 513 | | |
| | | 514 | | private bool TryApplyStationaryGroundFriction(Vector3d desiredDirection) |
| | | 515 | | { |
| | 2076 | 516 | | if (!IsOnSolid || IsFlying || desiredDirection != Vector3d.Zero) |
| | 1944 | 517 | | return false; |
| | | 518 | | |
| | 132 | 519 | | Fixed64 friction = CurrentState.GroundState?.SurfaceFriction ?? Fixed64.Zero; |
| | 132 | 520 | | if (friction <= Fixed64.Zero || _forceOutput == Vector3d.Zero) |
| | 126 | 521 | | return false; |
| | | 522 | | |
| | 6 | 523 | | _forceOutput *= Fixed64.One - friction; |
| | 6 | 524 | | return true; |
| | | 525 | | } |
| | | 526 | | |
| | | 527 | | private void ApplyDesiredVelocity(Vector3d desiredVelocity) |
| | | 528 | | { |
| | 2069 | 529 | | if (desiredVelocity == _forceOutput) |
| | 1904 | 530 | | return; |
| | | 531 | | |
| | 165 | 532 | | Fixed64 maxVelocityChange = GetMaxAcceleration() * DeltaTime; |
| | 165 | 533 | | Vector3d velocityChange = (desiredVelocity - _forceOutput).ClampMagnitude(maxVelocityChange); |
| | 165 | 534 | | if (!IsOnSolid && !Handler.IsInControl) |
| | 1 | 535 | | return; |
| | | 536 | | |
| | 164 | 537 | | _forceOutput += velocityChange; |
| | 164 | 538 | | if (IsOnSolid && !IsFlying) |
| | 92 | 539 | | _forceOutput.y = FixedMath.Min(_forceOutput.y, Fixed64.Zero); |
| | 164 | 540 | | } |
| | | 541 | | |
| | | 542 | | /// <summary> |
| | | 543 | | /// Retrieves the maximum acceleration value based on the object’s current traversal state. |
| | | 544 | | /// </summary> |
| | | 545 | | /// <returns>The acceleration limit depending on whether the object is grounded, airborne, or swimming.</returns> |
| | | 546 | | public Fixed64 GetMaxAcceleration() |
| | | 547 | | { |
| | 175 | 548 | | if (CurrentState == null) |
| | 1 | 549 | | throw new InvalidOperationException("NavMotor must be initialized before querying max acceleration."); |
| | | 550 | | |
| | 174 | 551 | | if (IsInLiquid) |
| | 18 | 552 | | return WaterModule?.IsEnabled == true |
| | 18 | 553 | | && WaterModule.CanSwim |
| | 18 | 554 | | && WaterModule.IsSwimming |
| | 18 | 555 | | ? WaterModule.MaxSwimAcceleration |
| | 18 | 556 | | : Handler.Move.MaxAirAcceleration; |
| | | 557 | | |
| | 156 | 558 | | if (IsClimbing) |
| | 31 | 559 | | return ClimbModule?.IsEnabled == true && ClimbModule.CanClimb |
| | 31 | 560 | | ? ClimbModule.MaxClimbAcceleration |
| | 31 | 561 | | : Handler.Move.MaxAirAcceleration; |
| | | 562 | | |
| | 125 | 563 | | if (IsFlying) |
| | 9 | 564 | | return FlyModule?.IsEnabled == true && FlyModule.CanFly |
| | 9 | 565 | | ? FlyModule.MaxFlyAcceleration |
| | 9 | 566 | | : Handler.Move.MaxAirAcceleration; |
| | | 567 | | |
| | 207 | 568 | | if (IsOnSolid) return Handler.Move.MaxGroundAcceleration; |
| | | 569 | | |
| | 25 | 570 | | if (IsJumping || IsFalling || IsInGas) |
| | 24 | 571 | | return Handler.Move.MaxAirAcceleration; |
| | | 572 | | |
| | 1 | 573 | | throw new InvalidOperationException( |
| | 1 | 574 | | $"Cannot resolve max acceleration while traversal medium is {CurrentState.Medium}. NavMotor requires a known |
| | | 575 | | } |
| | | 576 | | |
| | | 577 | | private void ApplyEnvironmentalForces() |
| | | 578 | | { |
| | 2089 | 579 | | Fixed64 gravityStep = Handler.Forces.GravityForce * DeltaTime; |
| | | 580 | | |
| | 2089 | 581 | | if (IsFlying) |
| | | 582 | | { |
| | 10 | 583 | | Fixed64 gravityCompensation = FixedMath.Clamp( |
| | 10 | 584 | | FlyModule?.GravityCompensation ?? Fixed64.Zero, |
| | 10 | 585 | | Fixed64.Zero, |
| | 10 | 586 | | Fixed64.One); |
| | 10 | 587 | | _forceOutput.y -= gravityStep * (Fixed64.One - gravityCompensation); |
| | 10 | 588 | | return; |
| | | 589 | | } |
| | | 590 | | |
| | 2079 | 591 | | if (IsClimbing) |
| | | 592 | | { |
| | 36 | 593 | | Fixed64 gravityCompensation = FixedMath.Clamp( |
| | 36 | 594 | | ClimbModule?.GravityCompensationWhileClimbing ?? Fixed64.Zero, |
| | 36 | 595 | | Fixed64.Zero, |
| | 36 | 596 | | Fixed64.One); |
| | 36 | 597 | | _forceOutput.y -= gravityStep * (Fixed64.One - gravityCompensation); |
| | 36 | 598 | | return; |
| | | 599 | | } |
| | | 600 | | |
| | 2043 | 601 | | if (IsOnSolid) |
| | | 602 | | { |
| | 215 | 603 | | _forceOutput.y = FixedMath.Min(Fixed64.Zero, _forceOutput.y) - gravityStep; |
| | 215 | 604 | | return; |
| | | 605 | | } |
| | | 606 | | |
| | 1828 | 607 | | if (IsInLiquid) |
| | | 608 | | { |
| | | 609 | | // Apply buoyancy if we can swim, otherwise apply gravity as normal. |
| | | 610 | | // Even if we can swim, we still apply gravity but reduce it based on the buoyancy factor to |
| | | 611 | | // create a more natural sinking effect when not actively swimming upwards. |
| | 175 | 612 | | if (WaterModule?.IsEnabled == true) |
| | 173 | 613 | | _forceOutput.y += gravityStep * (WaterModule.BuoyancyFactor - Fixed64.One); |
| | | 614 | | else |
| | 2 | 615 | | _forceOutput.y = Handler.Move.FrameVelocity.y - gravityStep; |
| | | 616 | | |
| | 2 | 617 | | return; |
| | | 618 | | } |
| | | 619 | | |
| | 1654 | 620 | | if (!IsInGas) return; |
| | | 621 | | |
| | 1652 | 622 | | _forceOutput.y = Handler.Move.FrameVelocity.y - gravityStep; |
| | | 623 | | |
| | | 624 | | // Ensure velocity does not exceed terminal fall speed |
| | 1652 | 625 | | Fixed64 terminalFallSpeed = Handler.Move.FrameVelocity.y + (_forceOutput.y * DeltaTime); |
| | 1652 | 626 | | if (terminalFallSpeed < -Handler.Forces.TerminalVelocity) |
| | 7 | 627 | | _forceOutput.y = -Handler.Forces.TerminalVelocity - Handler.Move.FrameVelocity.y; |
| | | 628 | | |
| | | 629 | | // When jumping up we don't apply gravity for some time when the user is holding the jump button. |
| | | 630 | | // This allows for more control over jump height by pressing the button longer. |
| | 1652 | 631 | | JumpLocomotion? jumpModule = JumpModule; |
| | 1652 | 632 | | if (IsJumping && jumpModule?.IsHoldingJump == true) |
| | | 633 | | { |
| | | 634 | | // Calculate the duration that the extra jump force should have effect. |
| | | 635 | | // If we're still less than that duration after the jumping time, apply the force. |
| | 2 | 636 | | Fixed64 extraJumpLimit = (jumpModule.JumpStartTime + jumpModule.ExtraJumpHeight) / GetVerticalJumpSpeed(); |
| | | 637 | | |
| | | 638 | | // Negate the gravity we just applied, except we push in jumpDir rather than jump upwards. |
| | 2 | 639 | | if (TotalTime <= extraJumpLimit) |
| | 2 | 640 | | _forceOutput += jumpModule.FrameJumpDirection * gravityStep; |
| | | 641 | | } |
| | 1652 | 642 | | } |
| | | 643 | | |
| | | 644 | | private void ApplyJumpForce(TrekRequest request) |
| | | 645 | | { |
| | 2087 | 646 | | if (!CanApplyJumpForce(request)) |
| | 2053 | 647 | | return; |
| | | 648 | | |
| | 34 | 649 | | Vector3d jumpForce = IsInLiquid |
| | 34 | 650 | | ? GetWaterBreachJumpForce() |
| | 34 | 651 | | : IsClimbing |
| | 34 | 652 | | ? GetClimbDetachJumpForce() |
| | 34 | 653 | | : GetGroundJumpForce(); |
| | 34 | 654 | | CommitJumpForce(jumpForce); |
| | 34 | 655 | | } |
| | | 656 | | |
| | | 657 | | private bool CanApplyJumpForce(TrekRequest request) |
| | | 658 | | { |
| | 2090 | 659 | | if (!(JumpModule?.IsEnabled == true |
| | 2090 | 660 | | && Handler.IsInControl |
| | 2090 | 661 | | && request.IsRequestingJump)) |
| | | 662 | | { |
| | 1974 | 663 | | return false; |
| | | 664 | | } |
| | | 665 | | |
| | 116 | 666 | | if (IsFlying || IsFalling) |
| | 18 | 667 | | return false; |
| | | 668 | | |
| | 98 | 669 | | if (IsClimbing && !(ClimbModule?.ActiveAllowDetachJump ?? false)) |
| | 0 | 670 | | return false; |
| | | 671 | | |
| | 98 | 672 | | if (IsInLiquid && !(WaterModule?.CanBreachWater ?? false)) |
| | 2 | 673 | | return false; |
| | | 674 | | |
| | 96 | 675 | | if (!JumpModule.CanJump) |
| | 60 | 676 | | return false; |
| | | 677 | | |
| | 36 | 678 | | return request.CanAffordJump; |
| | | 679 | | } |
| | | 680 | | |
| | | 681 | | private Vector3d GetWaterBreachJumpForce() |
| | | 682 | | { |
| | 2 | 683 | | JumpLocomotion? jumpModule = JumpModule; |
| | 2 | 684 | | WaterLocomotion? waterModule = WaterModule; |
| | 2 | 685 | | if (jumpModule == null || waterModule == null) |
| | 0 | 686 | | return Vector3d.Zero; |
| | | 687 | | |
| | 2 | 688 | | jumpModule.FrameJumpDirection = Vector3d.Up; |
| | 2 | 689 | | Events.OnStartWaterBreach?.Invoke(); |
| | 2 | 690 | | return jumpModule.FrameJumpDirection * (GetVerticalJumpSpeed() * waterModule.BreachJumpMultiplier); |
| | | 691 | | } |
| | | 692 | | |
| | | 693 | | private Vector3d GetGroundJumpForce() |
| | | 694 | | { |
| | 31 | 695 | | JumpLocomotion? jumpModule = JumpModule; |
| | 31 | 696 | | if (jumpModule == null) |
| | 0 | 697 | | return Vector3d.Zero; |
| | | 698 | | |
| | 31 | 699 | | EnsureJumpDirectionInitialized(); |
| | 31 | 700 | | Events.OnStartJump?.Invoke(jumpModule.AvoidGroundingTimer); |
| | 31 | 701 | | return jumpModule.FrameJumpDirection * GetVerticalJumpSpeed(); |
| | | 702 | | } |
| | | 703 | | |
| | | 704 | | private Vector3d GetClimbDetachJumpForce() |
| | | 705 | | { |
| | 1 | 706 | | JumpLocomotion? jumpModule = JumpModule; |
| | 1 | 707 | | ClimbLocomotion? climbModule = ClimbModule; |
| | 1 | 708 | | if (jumpModule == null || climbModule == null) |
| | 0 | 709 | | return Vector3d.Zero; |
| | | 710 | | |
| | 1 | 711 | | Vector3d upward = climbModule.AttachedUpDirection != Vector3d.Zero |
| | 1 | 712 | | ? climbModule.AttachedUpDirection.Normal |
| | 1 | 713 | | : Vector3d.Up; |
| | 1 | 714 | | Vector3d outward = climbModule.AttachedSurfaceNormal != Vector3d.Zero |
| | 1 | 715 | | ? climbModule.AttachedSurfaceNormal.Normal |
| | 1 | 716 | | : Vector3d.Backward; |
| | 1 | 717 | | jumpModule.FrameJumpDirection = Vector3d.Slerp(upward, outward, jumpModule.PerpendicularJumpAmount); |
| | 1 | 718 | | Events.OnStartJump?.Invoke(jumpModule.AvoidGroundingTimer); |
| | 1 | 719 | | return jumpModule.FrameJumpDirection * GetVerticalJumpSpeed(); |
| | | 720 | | } |
| | | 721 | | |
| | | 722 | | private void EnsureJumpDirectionInitialized() |
| | | 723 | | { |
| | 33 | 724 | | JumpLocomotion? jumpModule = JumpModule; |
| | 33 | 725 | | if (jumpModule == null || jumpModule.IsJumping) |
| | 5 | 726 | | return; |
| | | 727 | | |
| | 28 | 728 | | Fixed64 slerpAmount = IsTooSteep(FrameSlopeAngle) |
| | 28 | 729 | | ? jumpModule.SteepPerpendicularJumpAmount |
| | 28 | 730 | | : jumpModule.PerpendicularJumpAmount; |
| | | 731 | | |
| | 28 | 732 | | jumpModule.FrameJumpDirection = Vector3d.Slerp( |
| | 28 | 733 | | Vector3d.Up, |
| | 28 | 734 | | CurrentState.SurfaceNormal, |
| | 28 | 735 | | slerpAmount); |
| | 28 | 736 | | } |
| | | 737 | | |
| | | 738 | | private void CommitJumpForce(Vector3d jumpForce) |
| | | 739 | | { |
| | 34 | 740 | | if (IsClimbing) |
| | 1 | 741 | | StopClimb(wasForced: false); |
| | | 742 | | |
| | 34 | 743 | | JumpLocomotion? jumpModule = JumpModule; |
| | 34 | 744 | | if (jumpModule == null) |
| | 0 | 745 | | return; |
| | | 746 | | |
| | 34 | 747 | | jumpModule.RegisterJump(); |
| | | 748 | | |
| | | 749 | | // Remove any existing downward force before the jump impulse is applied. |
| | 34 | 750 | | _forceOutput.y = FixedMath.Max(Fixed64.Zero, _forceOutput.y); |
| | 34 | 751 | | _forceOutput += jumpForce; |
| | 34 | 752 | | } |
| | | 753 | | |
| | | 754 | | private void UpdateClimbState(TrekRequest request) |
| | | 755 | | { |
| | 2087 | 756 | | if (ClimbModule?.IsEnabled != true) |
| | 3 | 757 | | return; |
| | | 758 | | |
| | 2084 | 759 | | if (ClimbModule.IsMantling) |
| | 5 | 760 | | return; |
| | | 761 | | |
| | 2079 | 762 | | bool canAttemptClimb = request.IsRequestingClimb |
| | 2079 | 763 | | && ClimbModule.CanClimb |
| | 2079 | 764 | | && !IsInLiquid |
| | 2079 | 765 | | && CurrentState.Medium != TraversalMedium.Unknown; |
| | 2079 | 766 | | if (!canAttemptClimb) |
| | | 767 | | { |
| | 2034 | 768 | | if (ClimbModule.IsClimbing) |
| | 4 | 769 | | StopClimb(wasForced: false); |
| | | 770 | | |
| | 2034 | 771 | | return; |
| | | 772 | | } |
| | | 773 | | |
| | 45 | 774 | | if (ClimbModule.ClimbResolver == null |
| | 45 | 775 | | || !ClimbModule.ClimbResolver.TryResolveClimbAffordance(request, CurrentState, out ClimbAffordanceSnapshot s |
| | | 776 | | { |
| | 11 | 777 | | if (ClimbModule.IsClimbing) |
| | 1 | 778 | | StopClimb(wasForced: true); |
| | | 779 | | |
| | 11 | 780 | | return; |
| | | 781 | | } |
| | | 782 | | |
| | 34 | 783 | | if (ClimbModule.IsClimbing) |
| | | 784 | | { |
| | 13 | 785 | | bool canContinue = snapshot.CanContinueClimb |
| | 13 | 786 | | && IsCompatibleClimbAffordance(snapshot); |
| | 13 | 787 | | if (!canContinue) |
| | | 788 | | { |
| | 2 | 789 | | StopClimb(wasForced: true); |
| | 2 | 790 | | return; |
| | | 791 | | } |
| | | 792 | | |
| | 11 | 793 | | ClimbModule.ApplyClimbSnapshot(snapshot); |
| | | 794 | | |
| | 11 | 795 | | if (ShouldStartMantle(request, snapshot)) |
| | 7 | 796 | | StartMantle(snapshot); |
| | | 797 | | |
| | 11 | 798 | | return; |
| | | 799 | | } |
| | | 800 | | |
| | 21 | 801 | | bool canStart = snapshot.CanStartClimb; |
| | 21 | 802 | | if (!canStart) |
| | 1 | 803 | | return; |
| | | 804 | | |
| | 20 | 805 | | StartClimb(snapshot); |
| | 20 | 806 | | } |
| | | 807 | | |
| | | 808 | | private bool IsCompatibleClimbAffordance(ClimbAffordanceSnapshot snapshot) |
| | | 809 | | { |
| | 12 | 810 | | ClimbLocomotion? climbModule = ClimbModule; |
| | 12 | 811 | | if (climbModule == null) |
| | 0 | 812 | | return false; |
| | | 813 | | |
| | 12 | 814 | | if (climbModule.AttachmentId.HasValue && snapshot.AffordanceId.HasValue) |
| | 9 | 815 | | return climbModule.AttachmentId.Value == snapshot.AffordanceId.Value; |
| | | 816 | | |
| | 3 | 817 | | if (snapshot.Kind != climbModule.ActiveClimbKind) |
| | 0 | 818 | | return false; |
| | | 819 | | |
| | 3 | 820 | | if (!HasCompatibleClimbAxes(snapshot)) |
| | 1 | 821 | | return false; |
| | | 822 | | |
| | 2 | 823 | | Fixed64 tolerance = GetClimbContinuityTolerance(); |
| | 2 | 824 | | return (snapshot.AttachmentPoint - climbModule.AttachmentPoint).SqrMagnitude <= tolerance * tolerance; |
| | | 825 | | } |
| | | 826 | | |
| | | 827 | | private bool HasCompatibleClimbAxes(ClimbAffordanceSnapshot snapshot) |
| | | 828 | | { |
| | 3 | 829 | | ClimbLocomotion? climbModule = ClimbModule; |
| | 3 | 830 | | if (climbModule == null) |
| | 0 | 831 | | return false; |
| | | 832 | | |
| | 3 | 833 | | if (climbModule.AttachedSurfaceNormal != Vector3d.Zero |
| | 3 | 834 | | && snapshot.SurfaceNormal != Vector3d.Zero |
| | 3 | 835 | | && Vector3d.Dot(climbModule.AttachedSurfaceNormal.Normal, snapshot.SurfaceNormal.Normal) <= Fixed64.Zero) |
| | | 836 | | { |
| | 1 | 837 | | return false; |
| | | 838 | | } |
| | | 839 | | |
| | 2 | 840 | | if (climbModule.AttachedUpDirection != Vector3d.Zero |
| | 2 | 841 | | && snapshot.UpDirection != Vector3d.Zero |
| | 2 | 842 | | && Vector3d.Dot(climbModule.AttachedUpDirection.Normal, snapshot.UpDirection.Normal) <= Fixed64.Zero) |
| | | 843 | | { |
| | 0 | 844 | | return false; |
| | | 845 | | } |
| | | 846 | | |
| | 2 | 847 | | return true; |
| | | 848 | | } |
| | | 849 | | |
| | | 850 | | private Fixed64 GetClimbContinuityTolerance() |
| | | 851 | | { |
| | 2 | 852 | | ClimbLocomotion? climbModule = ClimbModule; |
| | 2 | 853 | | if (climbModule == null) |
| | 0 | 854 | | return Fixed64.Zero; |
| | | 855 | | |
| | 2 | 856 | | Fixed64 frameTravelAllowance = climbModule.MaxClimbSpeed * DeltaTime; |
| | 2 | 857 | | return climbModule.ClimbStartTolerance + frameTravelAllowance; |
| | | 858 | | } |
| | | 859 | | |
| | | 860 | | private void StartClimb(ClimbAffordanceSnapshot snapshot) |
| | | 861 | | { |
| | 20 | 862 | | ClimbLocomotion? climbModule = ClimbModule; |
| | 20 | 863 | | if (climbModule == null) |
| | 0 | 864 | | return; |
| | | 865 | | |
| | 20 | 866 | | climbModule.ApplyClimbSnapshot(snapshot); |
| | 20 | 867 | | climbModule.IsClimbing = true; |
| | 20 | 868 | | climbModule.IsMantling = false; |
| | | 869 | | |
| | 20 | 870 | | if (IsFalling) |
| | 1 | 871 | | Handler.ClearTransientState<FallLocomotion>(); |
| | | 872 | | |
| | 20 | 873 | | if (JumpModule != null) |
| | | 874 | | { |
| | 20 | 875 | | JumpModule.IsJumping = false; |
| | 20 | 876 | | JumpModule.IsHoldingJump = false; |
| | | 877 | | } |
| | | 878 | | |
| | 20 | 879 | | if (FlyModule != null) |
| | 20 | 880 | | FlyModule.IsFlying = false; |
| | | 881 | | |
| | 20 | 882 | | if (SlideModule != null) |
| | 20 | 883 | | SlideModule.IsSliding = false; |
| | | 884 | | |
| | 20 | 885 | | Events.OnStartClimb?.Invoke(snapshot); |
| | 3 | 886 | | } |
| | | 887 | | |
| | | 888 | | private bool ShouldStartMantle(TrekRequest request, ClimbAffordanceSnapshot snapshot) |
| | | 889 | | { |
| | 11 | 890 | | ClimbLocomotion? climbModule = ClimbModule; |
| | 11 | 891 | | if (climbModule == null) |
| | 0 | 892 | | return false; |
| | | 893 | | |
| | 11 | 894 | | if (snapshot.Kind != ClimbAffordanceKind.Ledge |
| | 11 | 895 | | || !climbModule.ActiveAllowMantle |
| | 11 | 896 | | || !climbModule.MantleTargetPosition.HasValue |
| | 11 | 897 | | || request.Rate == TrekRate.Stationary) |
| | | 898 | | { |
| | 3 | 899 | | return false; |
| | | 900 | | } |
| | | 901 | | |
| | 8 | 902 | | Vector3d upAxis = climbModule.AttachedUpDirection != Vector3d.Zero |
| | 8 | 903 | | ? climbModule.AttachedUpDirection.Normal |
| | 8 | 904 | | : Vector3d.Up; |
| | 8 | 905 | | return Vector3d.Dot(request.Direction, upAxis) > Fixed64.Zero; |
| | | 906 | | } |
| | | 907 | | |
| | | 908 | | private void StartMantle(ClimbAffordanceSnapshot snapshot) |
| | | 909 | | { |
| | 7 | 910 | | ClimbLocomotion? climbModule = ClimbModule; |
| | 7 | 911 | | if (climbModule == null) |
| | 0 | 912 | | return; |
| | | 913 | | |
| | 7 | 914 | | climbModule.ApplyClimbSnapshot(snapshot); |
| | 7 | 915 | | climbModule.IsMantling = true; |
| | 7 | 916 | | Events.OnStartMantle?.Invoke(); |
| | 1 | 917 | | } |
| | | 918 | | |
| | | 919 | | private void UpdateFlightState(TrekRequest request) |
| | | 920 | | { |
| | 2089 | 921 | | if (FlyModule?.IsEnabled != true) |
| | 3 | 922 | | return; |
| | | 923 | | |
| | 2086 | 924 | | bool shouldFly = request.IsRequestingFlight |
| | 2086 | 925 | | && FlyModule.CanFly |
| | 2086 | 926 | | && !IsClimbing |
| | 2086 | 927 | | && !IsInLiquid |
| | 2086 | 928 | | && CurrentState.Medium != TraversalMedium.Unknown; |
| | | 929 | | |
| | 2086 | 930 | | if (!shouldFly) |
| | | 931 | | { |
| | 2076 | 932 | | FlyModule.IsFlying = false; |
| | 2076 | 933 | | return; |
| | | 934 | | } |
| | | 935 | | |
| | 10 | 936 | | FlyModule.IsFlying = true; |
| | | 937 | | |
| | 10 | 938 | | if (IsFalling) |
| | 0 | 939 | | Handler.ClearTransientState<FallLocomotion>(); |
| | | 940 | | |
| | 10 | 941 | | if (JumpModule != null) |
| | | 942 | | { |
| | 10 | 943 | | JumpModule.IsJumping = false; |
| | 10 | 944 | | JumpModule.IsHoldingJump = false; |
| | | 945 | | } |
| | | 946 | | |
| | 10 | 947 | | if (SlideModule != null) |
| | 10 | 948 | | SlideModule.IsSliding = false; |
| | 10 | 949 | | } |
| | | 950 | | |
| | | 951 | | private void UpdateSwimState(TrekRequest request) |
| | | 952 | | { |
| | 2087 | 953 | | WaterLocomotion? waterModule = WaterModule; |
| | 2087 | 954 | | if (waterModule?.IsEnabled != true) |
| | 3 | 955 | | return; |
| | | 956 | | |
| | 2084 | 957 | | waterModule.RequestedSwimThisTraversal = request.IsRequestingSwim; |
| | 2084 | 958 | | waterModule.IsSwimming = IsInLiquid |
| | 2084 | 959 | | && waterModule.CanSwim |
| | 2084 | 960 | | && request.IsRequestingSwim; |
| | 2084 | 961 | | } |
| | | 962 | | |
| | | 963 | | #endregion |
| | | 964 | | |
| | | 965 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 966 | | private Vector3d ResolveTraversalVelocityDelta() |
| | | 967 | | { |
| | 2087 | 968 | | return _forceOutput != Vector3d.Zero |
| | 2087 | 969 | | ? _forceOutput * DeltaTime |
| | 2087 | 970 | | : Vector3d.Zero; |
| | | 971 | | } |
| | | 972 | | |
| | | 973 | | private void ResolvePlatformTraversal( |
| | | 974 | | TrekRequest request, |
| | | 975 | | ref Vector3d positionDelta, |
| | | 976 | | ref FixedQuaternion rotationDelta) |
| | | 977 | | { |
| | 2087 | 978 | | PlatformLocomotion platformModule = PlatformModule; |
| | | 979 | | |
| | | 980 | | // Do not apply platform movement if we just jumped or if the platform is not actively driving us. |
| | 2087 | 981 | | bool isMovingWithPlatform = platformModule.IsActive |
| | 2087 | 982 | | && !IsFlying |
| | 2087 | 983 | | && !IsClimbing |
| | 2087 | 984 | | && (IsOnSolid || platformModule.IsLockedToPlatform); |
| | 2087 | 985 | | if (!isMovingWithPlatform || IsJumping) |
| | 1943 | 986 | | return; |
| | | 987 | | |
| | 144 | 988 | | platformModule.GetPlatformInfluence( |
| | 144 | 989 | | request.FootPosition ?? request.Origin, |
| | 144 | 990 | | request.Rotation, |
| | 144 | 991 | | out positionDelta, |
| | 144 | 992 | | out rotationDelta); |
| | 144 | 993 | | } |
| | | 994 | | |
| | | 995 | | #endregion |
| | | 996 | | } |