| | | 1 | | //======================================================================= |
| | | 2 | | // ColliderHierarchyKey.cs |
| | | 3 | | //======================================================================= |
| | | 4 | | // MIT License, Copyright (c) 2026–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 | | |
| | | 11 | | namespace Gravitas.Colliders; |
| | | 12 | | |
| | | 13 | | internal readonly struct ColliderHierarchyKey : IEquatable<ColliderHierarchyKey> |
| | | 14 | | { |
| | | 15 | | private const byte NoneDimension = 0; |
| | | 16 | | private const byte ThreeDDimension = 1; |
| | | 17 | | private const byte TwoDDimension = 2; |
| | | 18 | | |
| | 1 | 19 | | public static readonly ColliderHierarchyKey None = new(NoneDimension, -1); |
| | | 20 | | |
| | | 21 | | private readonly byte _dimension; |
| | | 22 | | private readonly int _id; |
| | | 23 | | |
| | | 24 | | private ColliderHierarchyKey(byte dimension, int id) |
| | | 25 | | { |
| | 238131 | 26 | | _dimension = dimension; |
| | 238131 | 27 | | _id = id; |
| | 238131 | 28 | | } |
| | | 29 | | |
| | | 30 | | public byte Dimension |
| | | 31 | | { |
| | | 32 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 3053 | 33 | | get => _dimension; |
| | | 34 | | } |
| | | 35 | | |
| | | 36 | | public int Id |
| | | 37 | | { |
| | | 38 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 3083 | 39 | | get => _id; |
| | | 40 | | } |
| | | 41 | | |
| | | 42 | | public ulong Packed |
| | | 43 | | { |
| | | 44 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 132 | 45 | | get => IsValid ? ((ulong)_dimension << 32) | (uint)_id : 0UL; |
| | | 46 | | } |
| | | 47 | | |
| | | 48 | | public bool IsValid |
| | | 49 | | { |
| | | 50 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 355584 | 51 | | get => _dimension != NoneDimension && _id >= 0; |
| | | 52 | | } |
| | | 53 | | |
| | | 54 | | public bool Is3D |
| | | 55 | | { |
| | | 56 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 2274 | 57 | | get => _dimension == ThreeDDimension; |
| | | 58 | | } |
| | | 59 | | |
| | | 60 | | public bool Is2D |
| | | 61 | | { |
| | | 62 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 807 | 63 | | get => _dimension == TwoDDimension; |
| | | 64 | | } |
| | | 65 | | |
| | | 66 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 67 | | public static ColliderHierarchyKey Create3D(int id) |
| | | 68 | | { |
| | 213521 | 69 | | SwiftThrowHelper.ThrowIfNegative(id, nameof(id)); |
| | 213520 | 70 | | return new ColliderHierarchyKey(ThreeDDimension, id); |
| | | 71 | | } |
| | | 72 | | |
| | | 73 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 74 | | public static ColliderHierarchyKey Create2D(int id) |
| | | 75 | | { |
| | 24606 | 76 | | SwiftThrowHelper.ThrowIfNegative(id, nameof(id)); |
| | 24605 | 77 | | return new ColliderHierarchyKey(TwoDDimension, id); |
| | | 78 | | } |
| | | 79 | | |
| | | 80 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 81 | | public static ColliderHierarchyKey FromPacked(ulong packed) |
| | | 82 | | { |
| | 6 | 83 | | if (packed == 0UL) |
| | 1 | 84 | | return None; |
| | | 85 | | |
| | 5 | 86 | | return new ColliderHierarchyKey((byte)(packed >> 32), (int)(uint)packed); |
| | | 87 | | } |
| | | 88 | | |
| | | 89 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 339085 | 90 | | public bool Equals(ColliderHierarchyKey other) => _dimension == other._dimension && _id == other._id; |
| | | 91 | | |
| | | 92 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 16 | 93 | | public override bool Equals(object? obj) => obj is ColliderHierarchyKey other && Equals(other); |
| | | 94 | | |
| | | 95 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 2 | 96 | | public override int GetHashCode() => Packed.GetHashCode(); |
| | | 97 | | |
| | | 98 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 339007 | 99 | | public static bool operator ==(ColliderHierarchyKey left, ColliderHierarchyKey right) => left.Equals(right); |
| | | 100 | | |
| | | 101 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 64 | 102 | | public static bool operator !=(ColliderHierarchyKey left, ColliderHierarchyKey right) => !left.Equals(right); |
| | | 103 | | } |