| | | 1 | | using Chronicler; |
| | | 2 | | using FixedMathSharp; |
| | | 3 | | |
| | | 4 | | namespace Trailblazer.Navigation.Motor; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Holds the gravity and terminal velocity forces applied to a object, with support for per-instance overrides and glob |
| | | 8 | | /// </summary> |
| | | 9 | | public sealed class LocomotionForces : IRecordable |
| | | 10 | | { |
| | | 11 | | /// <summary> |
| | | 12 | | /// Simulation-wide gravity defaults applied to all <see cref="MoveLocomotion"/> instances |
| | | 13 | | /// that do not carry a per-instance override. |
| | | 14 | | /// </summary> |
| | | 15 | | /// <remarks> |
| | | 16 | | /// Assign fields on this instance to shift gravity for every unoverridden object at once. |
| | | 17 | | /// Call <see cref="GlobalEnvironmentForces.Reset"/> to restore the defaults. |
| | | 18 | | /// </remarks> |
| | 1 | 19 | | public static readonly GlobalEnvironmentForces GlobalForces = new(); |
| | | 20 | | |
| | | 21 | | #region Configurable Parameters |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// The gravity force applied to this object. |
| | | 25 | | /// Returns the per-instance override when one is set; |
| | | 26 | | /// otherwise delegates to <see cref="GlobalEnvironmentForces"/> so a single global change takes effect for all unov |
| | | 27 | | /// Assigning this property sets the per-instance override. |
| | | 28 | | /// Call <see cref="ClearGravityForceOverride"/> to remove the override and restore global tracking. |
| | | 29 | | /// </summary> |
| | | 30 | | public Fixed64 GravityForce |
| | | 31 | | { |
| | 2140 | 32 | | get => _gravityForceOverride ?? GlobalForces.GravityForce; |
| | 2 | 33 | | set => _gravityForceOverride = value; |
| | | 34 | | } |
| | | 35 | | |
| | | 36 | | private Fixed64? _gravityForceOverride; |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// Returns true when this instance carries a per-instance gravity override. |
| | | 40 | | /// When false, <see cref="GravityForce"/> follows <see cref="GlobalEnvironmentForces"/>. |
| | | 41 | | /// </summary> |
| | 5 | 42 | | public bool HasGravityForceOverride => _gravityForceOverride.HasValue; |
| | | 43 | | |
| | | 44 | | |
| | | 45 | | /// <summary> |
| | | 46 | | /// The terminal fall velocity cap for this object. |
| | | 47 | | /// Returns the per-instance override when one is set; otherwise delegates to |
| | | 48 | | /// <see cref="GlobalEnvironmentForces"/>. |
| | | 49 | | /// Assigning this property sets the per-instance override. |
| | | 50 | | /// Call <see cref="ClearTerminalVelocityOverride"/> to remove the override and restore global tracking. |
| | | 51 | | /// </summary> |
| | | 52 | | public Fixed64 TerminalVelocity |
| | | 53 | | { |
| | 1661 | 54 | | get => _terminalVelocityOverride ?? GlobalForces.TerminalVelocity; |
| | 1 | 55 | | set => _terminalVelocityOverride = value; |
| | | 56 | | } |
| | | 57 | | |
| | | 58 | | private Fixed64? _terminalVelocityOverride; |
| | | 59 | | |
| | | 60 | | /// <summary> |
| | | 61 | | /// Returns true when this instance carries a per-instance terminal velocity override. |
| | | 62 | | /// When false, <see cref="TerminalVelocity"/> follows <see cref="GlobalEnvironmentForces"/>. |
| | | 63 | | /// </summary> |
| | 3 | 64 | | public bool HasTerminalVelocityOverride => _terminalVelocityOverride.HasValue; |
| | | 65 | | |
| | | 66 | | #endregion |
| | | 67 | | |
| | | 68 | | #region Overrides |
| | | 69 | | |
| | | 70 | | /// <summary> |
| | | 71 | | /// Removes the per-instance gravity override so this object tracks |
| | | 72 | | /// <see cref="GlobalEnvironmentForces"/>. |
| | | 73 | | /// </summary> |
| | 1 | 74 | | public void ClearGravityForceOverride() => _gravityForceOverride = null; |
| | | 75 | | |
| | | 76 | | /// <summary> |
| | | 77 | | /// Removes the per-instance terminal velocity override so this object tracks |
| | | 78 | | /// <see cref="GlobalEnvironmentForces"/>. |
| | | 79 | | /// </summary> |
| | 1 | 80 | | public void ClearTerminalVelocityOverride() => _terminalVelocityOverride = null; |
| | | 81 | | |
| | | 82 | | #endregion |
| | | 83 | | |
| | | 84 | | #region Serialization |
| | | 85 | | |
| | | 86 | | /// <inheritdoc /> |
| | | 87 | | public void RecordData(IChronicler chronicler) |
| | | 88 | | { |
| | | 89 | | // Gravity and terminal velocity are stored as per-instance overrides. |
| | | 90 | | // Serialize the presence flag first so loading knows whether to restore an override or clear it. |
| | 106 | 91 | | bool hasGravityOverride = _gravityForceOverride.HasValue; |
| | 106 | 92 | | Fixed64 gravityForce = _gravityForceOverride ?? GlobalForces.GravityForce; |
| | 106 | 93 | | RecordValues.Look(chronicler, ref hasGravityOverride, "HasGravityOverride", false); |
| | 106 | 94 | | RecordValues.Look(chronicler, ref gravityForce, "GravityForce", GlobalForces.GravityForce); |
| | | 95 | | |
| | 106 | 96 | | bool hasTerminalVelocityOverride = _terminalVelocityOverride.HasValue; |
| | 106 | 97 | | Fixed64 terminalVelocity = _terminalVelocityOverride ?? GlobalForces.TerminalVelocity; |
| | 106 | 98 | | RecordValues.Look(chronicler, ref hasTerminalVelocityOverride, "HasTerminalVelocityOverride", false); |
| | 106 | 99 | | RecordValues.Look(chronicler, ref terminalVelocity, "TerminalVelocity", GlobalForces.TerminalVelocity); |
| | | 100 | | |
| | 106 | 101 | | if (chronicler.Mode == SerializationMode.Loading) |
| | | 102 | | { |
| | 53 | 103 | | _gravityForceOverride = hasGravityOverride ? gravityForce : null; |
| | 53 | 104 | | _terminalVelocityOverride = hasTerminalVelocityOverride ? terminalVelocity : null; |
| | | 105 | | } |
| | 106 | 106 | | } |
| | | 107 | | |
| | | 108 | | #endregion |
| | | 109 | | } |