| | | 1 | | //======================================================================= |
| | | 2 | | // PhysicsLayer.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 | | using System.Text.Json.Serialization; |
| | | 11 | | using MemoryPack; |
| | | 12 | | using SwiftCollections; |
| | | 13 | | |
| | | 14 | | namespace Gravitas.Support; |
| | | 15 | | |
| | | 16 | | /// <summary> |
| | | 17 | | /// Identifies one physics layer by index. |
| | | 18 | | /// </summary> |
| | | 19 | | [Serializable] |
| | | 20 | | [MemoryPackable] |
| | | 21 | | public partial struct PhysicsLayer : IEquatable<PhysicsLayer> |
| | | 22 | | { |
| | | 23 | | /// <summary> |
| | | 24 | | /// Minimum valid physics layer index. |
| | | 25 | | /// </summary> |
| | | 26 | | public const int MinIndex = 0; |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// Maximum valid physics layer index. |
| | | 30 | | /// </summary> |
| | | 31 | | public const int MaxIndex = 31; |
| | | 32 | | |
| | | 33 | | [JsonInclude] |
| | | 34 | | [MemoryPackInclude] |
| | | 35 | | private int _index; |
| | | 36 | | |
| | | 37 | | /// <summary> |
| | | 38 | | /// Creates a physics layer and optionally registers its display name. |
| | | 39 | | /// </summary> |
| | | 40 | | public PhysicsLayer(int index, string? layerName = null) |
| | | 41 | | { |
| | 18726 | 42 | | ValidateIndex(index, nameof(index)); |
| | 18725 | 43 | | _index = index; |
| | 18725 | 44 | | if (layerName != null) |
| | 5 | 45 | | LayerNamesCache[index] = layerName; |
| | 18725 | 46 | | } |
| | | 47 | | |
| | | 48 | | /// <summary> |
| | | 49 | | /// Gets the layer index. |
| | | 50 | | /// </summary> |
| | | 51 | | [JsonIgnore] |
| | | 52 | | [MemoryPackIgnore] |
| | | 53 | | public readonly int Index |
| | | 54 | | { |
| | | 55 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 6394357 | 56 | | get => _index; |
| | | 57 | | } |
| | | 58 | | |
| | | 59 | | /// <summary> |
| | | 60 | | /// Gets the single bit corresponding to this layer index. |
| | | 61 | | /// </summary> |
| | | 62 | | [JsonIgnore] |
| | | 63 | | [MemoryPackIgnore] |
| | | 64 | | public readonly int MaskBit |
| | | 65 | | { |
| | | 66 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 6666250 | 67 | | get => 1 << _index; |
| | | 68 | | } |
| | | 69 | | |
| | | 70 | | /// <summary> |
| | | 71 | | /// Replaces the layer index after validating its range. |
| | | 72 | | /// </summary> |
| | | 73 | | public void Set(int index) |
| | | 74 | | { |
| | 2 | 75 | | ValidateIndex(index, nameof(index)); |
| | 1 | 76 | | _index = index; |
| | 1 | 77 | | } |
| | | 78 | | |
| | | 79 | | /// <inheritdoc /> |
| | 3368 | 80 | | public readonly bool Equals(PhysicsLayer other) => _index == other._index; |
| | | 81 | | |
| | | 82 | | /// <inheritdoc /> |
| | 14 | 83 | | public override readonly bool Equals(object? obj) => obj is PhysicsLayer other && Equals(other); |
| | | 84 | | |
| | | 85 | | /// <inheritdoc /> |
| | 1 | 86 | | public override readonly int GetHashCode() => _index; |
| | | 87 | | |
| | | 88 | | /// <inheritdoc /> |
| | 1 | 89 | | public override readonly string ToString() => _index.ToString(); |
| | | 90 | | |
| | | 91 | | /// <summary> |
| | | 92 | | /// Determines whether two physics layers have the same index. |
| | | 93 | | /// </summary> |
| | 3187 | 94 | | public static bool operator ==(PhysicsLayer left, PhysicsLayer right) => left.Equals(right); |
| | | 95 | | |
| | | 96 | | /// <summary> |
| | | 97 | | /// Determines whether two physics layers have different indices. |
| | | 98 | | /// </summary> |
| | 1 | 99 | | public static bool operator !=(PhysicsLayer left, PhysicsLayer right) => !left.Equals(right); |
| | | 100 | | |
| | | 101 | | /// <summary> |
| | | 102 | | /// Maps registered layer indices to their names. |
| | | 103 | | /// </summary> |
| | 1 | 104 | | public static SwiftDictionary<int, string> LayerNamesCache = new(); |
| | | 105 | | |
| | | 106 | | /// <summary> |
| | | 107 | | /// Given a layer number, returns the registered layer name. |
| | | 108 | | /// </summary> |
| | | 109 | | public static string? LayerToName(int layer) |
| | | 110 | | { |
| | 351810 | 111 | | if (LayerNamesCache.TryGetValue(layer, out string name)) |
| | 5076 | 112 | | return name; |
| | 346734 | 113 | | return null; |
| | | 114 | | } |
| | | 115 | | |
| | | 116 | | /// <summary> |
| | | 117 | | /// Given a layer name, returns the registered layer index or -1 if the layer name is invalid. |
| | | 118 | | /// </summary> |
| | | 119 | | public static int NameToLayer(string layerName) |
| | | 120 | | { |
| | 7 | 121 | | foreach (var kvp in LayerNamesCache) |
| | | 122 | | { |
| | 2 | 123 | | if (kvp.Value == layerName) |
| | 1 | 124 | | return kvp.Key; |
| | | 125 | | } |
| | | 126 | | |
| | 1 | 127 | | return -1; |
| | 1 | 128 | | } |
| | | 129 | | |
| | | 130 | | private static void ValidateIndex(int index, string paramName) |
| | | 131 | | { |
| | 18728 | 132 | | ChronicleGuard.ThrowIfArgumentOutOfRange( |
| | 18728 | 133 | | index < MinIndex || index > MaxIndex, |
| | 18728 | 134 | | index, |
| | 18728 | 135 | | paramName, |
| | 18728 | 136 | | "Physics layer index must be between 0 and 31 inclusive."); |
| | 18726 | 137 | | } |
| | | 138 | | } |