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