| | | 1 | | using Chronicler; |
| | | 2 | | using SwiftCollections; |
| | | 3 | | using System; |
| | | 4 | | using System.Collections.Generic; |
| | | 5 | | using System.Diagnostics.CodeAnalysis; |
| | | 6 | | using System.Runtime.CompilerServices; |
| | | 7 | | |
| | | 8 | | namespace Trailblazer.Navigation.Motor; |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// Manages locomotion states and behaviors for the <see cref="NavMotor"/>. |
| | | 12 | | /// </summary> |
| | | 13 | | /// <remarks> |
| | | 14 | | /// This class coordinates multiple locomotion types, ensuring that movement states are properly managed. |
| | | 15 | | /// </remarks> |
| | | 16 | | public class LocomotionHandler : IRecordable |
| | | 17 | | { |
| | | 18 | | #region Nested Types |
| | | 19 | | |
| | | 20 | | private enum LocomotionSlot |
| | | 21 | | { |
| | | 22 | | Move, |
| | | 23 | | Platform, |
| | | 24 | | Jump, |
| | | 25 | | Fall, |
| | | 26 | | Slide, |
| | | 27 | | Water, |
| | | 28 | | Fly, |
| | | 29 | | Climb |
| | | 30 | | } |
| | | 31 | | |
| | | 32 | | #endregion |
| | | 33 | | |
| | | 34 | | #region Fields |
| | | 35 | | |
| | | 36 | | /// <summary> |
| | | 37 | | /// Determines whether the scout has control over movement input. |
| | | 38 | | /// </summary> |
| | 749 | 39 | | public bool IsInControl = true; |
| | | 40 | | |
| | 749 | 41 | | private LocomotionForces _forces = new(); |
| | | 42 | | |
| | | 43 | | private MoveLocomotion _move = null!; |
| | | 44 | | |
| | | 45 | | private FallLocomotion _fall = null!; |
| | | 46 | | |
| | | 47 | | private PlatformLocomotion _platform = null!; |
| | | 48 | | |
| | | 49 | | private JumpLocomotion? _jump; |
| | | 50 | | |
| | | 51 | | private SlideLocomotion? _slide; |
| | | 52 | | |
| | | 53 | | private WaterLocomotion? _water; |
| | | 54 | | |
| | | 55 | | private FlyLocomotion? _fly; |
| | | 56 | | |
| | | 57 | | private ClimbLocomotion? _climb; |
| | | 58 | | |
| | | 59 | | private TrailblazerWorldContext? _context; |
| | | 60 | | |
| | | 61 | | #endregion |
| | | 62 | | |
| | | 63 | | #region Initialization |
| | | 64 | | |
| | | 65 | | /// <summary> |
| | | 66 | | /// Initializes a new handler using the default locomotion profile. |
| | | 67 | | /// </summary> |
| | | 68 | | public LocomotionHandler() |
| | 1182 | 69 | | : this(LocomotionProfile.CreateDefault()) { } |
| | | 70 | | |
| | | 71 | | /// <summary> |
| | | 72 | | /// Initializes a new handler from a locomotion profile. |
| | | 73 | | /// </summary> |
| | 749 | 74 | | public LocomotionHandler(LocomotionProfile profile) |
| | | 75 | | { |
| | 749 | 76 | | ApplyProfile(profile ?? throw new ArgumentNullException(nameof(profile))); |
| | 748 | 77 | | } |
| | | 78 | | |
| | | 79 | | #endregion |
| | | 80 | | |
| | | 81 | | #region Properties |
| | | 82 | | |
| | | 83 | | /// <summary> |
| | | 84 | | /// Gets the currently installed locomotion kinds. |
| | | 85 | | /// </summary> |
| | | 86 | | public LocomotionKind InstalledKinds { get; private set; } = LocomotionKind.All; |
| | | 87 | | |
| | | 88 | | /// <inheritdoc cref="LocomotionForces"/> |
| | 3794 | 89 | | public LocomotionForces Forces => _forces; |
| | | 90 | | |
| | | 91 | | /// <summary> |
| | | 92 | | /// Handles general movement, including speed limits, acceleration, and velocity calculations. |
| | | 93 | | /// </summary> |
| | 12101 | 94 | | public MoveLocomotion Move => _move; |
| | | 95 | | |
| | | 96 | | /// <summary> |
| | | 97 | | /// Manages movement when interacting with moving platforms or surfaces. |
| | | 98 | | /// </summary> |
| | | 99 | | /// <remarks> |
| | | 100 | | /// This locomotion maintains platform velocity tracking and movement transfer states. |
| | | 101 | | /// </remarks> |
| | 13357 | 102 | | public PlatformLocomotion Platform => _platform; |
| | | 103 | | |
| | | 104 | | /// <summary> |
| | | 105 | | /// Controls the airborne state when a jump is executed successfully. |
| | | 106 | | /// </summary> |
| | | 107 | | /// <remarks> |
| | | 108 | | /// This locomotion governs jump height, cooldown timing, and jump force calculations. |
| | | 109 | | /// </remarks> |
| | 19405 | 110 | | public JumpLocomotion? Jump => _jump; |
| | | 111 | | |
| | | 112 | | /// <summary> |
| | | 113 | | /// Handles the scout’s falling behavior when downward momentum is detected. |
| | | 114 | | /// </summary> |
| | | 115 | | /// <remarks> |
| | | 116 | | /// This locomotion tracks fall distance, applies landing impact logic, and determines if a scout is free-falling. |
| | | 117 | | /// </remarks> |
| | 14296 | 118 | | public FallLocomotion Fall => _fall; |
| | | 119 | | |
| | | 120 | | /// <summary> |
| | | 121 | | /// Manages movement when sliding down steep surfaces. |
| | | 122 | | /// </summary> |
| | | 123 | | /// <remarks> |
| | | 124 | | /// This locomotion determines when the scout should slide and how much control it has over movement during the slid |
| | | 125 | | /// </remarks> |
| | 4688 | 126 | | public SlideLocomotion? Slide => _slide; |
| | | 127 | | |
| | | 128 | | /// <summary> |
| | | 129 | | /// Handles movement when the scout is in water, including active swimming, buoyancy, and water resistance. |
| | | 130 | | /// </summary> |
| | | 131 | | /// <remarks> |
| | | 132 | | /// This locomotion tracks liquid-medium state such as swim speed, floating and sinking behavior, |
| | | 133 | | /// dive time, and breath management. |
| | | 134 | | /// </remarks> |
| | 10282 | 135 | | public WaterLocomotion? Water => _water; |
| | | 136 | | |
| | | 137 | | /// <summary> |
| | | 138 | | /// Handles controlled flight while the scout is airborne. |
| | | 139 | | /// </summary> |
| | 26286 | 140 | | public FlyLocomotion? Fly => _fly; |
| | | 141 | | |
| | | 142 | | /// <summary> |
| | | 143 | | /// Handles climb configuration and runtime attachment state. |
| | | 144 | | /// </summary> |
| | 25489 | 145 | | public ClimbLocomotion? Climb => _climb; |
| | | 146 | | |
| | | 147 | | #endregion |
| | | 148 | | |
| | | 149 | | #region Composition |
| | | 150 | | |
| | | 151 | | /// <summary> |
| | | 152 | | /// Gets whether a built-in locomotion kind is installed. |
| | | 153 | | /// </summary> |
| | | 154 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 155 | | public bool Has(LocomotionKind kind) |
| | | 156 | | { |
| | 2 | 157 | | return (InstalledKinds & kind) == kind; |
| | | 158 | | } |
| | | 159 | | |
| | | 160 | | /// <summary> |
| | | 161 | | /// Gets whether a built-in locomotion type is installed. |
| | | 162 | | /// </summary> |
| | | 163 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 164 | | public bool Has<T>() where T : class, ILocomotion |
| | | 165 | | { |
| | 3 | 166 | | return TryGet<T>(out _); |
| | | 167 | | } |
| | | 168 | | |
| | | 169 | | /// <summary> |
| | | 170 | | /// Attempts to retrieve an installed locomotion by type. |
| | | 171 | | /// </summary> |
| | | 172 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 173 | | public bool TryGet<T>([NotNullWhen(true)] out T? locomotion) where T : class, ILocomotion |
| | | 174 | | { |
| | 9 | 175 | | locomotion = GetLocomotion(typeof(T)) as T; |
| | 9 | 176 | | return locomotion != null; |
| | | 177 | | } |
| | | 178 | | |
| | | 179 | | /// <summary> |
| | | 180 | | /// Retrieves an installed locomotion by type or throws if it is not installed. |
| | | 181 | | /// </summary> |
| | | 182 | | public T Require<T>() where T : class, ILocomotion |
| | | 183 | | { |
| | 3 | 184 | | if (TryGet<T>(out T? locomotion)) |
| | 1 | 185 | | return locomotion; |
| | | 186 | | |
| | 2 | 187 | | throw new InvalidOperationException($"{typeof(T).Name} is not installed on this locomotion handler."); |
| | | 188 | | } |
| | | 189 | | |
| | | 190 | | /// <summary> |
| | | 191 | | /// Installs or replaces a locomotion instance. |
| | | 192 | | /// </summary> |
| | | 193 | | public void Install<T>(T locomotion) where T : class, ILocomotion |
| | | 194 | | { |
| | 3 | 195 | | Replace(locomotion); |
| | 3 | 196 | | } |
| | | 197 | | |
| | | 198 | | /// <summary> |
| | | 199 | | /// Replaces a locomotion instance with a new one of the same type. |
| | | 200 | | /// </summary> |
| | | 201 | | public void Replace<T>(T locomotion) where T : class, ILocomotion |
| | | 202 | | { |
| | 13 | 203 | | SwiftThrowHelper.ThrowIfNull(locomotion, nameof(locomotion)); |
| | | 204 | | |
| | 12 | 205 | | SetLocomotion(typeof(T), locomotion); |
| | 11 | 206 | | RefreshInstalledKinds(); |
| | 11 | 207 | | } |
| | | 208 | | |
| | | 209 | | /// <summary> |
| | | 210 | | /// Removes an optional locomotion from the handler. |
| | | 211 | | /// </summary> |
| | | 212 | | public bool Remove<T>() where T : class, ILocomotion |
| | | 213 | | { |
| | 7 | 214 | | Type type = typeof(T); |
| | 7 | 215 | | if (type == typeof(MoveLocomotion) |
| | 7 | 216 | | || type == typeof(PlatformLocomotion) |
| | 7 | 217 | | || type == typeof(FallLocomotion)) |
| | | 218 | | { |
| | 3 | 219 | | return false; |
| | | 220 | | } |
| | | 221 | | |
| | 4 | 222 | | ILocomotion? locomotion = GetLocomotion(type); |
| | 4 | 223 | | if (locomotion == null) |
| | 1 | 224 | | return false; |
| | | 225 | | |
| | 3 | 226 | | locomotion.ClearTransientState(); |
| | | 227 | | |
| | 3 | 228 | | SetLocomotion(type, null); |
| | 3 | 229 | | RefreshInstalledKinds(); |
| | 3 | 230 | | return true; |
| | | 231 | | } |
| | | 232 | | |
| | | 233 | | /// <summary> |
| | | 234 | | /// Replaces the installed locomotions with a new profile. |
| | | 235 | | /// </summary> |
| | | 236 | | public void ApplyProfile(LocomotionProfile profile) |
| | | 237 | | { |
| | 866 | 238 | | SwiftThrowHelper.ThrowIfNull(profile, nameof(profile)); |
| | | 239 | | |
| | 865 | 240 | | ClearReplacedLocomotion(Move, profile.Move); |
| | 865 | 241 | | ClearReplacedLocomotion(Fall, profile.Fall); |
| | 865 | 242 | | ClearReplacedLocomotion(Platform, profile.Platform); |
| | 865 | 243 | | ClearReplacedLocomotion(Jump, profile.Jump); |
| | 865 | 244 | | ClearReplacedLocomotion(Slide, profile.Slide); |
| | 865 | 245 | | ClearReplacedLocomotion(Water, profile.Water); |
| | 865 | 246 | | ClearReplacedLocomotion(Fly, profile.Fly); |
| | 865 | 247 | | ClearReplacedLocomotion(Climb, profile.Climb); |
| | | 248 | | |
| | 865 | 249 | | _move = profile.Move; |
| | 865 | 250 | | _fall = profile.Fall; |
| | 865 | 251 | | _platform = profile.Platform; |
| | 865 | 252 | | _jump = profile.Jump; |
| | 865 | 253 | | _slide = profile.Slide; |
| | 865 | 254 | | _water = profile.Water; |
| | 865 | 255 | | _fly = profile.Fly; |
| | 865 | 256 | | _climb = profile.Climb; |
| | | 257 | | |
| | 865 | 258 | | BindInstalledLocomotions(); |
| | 865 | 259 | | RefreshInstalledKinds(); |
| | 865 | 260 | | } |
| | | 261 | | |
| | | 262 | | internal void BindContext(TrailblazerWorldContext context) |
| | | 263 | | { |
| | 779 | 264 | | Trailblazer.Pathing.PathRequestContextResolver.ThrowIfUnusable(context); |
| | 779 | 265 | | _context = context; |
| | 779 | 266 | | BindInstalledLocomotions(); |
| | 779 | 267 | | } |
| | | 268 | | |
| | | 269 | | /// <summary> |
| | | 270 | | /// Creates a profile representing the handler's current locomotion composition. |
| | | 271 | | /// </summary> |
| | | 272 | | public LocomotionProfile ToProfile() |
| | | 273 | | { |
| | 1 | 274 | | return new LocomotionProfile( |
| | 1 | 275 | | Move, |
| | 1 | 276 | | Fall, |
| | 1 | 277 | | Platform, |
| | 1 | 278 | | Jump, |
| | 1 | 279 | | Slide, |
| | 1 | 280 | | Water, |
| | 1 | 281 | | Fly, |
| | 1 | 282 | | Climb); |
| | | 283 | | } |
| | | 284 | | |
| | | 285 | | internal void ConfigureInstalledKinds(LocomotionKind kinds) |
| | | 286 | | { |
| | 56 | 287 | | LocomotionKind normalizedKinds = kinds | LocomotionKind.Core; |
| | 56 | 288 | | var builder = new LocomotionProfileBuilder(includeOptionalLocomotions: false); |
| | | 289 | | |
| | 56 | 290 | | if ((normalizedKinds & LocomotionKind.Jump) != 0) |
| | 50 | 291 | | builder.WithJump(); |
| | | 292 | | |
| | 56 | 293 | | if ((normalizedKinds & LocomotionKind.Slide) != 0) |
| | 50 | 294 | | builder.WithSlide(); |
| | | 295 | | |
| | 56 | 296 | | if ((normalizedKinds & LocomotionKind.Water) != 0) |
| | 50 | 297 | | builder.WithWater(); |
| | | 298 | | |
| | 56 | 299 | | if ((normalizedKinds & LocomotionKind.Fly) != 0) |
| | 51 | 300 | | builder.WithFly(); |
| | | 301 | | |
| | 56 | 302 | | if ((normalizedKinds & LocomotionKind.Climb) != 0) |
| | 51 | 303 | | builder.WithClimb(); |
| | | 304 | | |
| | 56 | 305 | | ApplyProfile(builder.Build()); |
| | 56 | 306 | | } |
| | | 307 | | |
| | | 308 | | private void RefreshInstalledKinds() |
| | | 309 | | { |
| | 879 | 310 | | InstalledKinds = LocomotionKind.Core; |
| | | 311 | | |
| | 879 | 312 | | if (Jump != null) |
| | 841 | 313 | | InstalledKinds |= LocomotionKind.Jump; |
| | | 314 | | |
| | 879 | 315 | | if (Slide != null) |
| | 834 | 316 | | InstalledKinds |= LocomotionKind.Slide; |
| | | 317 | | |
| | 879 | 318 | | if (Water != null) |
| | 836 | 319 | | InstalledKinds |= LocomotionKind.Water; |
| | | 320 | | |
| | 879 | 321 | | if (Fly != null) |
| | 836 | 322 | | InstalledKinds |= LocomotionKind.Fly; |
| | | 323 | | |
| | 879 | 324 | | if (Climb != null) |
| | 835 | 325 | | InstalledKinds |= LocomotionKind.Climb; |
| | 879 | 326 | | } |
| | | 327 | | |
| | | 328 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 329 | | private ILocomotion? GetLocomotion(Type type) |
| | | 330 | | { |
| | 57 | 331 | | if (!TryResolveLocomotionSlot(type, out LocomotionSlot slot)) |
| | 3 | 332 | | return null; |
| | | 333 | | |
| | 54 | 334 | | return GetLocomotion(slot); |
| | | 335 | | } |
| | | 336 | | |
| | | 337 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 338 | | private ILocomotion? GetLocomotion(LocomotionSlot slot) |
| | | 339 | | { |
| | | 340 | | #pragma warning disable CS8524 // LocomotionSlot is only produced by TryResolveLocomotionSlot over known built-in locomo |
| | 54 | 341 | | return slot switch |
| | 54 | 342 | | { |
| | 5 | 343 | | LocomotionSlot.Move => Move, |
| | 1 | 344 | | LocomotionSlot.Platform => Platform, |
| | 5 | 345 | | LocomotionSlot.Jump => Jump, |
| | 19 | 346 | | LocomotionSlot.Fall => Fall, |
| | 2 | 347 | | LocomotionSlot.Slide => Slide, |
| | 8 | 348 | | LocomotionSlot.Water => Water, |
| | 1 | 349 | | LocomotionSlot.Fly => Fly, |
| | 13 | 350 | | LocomotionSlot.Climb => Climb |
| | 54 | 351 | | }; |
| | | 352 | | #pragma warning restore CS8524 |
| | | 353 | | } |
| | | 354 | | |
| | | 355 | | /// <summary> |
| | | 356 | | /// Gets and enumerates all locomotion instances in the handler. |
| | | 357 | | /// </summary> |
| | | 358 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 359 | | public IEnumerable<ILocomotion> GetLocomotions() |
| | | 360 | | { |
| | 8 | 361 | | yield return Move; |
| | 8 | 362 | | yield return Platform; |
| | | 363 | | |
| | 8 | 364 | | if (Jump != null) |
| | 8 | 365 | | yield return Jump; |
| | | 366 | | |
| | 8 | 367 | | yield return Fall; |
| | | 368 | | |
| | 8 | 369 | | if (Water != null) |
| | 8 | 370 | | yield return Water; |
| | | 371 | | |
| | 8 | 372 | | if (Fly != null) |
| | 8 | 373 | | yield return Fly; |
| | | 374 | | |
| | 8 | 375 | | if (Climb != null) |
| | 8 | 376 | | yield return Climb; |
| | | 377 | | |
| | 8 | 378 | | if (Slide != null) |
| | 8 | 379 | | yield return Slide; |
| | 8 | 380 | | } |
| | | 381 | | |
| | | 382 | | private void SetLocomotion(Type type, ILocomotion? locomotion) |
| | | 383 | | { |
| | 15 | 384 | | if (!TryResolveLocomotionSlot(type, out LocomotionSlot slot)) |
| | 1 | 385 | | throw new NotSupportedException($"Unsupported locomotion type '{type.Name}'."); |
| | | 386 | | |
| | 14 | 387 | | SetLocomotion(slot, locomotion); |
| | 14 | 388 | | } |
| | | 389 | | |
| | | 390 | | private static bool TryResolveLocomotionSlot(Type type, out LocomotionSlot slot) |
| | | 391 | | { |
| | 72 | 392 | | if (type == typeof(MoveLocomotion)) |
| | | 393 | | { |
| | 6 | 394 | | slot = LocomotionSlot.Move; |
| | 6 | 395 | | return true; |
| | | 396 | | } |
| | | 397 | | |
| | 66 | 398 | | if (type == typeof(PlatformLocomotion)) |
| | | 399 | | { |
| | 2 | 400 | | slot = LocomotionSlot.Platform; |
| | 2 | 401 | | return true; |
| | | 402 | | } |
| | | 403 | | |
| | 64 | 404 | | if (type == typeof(JumpLocomotion)) |
| | | 405 | | { |
| | 8 | 406 | | slot = LocomotionSlot.Jump; |
| | 8 | 407 | | return true; |
| | | 408 | | } |
| | | 409 | | |
| | 56 | 410 | | if (type == typeof(FallLocomotion)) |
| | | 411 | | { |
| | 20 | 412 | | slot = LocomotionSlot.Fall; |
| | 20 | 413 | | return true; |
| | | 414 | | } |
| | | 415 | | |
| | 36 | 416 | | if (type == typeof(SlideLocomotion)) |
| | | 417 | | { |
| | 4 | 418 | | slot = LocomotionSlot.Slide; |
| | 4 | 419 | | return true; |
| | | 420 | | } |
| | | 421 | | |
| | 32 | 422 | | if (type == typeof(WaterLocomotion)) |
| | | 423 | | { |
| | 11 | 424 | | slot = LocomotionSlot.Water; |
| | 11 | 425 | | return true; |
| | | 426 | | } |
| | | 427 | | |
| | 21 | 428 | | if (type == typeof(FlyLocomotion)) |
| | | 429 | | { |
| | 2 | 430 | | slot = LocomotionSlot.Fly; |
| | 2 | 431 | | return true; |
| | | 432 | | } |
| | | 433 | | |
| | 19 | 434 | | if (type == typeof(ClimbLocomotion)) |
| | | 435 | | { |
| | 15 | 436 | | slot = LocomotionSlot.Climb; |
| | 15 | 437 | | return true; |
| | | 438 | | } |
| | | 439 | | |
| | 4 | 440 | | slot = default; |
| | 4 | 441 | | return false; |
| | | 442 | | } |
| | | 443 | | |
| | | 444 | | private void SetLocomotion(LocomotionSlot slot, ILocomotion? locomotion) |
| | | 445 | | { |
| | 14 | 446 | | BindLocomotion(locomotion); |
| | | 447 | | |
| | | 448 | | switch (slot) |
| | | 449 | | { |
| | | 450 | | case LocomotionSlot.Move: |
| | 1 | 451 | | _move = (MoveLocomotion)locomotion!; |
| | 1 | 452 | | return; |
| | | 453 | | case LocomotionSlot.Platform: |
| | 1 | 454 | | _platform = (PlatformLocomotion)locomotion!; |
| | 1 | 455 | | return; |
| | | 456 | | case LocomotionSlot.Jump: |
| | 3 | 457 | | _jump = locomotion as JumpLocomotion; |
| | 3 | 458 | | return; |
| | | 459 | | case LocomotionSlot.Fall: |
| | 1 | 460 | | _fall = (FallLocomotion)locomotion!; |
| | 1 | 461 | | return; |
| | | 462 | | case LocomotionSlot.Slide: |
| | 2 | 463 | | _slide = locomotion as SlideLocomotion; |
| | 2 | 464 | | return; |
| | | 465 | | case LocomotionSlot.Water: |
| | 3 | 466 | | _water = locomotion as WaterLocomotion; |
| | 3 | 467 | | return; |
| | | 468 | | case LocomotionSlot.Fly: |
| | 1 | 469 | | _fly = locomotion as FlyLocomotion; |
| | 1 | 470 | | return; |
| | | 471 | | case LocomotionSlot.Climb: |
| | 2 | 472 | | _climb = locomotion as ClimbLocomotion; |
| | 2 | 473 | | return; |
| | | 474 | | } |
| | | 475 | | } |
| | | 476 | | |
| | | 477 | | private void BindInstalledLocomotions() |
| | | 478 | | { |
| | 1644 | 479 | | BindLocomotion(_move); |
| | 1644 | 480 | | BindLocomotion(_platform); |
| | 1644 | 481 | | BindLocomotion(_jump); |
| | 1644 | 482 | | BindLocomotion(_fall); |
| | 1644 | 483 | | BindLocomotion(_slide); |
| | 1644 | 484 | | BindLocomotion(_water); |
| | 1644 | 485 | | BindLocomotion(_fly); |
| | 1644 | 486 | | BindLocomotion(_climb); |
| | 1644 | 487 | | } |
| | | 488 | | |
| | | 489 | | private void BindLocomotion(ILocomotion? locomotion) |
| | | 490 | | { |
| | 13166 | 491 | | if (_context == null || locomotion == null) |
| | 6164 | 492 | | return; |
| | | 493 | | |
| | | 494 | | switch (locomotion) |
| | | 495 | | { |
| | | 496 | | case JumpLocomotion jump: |
| | 866 | 497 | | jump.BindContext(_context); |
| | 866 | 498 | | break; |
| | | 499 | | case PlatformLocomotion platform: |
| | 893 | 500 | | platform.BindContext(_context); |
| | 893 | 501 | | break; |
| | | 502 | | case WaterLocomotion water: |
| | 864 | 503 | | water.BindContext(_context); |
| | | 504 | | break; |
| | | 505 | | } |
| | 864 | 506 | | } |
| | | 507 | | |
| | | 508 | | private static void ClearReplacedLocomotion(ILocomotion? current, ILocomotion? next) |
| | | 509 | | { |
| | 6920 | 510 | | if (current == null || ReferenceEquals(current, next)) |
| | 6445 | 511 | | return; |
| | | 512 | | |
| | 475 | 513 | | current.ClearTransientState(); |
| | 475 | 514 | | } |
| | | 515 | | |
| | | 516 | | #endregion |
| | | 517 | | |
| | | 518 | | #region Transient State Management |
| | | 519 | | |
| | | 520 | | /// <summary> |
| | | 521 | | /// Synchronizes locomotion states with another <see cref="LocomotionHandler"/> instance. |
| | | 522 | | /// </summary> |
| | | 523 | | /// <remarks> |
| | | 524 | | /// This ensures that all locomotion modules maintain consistent movement behavior when synchronizing states, |
| | | 525 | | /// which is useful for rollback systems or deterministic simulations. |
| | | 526 | | /// </remarks> |
| | | 527 | | /// <param name="other">The locomotion handler instance to sync with.</param> |
| | | 528 | | public void SyncTransientState(LocomotionHandler other) |
| | | 529 | | { |
| | 3 | 530 | | if (other == null) return; |
| | | 531 | | |
| | 1 | 532 | | IsInControl = other.IsInControl; |
| | | 533 | | |
| | 18 | 534 | | foreach (var locomotion in GetLocomotions()) |
| | | 535 | | { |
| | 8 | 536 | | if (!locomotion.IsEnabled) continue; |
| | 7 | 537 | | ILocomotion? otherLocomotion = other.GetLocomotion(locomotion.GetType()); |
| | 7 | 538 | | if (otherLocomotion == null) continue; |
| | 3 | 539 | | locomotion.SyncTransientState(otherLocomotion); |
| | | 540 | | } |
| | 1 | 541 | | } |
| | | 542 | | |
| | | 543 | | /// <summary> |
| | | 544 | | /// Clears the transient state for the locomotion component of the specified type, if it is enabled. |
| | | 545 | | /// </summary> |
| | | 546 | | /// <remarks>This method has no effect if the specified locomotion component is not present or is not enabled.</rema |
| | | 547 | | /// <typeparam name="T">The type of locomotion component for which to clear transient state. Must implement the ILoc |
| | | 548 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 549 | | public void ClearTransientState<T>() where T : ILocomotion |
| | | 550 | | { |
| | 37 | 551 | | var locomotion = GetLocomotion(typeof(T)); |
| | 37 | 552 | | if (locomotion != null && locomotion.IsEnabled) |
| | 37 | 553 | | locomotion.ClearTransientState(); |
| | 37 | 554 | | } |
| | | 555 | | |
| | | 556 | | /// <summary> |
| | | 557 | | /// Clears the transient state of all locomotion modules. |
| | | 558 | | /// </summary> |
| | | 559 | | /// <remarks> |
| | | 560 | | /// This method resets movement states without altering locomotion configurations, |
| | | 561 | | /// ensuring a clean reset of position, velocity, and state-based properties. |
| | | 562 | | /// </remarks> |
| | | 563 | | public void ClearAllTransientState() |
| | | 564 | | { |
| | 108 | 565 | | foreach (var locomotion in GetLocomotions()) |
| | | 566 | | { |
| | 48 | 567 | | if (locomotion.IsEnabled) |
| | 48 | 568 | | locomotion.ClearTransientState(); |
| | | 569 | | } |
| | 6 | 570 | | } |
| | | 571 | | |
| | | 572 | | #endregion |
| | | 573 | | |
| | | 574 | | #region Serialization |
| | | 575 | | |
| | | 576 | | /// <inheritdoc /> |
| | | 577 | | public void RecordData(IChronicler chronicler) |
| | | 578 | | { |
| | 106 | 579 | | RecordValues.Look(chronicler, ref IsInControl, "IsInControl", true); |
| | 106 | 580 | | int installedKinds = (int)InstalledKinds; |
| | 106 | 581 | | RecordValues.Look(chronicler, ref installedKinds, "InstalledKinds", (int)LocomotionKind.All); |
| | | 582 | | |
| | 106 | 583 | | if (chronicler.Mode == SerializationMode.Loading) |
| | 53 | 584 | | ConfigureInstalledKinds((LocomotionKind)installedKinds); |
| | | 585 | | |
| | 106 | 586 | | RecordDeep.Look(chronicler, ref _forces, "Forces"); |
| | 106 | 587 | | RecordDeep.Look(chronicler, ref _move, "Move"); |
| | 106 | 588 | | RecordDeep.Look(chronicler, ref _platform, "Platform"); |
| | 106 | 589 | | RecordOptionalLocomotion(chronicler, ref _jump, "Jump"); |
| | 106 | 590 | | RecordDeep.Look(chronicler, ref _fall, "Fall"); |
| | 106 | 591 | | RecordOptionalLocomotion(chronicler, ref _slide, "Slide"); |
| | 106 | 592 | | RecordOptionalLocomotion(chronicler, ref _water, "Water"); |
| | 106 | 593 | | RecordOptionalLocomotion(chronicler, ref _fly, "Fly"); |
| | 106 | 594 | | RecordOptionalLocomotion(chronicler, ref _climb, "Climb"); |
| | | 595 | | |
| | 106 | 596 | | if (chronicler.Mode == SerializationMode.Loading) |
| | | 597 | | { |
| | 53 | 598 | | ApplyProfile(new LocomotionProfile( |
| | 53 | 599 | | _move, |
| | 53 | 600 | | _fall, |
| | 53 | 601 | | _platform, |
| | 53 | 602 | | _jump, |
| | 53 | 603 | | _slide, |
| | 53 | 604 | | _water, |
| | 53 | 605 | | _fly, |
| | 53 | 606 | | _climb)); |
| | | 607 | | } |
| | 106 | 608 | | } |
| | | 609 | | |
| | | 610 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 611 | | private static void RecordOptionalLocomotion<T>( |
| | | 612 | | IChronicler chronicler, |
| | | 613 | | ref T? locomotion, |
| | | 614 | | string id) |
| | | 615 | | where T : class, ILocomotion, IRecordable |
| | | 616 | | { |
| | 530 | 617 | | T local = locomotion ?? null!; |
| | 530 | 618 | | RecordDeep.Look(chronicler, ref local, id); |
| | 530 | 619 | | locomotion = local; |
| | 530 | 620 | | } |
| | | 621 | | |
| | | 622 | | #endregion |
| | | 623 | | } |