| | | 1 | | using Chronicler; |
| | | 2 | | using FixedMathSharp; |
| | | 3 | | using System; |
| | | 4 | | using System.Runtime.CompilerServices; |
| | | 5 | | |
| | | 6 | | namespace Trailblazer.Navigation.Motor; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Represents a movement request for a object to pass to <see cref="NavMotor"/>, |
| | | 10 | | /// containing the current origin, foot position, rotation, direction, speed, and frame-owned locomotion intent. |
| | | 11 | | /// </summary> |
| | | 12 | | [Serializable] |
| | | 13 | | public struct TrekRequest : IRecordable |
| | | 14 | | { |
| | | 15 | | /// <summary> |
| | | 16 | | /// The world position from which the scout is requesting movement. |
| | | 17 | | /// </summary> |
| | | 18 | | public Vector3d Origin; |
| | | 19 | | |
| | | 20 | | /// <summary> |
| | | 21 | | /// The position of the scout's foot in world space, used for ground detection and platform interaction. |
| | | 22 | | /// </summary> |
| | | 23 | | public Vector3d? FootPosition; |
| | | 24 | | |
| | | 25 | | /// <summary> |
| | | 26 | | /// The desired rotation of the scout in world space, used for orientation and facing direction. |
| | | 27 | | /// </summary> |
| | | 28 | | public FixedQuaternion Rotation; |
| | | 29 | | |
| | | 30 | | /// <summary> |
| | | 31 | | /// Normalized distance of movement |
| | | 32 | | /// </summary> |
| | | 33 | | public Vector3d Direction; |
| | | 34 | | |
| | | 35 | | /// <summary> |
| | | 36 | | /// Optional world-space facing direction that overrides the default "face movement" behavior for this request. |
| | | 37 | | /// </summary> |
| | | 38 | | public Vector3d? FacingDirection; |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// The speed at which the scout wants to move. |
| | | 42 | | /// </summary> |
| | | 43 | | public TrekRate Rate; |
| | | 44 | | |
| | | 45 | | /// <summary> |
| | | 46 | | /// Indicates whether the scout is requesting to jump. |
| | | 47 | | /// </summary> |
| | | 48 | | public bool IsRequestingJump; |
| | | 49 | | |
| | | 50 | | /// <summary> |
| | | 51 | | /// Per-frame host query result that authoritatively answers whether jump may spend host-owned resources this frame. |
| | | 52 | | /// </summary> |
| | | 53 | | public bool CanAffordJump; |
| | | 54 | | |
| | | 55 | | /// <summary> |
| | | 56 | | /// Indicates whether the scout is requesting controlled flight. |
| | | 57 | | /// </summary> |
| | | 58 | | public bool IsRequestingFlight; |
| | | 59 | | |
| | | 60 | | /// <summary> |
| | | 61 | | /// Indicates whether the scout is requesting active swimming while traversing liquid. |
| | | 62 | | /// </summary> |
| | | 63 | | public bool IsRequestingSwim; |
| | | 64 | | |
| | | 65 | | /// <summary> |
| | | 66 | | /// Indicates whether the scout is requesting climb engagement or continuation. |
| | | 67 | | /// </summary> |
| | | 68 | | public bool IsRequestingClimb; |
| | | 69 | | |
| | | 70 | | /// <summary> |
| | | 71 | | /// Initializes a new instance of the TrekRequest class with default values. |
| | | 72 | | /// </summary> |
| | | 73 | | public TrekRequest() |
| | | 74 | | { |
| | 457 | 75 | | CanAffordJump = true; |
| | 457 | 76 | | } |
| | | 77 | | |
| | | 78 | | /// <summary> |
| | | 79 | | /// Sets the current movement request parameters, including direction, rate, and action flags such as jump, flight, |
| | | 80 | | /// </summary> |
| | | 81 | | /// <param name="direction">The movement direction vector to apply for the request.</param> |
| | | 82 | | /// <param name="rate">The rate at which the movement should be performed.</param> |
| | | 83 | | /// <param name="isRequestingJump">true to request a jump action; otherwise, false.</param> |
| | | 84 | | /// <param name="isRequestingFlight">true to request a flight action; otherwise, false.</param> |
| | | 85 | | /// <param name="isRequestingSwim">true to request active swim control while in liquid; otherwise, false.</param> |
| | | 86 | | /// <param name="isRequestingClimb">true to request a climb action; otherwise, false.</param> |
| | | 87 | | /// <param name="facingDirection"> |
| | | 88 | | /// An optional vector specifying the desired facing direction. |
| | | 89 | | /// If null or equal to Vector3d.Zero, the facing direction is not changed.</param> |
| | | 90 | | /// <param name="canAffordJump">true if the jump action can be afforded; otherwise, false. Defaults to true.</param> |
| | | 91 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 92 | | public void SetRequest( |
| | | 93 | | Vector3d direction, |
| | | 94 | | TrekRate rate, |
| | | 95 | | bool isRequestingJump, |
| | | 96 | | bool isRequestingFlight, |
| | | 97 | | bool isRequestingSwim, |
| | | 98 | | bool isRequestingClimb, |
| | | 99 | | Vector3d? facingDirection = null, |
| | | 100 | | bool canAffordJump = true) |
| | | 101 | | { |
| | 70 | 102 | | Direction = direction; |
| | 70 | 103 | | FacingDirection = facingDirection.HasValue && facingDirection.Value != Vector3d.Zero |
| | 70 | 104 | | ? facingDirection |
| | 70 | 105 | | : null; |
| | 70 | 106 | | Rate = rate; |
| | 70 | 107 | | IsRequestingJump = isRequestingJump; |
| | 70 | 108 | | CanAffordJump = canAffordJump; |
| | 70 | 109 | | IsRequestingFlight = isRequestingFlight; |
| | 70 | 110 | | IsRequestingSwim = isRequestingSwim; |
| | 70 | 111 | | IsRequestingClimb = isRequestingClimb; |
| | 70 | 112 | | } |
| | | 113 | | |
| | | 114 | | /// <summary> |
| | | 115 | | /// Sets the transient state for the current frame, including origin, optional foot position, rotation, and optional |
| | | 116 | | /// </summary> |
| | | 117 | | /// <remarks> |
| | | 118 | | /// If <paramref name="direction"/> is null, the current direction is retained for this frame. |
| | | 119 | | /// This method is intended for per-frame updates where only some state components may change. |
| | | 120 | | /// </remarks> |
| | | 121 | | /// <param name="origin">The new origin position to set for the transient state.</param> |
| | | 122 | | /// <param name="footPosition">The foot position to set for the transient state, or null to leave it unset.</param> |
| | | 123 | | /// <param name="rotation">The rotation to apply to the transient state.</param> |
| | | 124 | | /// <param name="direction">The direction to set for the transient state, or null to preserve the existing direction |
| | | 125 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 126 | | public void SetTransientState( |
| | | 127 | | Vector3d origin, |
| | | 128 | | Vector3d? footPosition, |
| | | 129 | | FixedQuaternion rotation, |
| | | 130 | | Vector3d? direction) |
| | | 131 | | { |
| | 44 | 132 | | Origin = origin; |
| | 44 | 133 | | FootPosition = footPosition; |
| | 44 | 134 | | Rotation = rotation; |
| | | 135 | | |
| | | 136 | | // Only update direction if a new value is provided, |
| | | 137 | | // otherwise preserve the existing direction for this frame. |
| | 44 | 138 | | if (direction.HasValue) |
| | 36 | 139 | | Direction = direction.Value; |
| | 44 | 140 | | } |
| | | 141 | | |
| | | 142 | | /// <summary> |
| | | 143 | | /// Creates a deep copy of the current <see cref="TrekRequest"/> instance. |
| | | 144 | | /// </summary> |
| | | 145 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1 | 146 | | public TrekRequest Clone() => new() |
| | 1 | 147 | | { |
| | 1 | 148 | | Origin = Origin, |
| | 1 | 149 | | Rotation = Rotation, |
| | 1 | 150 | | Direction = Direction, |
| | 1 | 151 | | FacingDirection = FacingDirection, |
| | 1 | 152 | | Rate = Rate, |
| | 1 | 153 | | IsRequestingJump = IsRequestingJump, |
| | 1 | 154 | | CanAffordJump = CanAffordJump, |
| | 1 | 155 | | IsRequestingFlight = IsRequestingFlight, |
| | 1 | 156 | | IsRequestingSwim = IsRequestingSwim, |
| | 1 | 157 | | IsRequestingClimb = IsRequestingClimb, |
| | 1 | 158 | | FootPosition = FootPosition |
| | 1 | 159 | | }; |
| | | 160 | | |
| | | 161 | | /// <summary> |
| | | 162 | | /// Resets the per-frame movement data. |
| | | 163 | | /// </summary> |
| | | 164 | | public void Reset() |
| | | 165 | | { |
| | 2034 | 166 | | Origin = Vector3d.Zero; |
| | 2034 | 167 | | FootPosition = null; |
| | 2034 | 168 | | Rotation = FixedQuaternion.Identity; |
| | 2034 | 169 | | Direction = Vector3d.Zero; |
| | 2034 | 170 | | FacingDirection = null; |
| | 2034 | 171 | | IsRequestingJump = false; |
| | 2034 | 172 | | CanAffordJump = true; |
| | 2034 | 173 | | Rate = TrekRate.Stationary; |
| | 2034 | 174 | | IsRequestingFlight = false; |
| | 2034 | 175 | | IsRequestingSwim = false; |
| | 2034 | 176 | | IsRequestingClimb = false; |
| | 2034 | 177 | | } |
| | | 178 | | |
| | | 179 | | /// <summary> |
| | | 180 | | /// Resets only the transient movement data that should be cleared each frame, |
| | | 181 | | /// while preserving any persistent data that may be set externally. |
| | | 182 | | /// </summary> |
| | | 183 | | public void ResetTransient() |
| | | 184 | | { |
| | 3 | 185 | | Origin = Vector3d.Zero; |
| | 3 | 186 | | FootPosition = null; |
| | 3 | 187 | | Rotation = FixedQuaternion.Identity; |
| | 3 | 188 | | Direction = Vector3d.Zero; |
| | 3 | 189 | | IsRequestingJump = false; |
| | 3 | 190 | | CanAffordJump = true; |
| | 3 | 191 | | IsRequestingClimb = false; |
| | 3 | 192 | | } |
| | | 193 | | |
| | | 194 | | /// <inheritdoc/> |
| | | 195 | | public void RecordData(IChronicler chronicler) |
| | | 196 | | { |
| | 130 | 197 | | RecordValues.Look(chronicler, ref Origin, "Origin", Vector3d.Zero); |
| | 130 | 198 | | RecordValues.Look(chronicler, ref FootPosition, "FootPosition", null); |
| | 130 | 199 | | RecordValues.Look(chronicler, ref Rotation, "Rotation", FixedQuaternion.Identity); |
| | 130 | 200 | | RecordValues.Look(chronicler, ref Direction, "Direction", Vector3d.Zero); |
| | 130 | 201 | | RecordValues.Look(chronicler, ref FacingDirection, "FacingDirection", null); |
| | 130 | 202 | | RecordValues.Look(chronicler, ref Rate, "Rate", TrekRate.Stationary); |
| | 130 | 203 | | RecordValues.Look(chronicler, ref IsRequestingJump, "IsRequestingJump", false); |
| | 130 | 204 | | RecordValues.Look(chronicler, ref CanAffordJump, "CanAffordJump", true); |
| | 130 | 205 | | RecordValues.Look(chronicler, ref IsRequestingFlight, "IsRequestingFlight", false); |
| | 130 | 206 | | RecordValues.Look(chronicler, ref IsRequestingSwim, "IsRequestingSwim", false); |
| | 130 | 207 | | RecordValues.Look(chronicler, ref IsRequestingClimb, "IsRequestingClimb", false); |
| | 130 | 208 | | } |
| | | 209 | | } |