| | | 1 | | using Chronicler; |
| | | 2 | | using FixedMathSharp; |
| | | 3 | | using Trailblazer.Support; |
| | | 4 | | |
| | | 5 | | namespace Trailblazer.Navigation.Motor; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Handles controlled flight movement while the scout is airborne. |
| | | 9 | | /// </summary> |
| | | 10 | | /// <remarks> |
| | | 11 | | /// Flight is distinct from both jumping and falling. |
| | | 12 | | /// It provides active air control, optional gravity cancellation, and controlled ascent or descent. |
| | | 13 | | /// </remarks> |
| | | 14 | | public class FlyLocomotion : ILocomotion |
| | | 15 | | { |
| | | 16 | | #region Constants |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// Default maximum horizontal flight speed. |
| | | 20 | | /// </summary> |
| | 1 | 21 | | public static readonly Fixed64 DefaultMaxFlySpeed = (Fixed64)1.5d; |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// Default maximum upward flight speed. |
| | | 25 | | /// </summary> |
| | 1 | 26 | | public static readonly Fixed64 DefaultMaxAscendSpeed = (Fixed64)1.5d; |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// Default maximum downward flight speed while actively descending. |
| | | 30 | | /// </summary> |
| | 1 | 31 | | public static readonly Fixed64 DefaultMaxDescendSpeed = (Fixed64)1.5d; |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// Default acceleration used while actively flying. |
| | | 35 | | /// </summary> |
| | 1 | 36 | | public static readonly Fixed64 DefaultMaxFlyAcceleration = (Fixed64)20; |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// Default amount of gravity canceled while actively flying. |
| | | 40 | | /// </summary> |
| | 1 | 41 | | public static readonly Fixed64 DefaultGravityCompensation = Fixed64.One; |
| | | 42 | | |
| | | 43 | | #endregion |
| | | 44 | | |
| | | 45 | | #region Configuration State |
| | | 46 | | |
| | 786 | 47 | | private bool _isEnabled = true; |
| | | 48 | | |
| | | 49 | | /// <summary> |
| | | 50 | | /// Determines whether the scout is allowed to enter controlled flight. |
| | | 51 | | /// </summary> |
| | 786 | 52 | | public bool CanFly = true; |
| | | 53 | | |
| | | 54 | | /// <summary> |
| | | 55 | | /// The maximum horizontal speed while flying. |
| | | 56 | | /// </summary> |
| | 786 | 57 | | public Fixed64 MaxFlySpeed = DefaultMaxFlySpeed; |
| | | 58 | | |
| | | 59 | | /// <summary> |
| | | 60 | | /// The maximum upward speed while flying. |
| | | 61 | | /// </summary> |
| | 786 | 62 | | public Fixed64 MaxAscendSpeed = DefaultMaxAscendSpeed; |
| | | 63 | | |
| | | 64 | | /// <summary> |
| | | 65 | | /// The maximum downward speed while actively descending under flight control. |
| | | 66 | | /// </summary> |
| | 786 | 67 | | public Fixed64 MaxDescendSpeed = DefaultMaxDescendSpeed; |
| | | 68 | | |
| | | 69 | | /// <summary> |
| | | 70 | | /// The maximum acceleration applied while steering in flight. |
| | | 71 | | /// </summary> |
| | 786 | 72 | | public Fixed64 MaxFlyAcceleration = DefaultMaxFlyAcceleration; |
| | | 73 | | |
| | | 74 | | /// <summary> |
| | | 75 | | /// The amount of gravity canceled while flying, clamped between 0 and 1 by the motor. |
| | | 76 | | /// </summary> |
| | 786 | 77 | | public Fixed64 GravityCompensation = DefaultGravityCompensation; |
| | | 78 | | |
| | | 79 | | #endregion |
| | | 80 | | |
| | | 81 | | #region Transient State |
| | | 82 | | |
| | | 83 | | /// <inheritdoc cref="ILocomotion.IsEnabled"/> |
| | | 84 | | public bool IsEnabled |
| | | 85 | | { |
| | 4180 | 86 | | get => _isEnabled; |
| | | 87 | | set |
| | | 88 | | { |
| | 6 | 89 | | _isEnabled = value; |
| | 6 | 90 | | if (!_isEnabled) |
| | 4 | 91 | | this.ClearTransientState(); |
| | 6 | 92 | | } |
| | | 93 | | } |
| | | 94 | | |
| | | 95 | | /// <summary> |
| | | 96 | | /// Indicates whether controlled flight is currently active. |
| | | 97 | | /// </summary> |
| | | 98 | | [Transient] |
| | | 99 | | public bool IsFlying { get; set; } |
| | | 100 | | |
| | | 101 | | #endregion |
| | | 102 | | |
| | | 103 | | /// <inheritdoc /> |
| | | 104 | | public void RecordData(IChronicler chronicler) |
| | | 105 | | { |
| | 100 | 106 | | RecordValues.Look(chronicler, ref _isEnabled, "IsEnabled", true); |
| | 100 | 107 | | RecordValues.Look(chronicler, ref CanFly, "CanFly", true); |
| | 100 | 108 | | RecordValues.Look(chronicler, ref MaxFlySpeed, "MaxFlySpeed", DefaultMaxFlySpeed); |
| | 100 | 109 | | RecordValues.Look(chronicler, ref MaxAscendSpeed, "MaxAscendSpeed", DefaultMaxAscendSpeed); |
| | 100 | 110 | | RecordValues.Look(chronicler, ref MaxDescendSpeed, "MaxDescendSpeed", DefaultMaxDescendSpeed); |
| | 100 | 111 | | RecordValues.Look(chronicler, ref MaxFlyAcceleration, "MaxFlyAcceleration", DefaultMaxFlyAcceleration); |
| | 100 | 112 | | RecordValues.Look(chronicler, ref GravityCompensation, "GravityCompensation", DefaultGravityCompensation); |
| | | 113 | | |
| | 100 | 114 | | bool isFlying = IsFlying; |
| | 100 | 115 | | RecordValues.Look(chronicler, ref isFlying, "IsFlying", false); |
| | | 116 | | |
| | 100 | 117 | | if (chronicler.Mode == SerializationMode.Loading) |
| | | 118 | | { |
| | 50 | 119 | | IsFlying = isFlying; |
| | | 120 | | |
| | 50 | 121 | | if (!_isEnabled) |
| | 2 | 122 | | this.ClearTransientState(); |
| | | 123 | | } |
| | 100 | 124 | | } |
| | | 125 | | } |