| | | 1 | | //======================================================================= |
| | | 2 | | // KinematicBodyShape.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 FixedMathSharp; |
| | | 10 | | using SwiftCollections.Utility; |
| | | 11 | | |
| | | 12 | | namespace Trailblazer.Pathing; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Defines the authoritative deterministic body geometry used by navigation and movement. |
| | | 16 | | /// </summary> |
| | | 17 | | public readonly struct KinematicBodyShape : IEquatable<KinematicBodyShape> |
| | | 18 | | { |
| | | 19 | | /// <summary> |
| | | 20 | | /// Gets the non-negative horizontal body radius. |
| | | 21 | | /// </summary> |
| | | 22 | | public Fixed64 Radius { get; } |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// Gets the positive body height measured upward from the foot position. |
| | | 26 | | /// </summary> |
| | | 27 | | public Fixed64 Height { get; } |
| | | 28 | | |
| | | 29 | | /// <summary> |
| | | 30 | | /// Gets the non-negative vertical offset from the host root to its foot position. |
| | | 31 | | /// </summary> |
| | | 32 | | public Fixed64 RootToFootOffsetY { get; } |
| | | 33 | | |
| | | 34 | | /// <summary> |
| | | 35 | | /// Creates an immutable body shape. |
| | | 36 | | /// </summary> |
| | | 37 | | /// <exception cref="ArgumentOutOfRangeException">Thrown when a dimension is outside its valid range.</exception> |
| | | 38 | | public KinematicBodyShape(Fixed64 radius, Fixed64 height, Fixed64 rootToFootOffsetY) |
| | | 39 | | { |
| | 1598 | 40 | | SwiftThrowHelper.ThrowIfArgumentOutOfRange( |
| | 1598 | 41 | | radius < Fixed64.Zero, |
| | 1598 | 42 | | actualValue: null, |
| | 1598 | 43 | | nameof(radius), |
| | 1598 | 44 | | "Body radius cannot be negative."); |
| | 1593 | 45 | | SwiftThrowHelper.ThrowIfArgumentOutOfRange( |
| | 1593 | 46 | | height <= Fixed64.Zero, |
| | 1593 | 47 | | actualValue: null, |
| | 1593 | 48 | | nameof(height), |
| | 1593 | 49 | | "Body height must be positive."); |
| | 1591 | 50 | | SwiftThrowHelper.ThrowIfArgumentOutOfRange( |
| | 1591 | 51 | | rootToFootOffsetY < Fixed64.Zero, |
| | 1591 | 52 | | actualValue: null, |
| | 1591 | 53 | | nameof(rootToFootOffsetY), |
| | 1591 | 54 | | "Root-to-foot offset cannot be negative."); |
| | | 55 | | |
| | 1590 | 56 | | Radius = radius; |
| | 1590 | 57 | | Height = height; |
| | 1590 | 58 | | RootToFootOffsetY = rootToFootOffsetY; |
| | 1590 | 59 | | } |
| | | 60 | | |
| | | 61 | | /// <inheritdoc/> |
| | | 62 | | public bool Equals(KinematicBodyShape other) => |
| | 2895 | 63 | | Radius == other.Radius |
| | 2895 | 64 | | && Height == other.Height |
| | 2895 | 65 | | && RootToFootOffsetY == other.RootToFootOffsetY; |
| | | 66 | | |
| | | 67 | | /// <inheritdoc/> |
| | 5 | 68 | | public override bool Equals(object? obj) => obj is KinematicBodyShape other && Equals(other); |
| | | 69 | | |
| | | 70 | | /// <inheritdoc/> |
| | | 71 | | public override int GetHashCode() |
| | | 72 | | { |
| | 3374 | 73 | | int hash = SwiftHashTools.CombineHashCodes(Radius.GetHashCode(), Height.GetHashCode()); |
| | 3374 | 74 | | return SwiftHashTools.CombineHashCodes(hash, RootToFootOffsetY.GetHashCode()); |
| | | 75 | | } |
| | | 76 | | |
| | | 77 | | /// <summary> |
| | | 78 | | /// Returns whether two shapes have exactly equal dimensions. |
| | | 79 | | /// </summary> |
| | 2888 | 80 | | public static bool operator ==(KinematicBodyShape left, KinematicBodyShape right) => left.Equals(right); |
| | | 81 | | |
| | | 82 | | /// <summary> |
| | | 83 | | /// Returns whether two shapes have different dimensions. |
| | | 84 | | /// </summary> |
| | 1 | 85 | | public static bool operator !=(KinematicBodyShape left, KinematicBodyShape right) => !left.Equals(right); |
| | | 86 | | |
| | 27520 | 87 | | internal bool IsValid => Height > Fixed64.Zero; |
| | | 88 | | |
| | | 89 | | internal void Validate(string parameterName) |
| | | 90 | | { |
| | 26788 | 91 | | SwiftThrowHelper.ThrowIfArgument( |
| | 26788 | 92 | | !IsValid, |
| | 26788 | 93 | | parameterName, |
| | 26788 | 94 | | "Body shape must be initialized."); |
| | 26786 | 95 | | } |
| | | 96 | | } |