| | | 1 | | //======================================================================= |
| | | 2 | | // FallLocomotion.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 Chronicler; |
| | | 9 | | using FixedMathSharp; |
| | | 10 | | using Trailblazer.Support; |
| | | 11 | | |
| | | 12 | | namespace Trailblazer.Navigation.Motor; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Handles the scout’s behavior when falling, including tracking fall distance and applying movement constraints. |
| | | 16 | | /// </summary> |
| | | 17 | | public class FallLocomotion : ILocomotion |
| | | 18 | | { |
| | | 19 | | #region Constants |
| | | 20 | | |
| | | 21 | | /// <summary> |
| | | 22 | | /// The default maximum height a scout can fall before a fatal impact occurs. |
| | | 23 | | /// </summary> |
| | 1 | 24 | | public static readonly Fixed64 DefaultMaxFallHeight = (Fixed64)30; |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Default movement control multiplier when falling. |
| | | 28 | | /// Reduces movement responsiveness to simulate loss of control while airborne. |
| | | 29 | | /// </summary> |
| | 1 | 30 | | public static readonly Fixed64 DefaultFallControlMultiplier = Fixed64.FromFraction(3, 16); // 50% control when falli |
| | | 31 | | |
| | | 32 | | #endregion |
| | | 33 | | |
| | | 34 | | #region Configuration State |
| | | 35 | | |
| | | 36 | | /// <summary> |
| | | 37 | | /// Determines whether falling mechanics are enabled. |
| | | 38 | | /// If disabled, the scout will not experience fall behavior. |
| | | 39 | | /// </summary> |
| | 1201 | 40 | | private bool _isEnabled = true; |
| | | 41 | | |
| | | 42 | | /// <summary> |
| | | 43 | | /// The maximum allowable fall height before the scout reaches a critical threshold (e.g., death or heavy impact). |
| | | 44 | | /// </summary> |
| | 1201 | 45 | | public Fixed64 MaxFallHeight = DefaultMaxFallHeight; |
| | | 46 | | |
| | | 47 | | /// <summary> |
| | | 48 | | /// A multiplier controlling how much movement input affects the scout while falling. |
| | | 49 | | /// Lower values reduce movement responsiveness. |
| | | 50 | | /// </summary> |
| | 1201 | 51 | | public Fixed64 FallControlMultiplier = DefaultFallControlMultiplier; |
| | | 52 | | |
| | | 53 | | #endregion |
| | | 54 | | |
| | | 55 | | #region Transient State |
| | | 56 | | |
| | | 57 | | /// <inheritdoc cref="ILocomotion.IsEnabled"/> |
| | | 58 | | public bool IsEnabled |
| | | 59 | | { |
| | 2179 | 60 | | get => _isEnabled; |
| | | 61 | | set |
| | | 62 | | { |
| | 3 | 63 | | _isEnabled = value; |
| | 3 | 64 | | if (!value) |
| | 2 | 65 | | this.ClearTransientState(); |
| | 3 | 66 | | } |
| | | 67 | | } |
| | | 68 | | |
| | | 69 | | /// <summary> |
| | | 70 | | /// Indicates whether the scout is currently falling. |
| | | 71 | | /// </summary> |
| | | 72 | | [Transient] |
| | | 73 | | public bool IsFalling { get; set; } |
| | | 74 | | |
| | | 75 | | /// <summary> |
| | | 76 | | /// The vertical position where the scout started falling. |
| | | 77 | | /// </summary> |
| | | 78 | | [Transient] |
| | | 79 | | public Fixed64 FallStart { get; set; } |
| | | 80 | | |
| | | 81 | | /// <summary> |
| | | 82 | | /// The vertical position where the scout landed. |
| | | 83 | | /// </summary> |
| | | 84 | | [Transient] |
| | | 85 | | public Fixed64 FallEnd { get; set; } |
| | | 86 | | |
| | | 87 | | /// <summary> |
| | | 88 | | /// The total distance fallen, calculated as the difference between <see cref="FallStart"/> and <see cref="FallEnd"/ |
| | | 89 | | /// </summary> |
| | 17 | 90 | | public Fixed64 FallHeight => FallStart - FallEnd; |
| | | 91 | | |
| | | 92 | | #endregion |
| | | 93 | | |
| | | 94 | | /// <inheritdoc /> |
| | | 95 | | public void RecordData(IChronicler chronicler) |
| | | 96 | | { |
| | 164 | 97 | | RecordValues.Look(chronicler, ref _isEnabled, "IsEnabled", true); |
| | 164 | 98 | | RecordValues.Look(chronicler, ref MaxFallHeight, "MaxFallHeight", DefaultMaxFallHeight); |
| | 164 | 99 | | RecordValues.Look(chronicler, ref FallControlMultiplier, "FallControlMultiplier", DefaultFallControlMultiplier); |
| | | 100 | | |
| | 164 | 101 | | bool isFalling = IsFalling; |
| | 164 | 102 | | Fixed64 fallStart = FallStart; |
| | 164 | 103 | | Fixed64 fallEnd = FallEnd; |
| | | 104 | | |
| | 164 | 105 | | RecordValues.Look(chronicler, ref isFalling, "IsFalling", false); |
| | 164 | 106 | | RecordValues.Look(chronicler, ref fallStart, "FallStart", Fixed64.Zero); |
| | 164 | 107 | | RecordValues.Look(chronicler, ref fallEnd, "FallEnd", Fixed64.Zero); |
| | | 108 | | |
| | 164 | 109 | | if (chronicler.Mode == SerializationMode.Loading) |
| | | 110 | | { |
| | 77 | 111 | | IsFalling = isFalling; |
| | 77 | 112 | | FallStart = fallStart; |
| | 77 | 113 | | FallEnd = fallEnd; |
| | | 114 | | |
| | 77 | 115 | | if (!_isEnabled) |
| | 2 | 116 | | this.ClearTransientState(); |
| | | 117 | | } |
| | 164 | 118 | | } |
| | | 119 | | } |