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