| | | 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 | | /// <summary> |
| | | 14 | | /// Identifies a collider within its dimension for hierarchy links. |
| | | 15 | | /// </summary> |
| | | 16 | | internal readonly struct ColliderHierarchyKey : IEquatable<ColliderHierarchyKey> |
| | | 17 | | { |
| | | 18 | | private const byte NoneDimension = 0; |
| | | 19 | | private const byte ThreeDDimension = 1; |
| | | 20 | | private const byte TwoDDimension = 2; |
| | | 21 | | |
| | 1 | 22 | | public static readonly ColliderHierarchyKey None = new(NoneDimension, -1); |
| | | 23 | | |
| | | 24 | | private readonly byte _dimension; |
| | | 25 | | private readonly int _id; |
| | | 26 | | |
| | | 27 | | private ColliderHierarchyKey(byte dimension, int id) |
| | | 28 | | { |
| | 5783818 | 29 | | _dimension = dimension; |
| | 5783818 | 30 | | _id = id; |
| | 5783818 | 31 | | } |
| | | 32 | | |
| | | 33 | | public byte Dimension |
| | | 34 | | { |
| | | 35 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 16796 | 36 | | get => _dimension; |
| | | 37 | | } |
| | | 38 | | |
| | | 39 | | public int Id |
| | | 40 | | { |
| | | 41 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 16831 | 42 | | get => _id; |
| | | 43 | | } |
| | | 44 | | |
| | | 45 | | public ulong Packed |
| | | 46 | | { |
| | | 47 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 143 | 48 | | get => IsValid ? ((ulong)_dimension << 32) | (uint)_id : 0UL; |
| | | 49 | | } |
| | | 50 | | |
| | | 51 | | public bool IsValid |
| | | 52 | | { |
| | | 53 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 8695053 | 54 | | get => _dimension != NoneDimension && _id >= 0; |
| | | 55 | | } |
| | | 56 | | |
| | | 57 | | public bool Is3D |
| | | 58 | | { |
| | | 59 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 11123 | 60 | | get => _dimension == ThreeDDimension; |
| | | 61 | | } |
| | | 62 | | |
| | | 63 | | public bool Is2D |
| | | 64 | | { |
| | | 65 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 5709 | 66 | | get => _dimension == TwoDDimension; |
| | | 67 | | } |
| | | 68 | | |
| | | 69 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 70 | | public static ColliderHierarchyKey Create3D(int id) |
| | | 71 | | { |
| | 5353098 | 72 | | ChronicleGuard.ThrowIfNegative(id, nameof(id)); |
| | 5353097 | 73 | | return new ColliderHierarchyKey(ThreeDDimension, id); |
| | | 74 | | } |
| | | 75 | | |
| | | 76 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 77 | | public static ColliderHierarchyKey Create2D(int id) |
| | | 78 | | { |
| | 430711 | 79 | | ChronicleGuard.ThrowIfNegative(id, nameof(id)); |
| | 430710 | 80 | | return new ColliderHierarchyKey(TwoDDimension, id); |
| | | 81 | | } |
| | | 82 | | |
| | | 83 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 84 | | public static ColliderHierarchyKey FromPacked(ulong packed) |
| | | 85 | | { |
| | 11 | 86 | | if (packed == 0UL) |
| | 1 | 87 | | return None; |
| | | 88 | | |
| | 10 | 89 | | return new ColliderHierarchyKey((byte)(packed >> 32), (int)(uint)packed); |
| | | 90 | | } |
| | | 91 | | |
| | | 92 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 8634776 | 93 | | public bool Equals(ColliderHierarchyKey other) => _dimension == other._dimension && _id == other._id; |
| | | 94 | | |
| | | 95 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 16 | 96 | | public override bool Equals(object? obj) => obj is ColliderHierarchyKey other && Equals(other); |
| | | 97 | | |
| | | 98 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 2 | 99 | | public override int GetHashCode() => Packed.GetHashCode(); |
| | | 100 | | |
| | | 101 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 8634683 | 102 | | public static bool operator ==(ColliderHierarchyKey left, ColliderHierarchyKey right) => left.Equals(right); |
| | | 103 | | |
| | | 104 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 79 | 105 | | public static bool operator !=(ColliderHierarchyKey left, ColliderHierarchyKey right) => !left.Equals(right); |
| | | 106 | | } |