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