| | | 1 | | //======================================================================= |
| | | 2 | | // JumpLocomotion.cs |
| | | 3 | | //======================================================================= |
| | | 4 | | // MIT License, Copyright (c) 2024-present David Oravsky (mrdav30) |
| | | 5 | | // See LICENSE file in the project root for full license information. |
| | | 6 | | //======================================================================= |
| | | 7 | | |
| | | 8 | | using System; |
| | | 9 | | using Chronicler; |
| | | 10 | | using FixedMathSharp; |
| | | 11 | | using Trailblazer.Support; |
| | | 12 | | |
| | | 13 | | namespace Trailblazer.Navigation.Motor; |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Handles the scout’s jumping mechanics, including jump height, cooldown, and movement control while airborne. |
| | | 17 | | /// </summary> |
| | | 18 | | /// <remarks> |
| | | 19 | | /// This locomotion component determines how high the scout can jump, how much control they retain mid-air, |
| | | 20 | | /// and enforces a cooldown period between consecutive jumps. |
| | | 21 | | /// </remarks> |
| | | 22 | | public class JumpLocomotion : ILocomotion |
| | | 23 | | { |
| | | 24 | | private TrailblazerWorldContext? _context; |
| | | 25 | | |
| | | 26 | | #region Constants |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// The default base jump height, representing how high the scout jumps when pressing and immediately releasing the |
| | | 30 | | /// </summary> |
| | 1 | 31 | | public static readonly Fixed64 DefaultBaseJumpHeight = Fixed64.One; |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// The additional height added when the jump button is held longer. |
| | | 35 | | /// </summary> |
| | 1 | 36 | | public static readonly Fixed64 DefaultExtraJumpHeight = (Fixed64)2f; |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// Determines how much the scout's jump direction is influenced by the slope of the surface they are on. |
| | | 40 | | /// </summary> |
| | 1 | 41 | | public static readonly Fixed64 DefaultPerpendicularJumpAmount = Fixed64.Half; |
| | | 42 | | |
| | | 43 | | /// <summary> |
| | | 44 | | /// Determines how much the scout's jump direction is influenced by steep slopes. |
| | | 45 | | /// </summary> |
| | 1 | 46 | | public static readonly Fixed64 DefaultSteepPerpendicularJumpAmount = Fixed64.Half; |
| | | 47 | | |
| | | 48 | | /// <summary> |
| | | 49 | | /// Determines how much movement input affects the scout while jumping. |
| | | 50 | | /// </summary> |
| | 1 | 51 | | public static readonly Fixed64 DefaultJumpControlMultiplier = Fixed64.FromFraction(3, 8); // 75% control when jumpin |
| | | 52 | | |
| | | 53 | | /// <summary> |
| | | 54 | | /// The default cooldown time after a jump before the scout can jump again. |
| | | 55 | | /// </summary> |
| | 1 | 56 | | public static readonly Fixed64 DefaultCooldownTime = Fixed64.FromFraction(1, 5); |
| | | 57 | | |
| | | 58 | | /// <summary> |
| | | 59 | | /// The default duration after jumping during which ground checks should be ignored. |
| | | 60 | | /// </summary> |
| | 1 | 61 | | public static readonly Fixed64 DefaultAvoidGroundingTimer = Fixed64.FromFraction(1, 20); |
| | | 62 | | |
| | | 63 | | #endregion |
| | | 64 | | |
| | | 65 | | #region Configuration State |
| | | 66 | | |
| | | 67 | | /// <summary> |
| | | 68 | | /// Determines whether jumping is enabled. |
| | | 69 | | /// If disabled, the scout will be unable to jump. |
| | | 70 | | /// </summary> |
| | 1154 | 71 | | private bool _isEnabled = true; |
| | | 72 | | |
| | | 73 | | /// <summary> |
| | | 74 | | /// Maximum number of consecutive jumps allowed (e.g., 2 = double jump). |
| | | 75 | | /// </summary> |
| | 1154 | 76 | | public int MaxJumpCount = 1; |
| | | 77 | | |
| | | 78 | | /// <summary> |
| | | 79 | | /// The cooldown time before another jump can be performed. |
| | | 80 | | /// </summary> |
| | 1154 | 81 | | public Fixed64 CooldownTime = DefaultCooldownTime; |
| | | 82 | | |
| | | 83 | | /// <summary> |
| | | 84 | | /// The duration after jumping where ground detection is temporarily disabled. |
| | | 85 | | /// </summary> |
| | 1154 | 86 | | public Fixed64 AvoidGroundingTimer = DefaultAvoidGroundingTimer; |
| | | 87 | | |
| | | 88 | | /// <summary> |
| | | 89 | | /// The base height the scout can jump when the button is pressed briefly. |
| | | 90 | | /// </summary> |
| | 1154 | 91 | | public Fixed64 BaseJumpHeight = DefaultBaseJumpHeight; |
| | | 92 | | |
| | | 93 | | /// <summary> |
| | | 94 | | /// Additional height gained when holding the jump button. |
| | | 95 | | /// </summary> |
| | 1154 | 96 | | public Fixed64 ExtraJumpHeight = DefaultExtraJumpHeight; |
| | | 97 | | |
| | | 98 | | /// <summary> |
| | | 99 | | /// Controls how much movement input affects the scout while jumping. |
| | | 100 | | /// Lower values reduce movement responsiveness. |
| | | 101 | | /// </summary> |
| | 1154 | 102 | | public Fixed64 JumpControlMultiplier = DefaultJumpControlMultiplier; |
| | | 103 | | |
| | | 104 | | /// <summary> |
| | | 105 | | /// Controls how much the scout jumps out perpendicular to the surface on walkable terrain. |
| | | 106 | | /// A value of 0 means fully vertical, and 1 means fully perpendicular. |
| | | 107 | | /// </summary> |
| | 1154 | 108 | | public Fixed64 PerpendicularJumpAmount = DefaultPerpendicularJumpAmount; |
| | | 109 | | |
| | | 110 | | /// <summary> |
| | | 111 | | /// Controls how much the scout jumps out perpendicular to the surface on steep terrain. |
| | | 112 | | /// A value of 0 means fully vertical, and 1 means fully perpendicular. |
| | | 113 | | /// </summary> |
| | 1154 | 114 | | public Fixed64 SteepPerpendicularJumpAmount = DefaultSteepPerpendicularJumpAmount; |
| | | 115 | | |
| | | 116 | | #endregion |
| | | 117 | | |
| | | 118 | | #region Transient State |
| | | 119 | | |
| | | 120 | | /// <inheritdoc cref="ILocomotion.IsEnabled"/> |
| | | 121 | | public bool IsEnabled |
| | | 122 | | { |
| | 2187 | 123 | | get => _isEnabled; |
| | | 124 | | set |
| | | 125 | | { |
| | 2 | 126 | | _isEnabled = value; |
| | 2 | 127 | | if (!_isEnabled) |
| | 1 | 128 | | this.ClearTransientState(); |
| | 2 | 129 | | } |
| | | 130 | | } |
| | | 131 | | |
| | | 132 | | /// <summary> |
| | | 133 | | /// Indicates whether the scout is currently performing a jump. |
| | | 134 | | /// </summary> |
| | | 135 | | /// <remarks> |
| | | 136 | | /// This is true if the jump button was pressed and the scout is not grounded. |
| | | 137 | | /// </remarks> |
| | | 138 | | [Transient] |
| | | 139 | | public bool IsJumping { get; set; } |
| | | 140 | | |
| | | 141 | | /// <summary> |
| | | 142 | | /// Indicates whether the scout is holding the jump button. |
| | | 143 | | /// </summary> |
| | | 144 | | [Transient] |
| | | 145 | | public bool IsHoldingJump { get; set; } |
| | | 146 | | |
| | | 147 | | /// <summary> |
| | | 148 | | /// The simulation frame when the scout started jumping. |
| | | 149 | | /// </summary> |
| | | 150 | | [Transient] |
| | | 151 | | public Fixed64 JumpStartTime { get; set; } |
| | | 152 | | |
| | | 153 | | /// <summary> |
| | | 154 | | /// The direction in which the scout jumped during the current frame. |
| | | 155 | | /// </summary> |
| | | 156 | | [Transient] |
| | | 157 | | public Vector3d FrameJumpDirection { get; set; } |
| | | 158 | | |
| | | 159 | | /// <summary> |
| | | 160 | | /// The elapsed time in the cooldown state. |
| | | 161 | | /// </summary> |
| | | 162 | | [Transient] |
| | | 163 | | public Fixed64 CooldownTimer { get; private set; } |
| | | 164 | | |
| | | 165 | | /// <summary> |
| | | 166 | | /// Indicates whether the scout is currently in a jump cooldown period. |
| | | 167 | | /// </summary> |
| | | 168 | | [Transient] |
| | | 169 | | public bool IsCoolingDown { get; private set; } |
| | | 170 | | |
| | | 171 | | /// <summary> |
| | | 172 | | /// The current number of jumps performed since the last grounding. |
| | | 173 | | /// </summary> |
| | | 174 | | [Transient] |
| | | 175 | | public int JumpCount { get; private set; } |
| | | 176 | | |
| | | 177 | | /// <summary> |
| | | 178 | | /// Returns true if more jumps are allowed. |
| | | 179 | | /// </summary> |
| | 213 | 180 | | public bool CanJump => JumpCount < MaxJumpCount && !IsCoolingDown; |
| | | 181 | | |
| | | 182 | | private TrailblazerWorldContext RequireContext() => |
| | 151 | 183 | | _context ?? throw new InvalidOperationException("JumpLocomotion requires an explicit TrailblazerWorldContext."); |
| | | 184 | | |
| | 102 | 185 | | private Fixed64 DeltaTime => RequireContext().DeltaTime; |
| | | 186 | | |
| | 49 | 187 | | private Fixed64 TotalTime => RequireContext().TotalTime; |
| | | 188 | | |
| | | 189 | | #endregion |
| | | 190 | | |
| | | 191 | | #region Methods |
| | | 192 | | |
| | | 193 | | /// <summary> |
| | | 194 | | /// Increments the jump counter. |
| | | 195 | | /// </summary> |
| | | 196 | | public void RegisterJump() |
| | | 197 | | { |
| | 49 | 198 | | Fixed64 startTime = TotalTime; |
| | 48 | 199 | | JumpCount++; |
| | 48 | 200 | | IsJumping = true; |
| | 48 | 201 | | IsHoldingJump = true; |
| | 48 | 202 | | JumpStartTime = startTime; |
| | | 203 | | |
| | | 204 | | // Start cooldown only if this was the last allowed jump |
| | 48 | 205 | | if (JumpCount >= MaxJumpCount) |
| | 36 | 206 | | StartCooldown(); |
| | 48 | 207 | | } |
| | | 208 | | |
| | | 209 | | /// <summary> |
| | | 210 | | /// Starts the jump cooldown period, preventing another jump until the cooldown expires. |
| | | 211 | | /// </summary> |
| | | 212 | | public void StartCooldown() |
| | | 213 | | { |
| | 48 | 214 | | IsCoolingDown = true; |
| | 48 | 215 | | CooldownTimer = Fixed64.Zero; |
| | 48 | 216 | | } |
| | | 217 | | |
| | | 218 | | /// <summary> |
| | | 219 | | /// Updates the jump cooldown timer, resetting the jump state when the cooldown expires. |
| | | 220 | | /// </summary> |
| | | 221 | | public void UpdateCooldown() |
| | | 222 | | { |
| | 103 | 223 | | if (!IsCoolingDown) |
| | 1 | 224 | | return; |
| | 102 | 225 | | CooldownTimer += DeltaTime; |
| | 102 | 226 | | if (CooldownTimer >= CooldownTime) |
| | | 227 | | { |
| | 12 | 228 | | CooldownTimer = Fixed64.Zero; |
| | 12 | 229 | | IsCoolingDown = false; |
| | | 230 | | } |
| | 102 | 231 | | } |
| | | 232 | | |
| | | 233 | | /// <summary> |
| | | 234 | | /// Resets jump state upon grounding. |
| | | 235 | | /// </summary> |
| | | 236 | | public void ResetJumpCounter() |
| | | 237 | | { |
| | 8 | 238 | | JumpCount = 0; |
| | 8 | 239 | | IsJumping = false; |
| | 8 | 240 | | IsHoldingJump = false; |
| | 8 | 241 | | } |
| | | 242 | | |
| | | 243 | | internal void BindContext(TrailblazerWorldContext context) |
| | | 244 | | { |
| | 1244 | 245 | | TrailblazerWorldContext.ThrowIfUnusable(context); |
| | 1244 | 246 | | _context = context; |
| | 1244 | 247 | | } |
| | | 248 | | |
| | | 249 | | #endregion |
| | | 250 | | |
| | | 251 | | /// <inheritdoc /> |
| | | 252 | | public void RecordData(IChronicler chronicler) |
| | | 253 | | { |
| | 158 | 254 | | RecordValues.Look(chronicler, ref _isEnabled, "IsEnabled", true); |
| | 158 | 255 | | RecordValues.Look(chronicler, ref MaxJumpCount, "MaxJumpCount", 1); |
| | 158 | 256 | | RecordValues.Look(chronicler, ref CooldownTime, "CooldownTime", DefaultCooldownTime); |
| | 158 | 257 | | RecordValues.Look(chronicler, ref AvoidGroundingTimer, "AvoidGroundingTimer", DefaultAvoidGroundingTimer); |
| | 158 | 258 | | RecordValues.Look(chronicler, ref BaseJumpHeight, "BaseJumpHeight", DefaultBaseJumpHeight); |
| | 158 | 259 | | RecordValues.Look(chronicler, ref ExtraJumpHeight, "ExtraJumpHeight", DefaultExtraJumpHeight); |
| | 158 | 260 | | RecordValues.Look(chronicler, ref JumpControlMultiplier, "JumpControlMultiplier", DefaultJumpControlMultiplier); |
| | 158 | 261 | | RecordValues.Look(chronicler, ref PerpendicularJumpAmount, "PerpendicularJumpAmount", DefaultPerpendicularJumpAm |
| | 158 | 262 | | RecordValues.Look(chronicler, ref SteepPerpendicularJumpAmount, "SteepPerpendicularJumpAmount", DefaultSteepPerp |
| | | 263 | | |
| | 158 | 264 | | bool isJumping = IsJumping; |
| | 158 | 265 | | bool isHoldingJump = IsHoldingJump; |
| | 158 | 266 | | Fixed64 jumpStartTime = JumpStartTime; |
| | 158 | 267 | | Vector3d frameJumpDirection = FrameJumpDirection; |
| | 158 | 268 | | Fixed64 cooldownTimer = CooldownTimer; |
| | 158 | 269 | | bool isCoolingDown = IsCoolingDown; |
| | 158 | 270 | | int jumpCount = JumpCount; |
| | | 271 | | |
| | 158 | 272 | | RecordValues.Look(chronicler, ref isJumping, "IsJumping", false); |
| | 158 | 273 | | RecordValues.Look(chronicler, ref isHoldingJump, "IsHoldingJump", false); |
| | 158 | 274 | | RecordValues.Look(chronicler, ref jumpStartTime, "JumpStartTime", Fixed64.Zero); |
| | 158 | 275 | | RecordValues.Look(chronicler, ref frameJumpDirection, "FrameJumpDirection", Vector3d.Zero); |
| | 158 | 276 | | RecordValues.Look(chronicler, ref cooldownTimer, "CooldownTimer", Fixed64.Zero); |
| | 158 | 277 | | RecordValues.Look(chronicler, ref isCoolingDown, "IsCoolingDown", false); |
| | 158 | 278 | | RecordValues.Look(chronicler, ref jumpCount, "JumpCount", 0); |
| | | 279 | | |
| | 158 | 280 | | if (chronicler.Mode == SerializationMode.Loading) |
| | | 281 | | { |
| | 74 | 282 | | IsJumping = isJumping; |
| | 74 | 283 | | IsHoldingJump = isHoldingJump; |
| | 74 | 284 | | JumpStartTime = jumpStartTime; |
| | 74 | 285 | | FrameJumpDirection = frameJumpDirection; |
| | 74 | 286 | | CooldownTimer = cooldownTimer; |
| | 74 | 287 | | IsCoolingDown = isCoolingDown; |
| | 74 | 288 | | JumpCount = jumpCount; |
| | | 289 | | |
| | 74 | 290 | | if (!_isEnabled) |
| | 2 | 291 | | this.ClearTransientState(); |
| | | 292 | | } |
| | 158 | 293 | | } |
| | | 294 | | } |