| | | 1 | | //======================================================================= |
| | | 2 | | // PhysicsLayerMask.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 | | |
| | | 13 | | namespace Gravitas.Support; |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Represents an include mask for physics layer queries and filters. |
| | | 17 | | /// </summary> |
| | | 18 | | [Serializable] |
| | | 19 | | [MemoryPackable] |
| | | 20 | | public partial struct PhysicsLayerMask : IEquatable<PhysicsLayerMask> |
| | | 21 | | { |
| | | 22 | | [JsonInclude] |
| | | 23 | | [MemoryPackInclude] |
| | | 24 | | private int _bits; |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Creates an include mask from its raw bit field. |
| | | 28 | | /// </summary> |
| | | 29 | | public PhysicsLayerMask(int bits) |
| | | 30 | | { |
| | 53148 | 31 | | _bits = bits; |
| | 53148 | 32 | | } |
| | | 33 | | |
| | | 34 | | /// <summary> |
| | | 35 | | /// Gets the raw include-mask bits. |
| | | 36 | | /// </summary> |
| | | 37 | | [JsonIgnore] |
| | | 38 | | [MemoryPackIgnore] |
| | | 39 | | public readonly int Bits |
| | | 40 | | { |
| | | 41 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 50162 | 42 | | get => _bits; |
| | | 43 | | } |
| | | 44 | | |
| | | 45 | | /// <summary> |
| | | 46 | | /// Gets a mask that includes no physics layers. |
| | | 47 | | /// </summary> |
| | | 48 | | [JsonIgnore] |
| | | 49 | | [MemoryPackIgnore] |
| | 25351 | 50 | | public static PhysicsLayerMask None => new(0); |
| | | 51 | | |
| | | 52 | | /// <summary> |
| | | 53 | | /// Gets a mask that includes every physics layer. |
| | | 54 | | /// </summary> |
| | | 55 | | [JsonIgnore] |
| | | 56 | | [MemoryPackIgnore] |
| | 26026 | 57 | | public static PhysicsLayerMask All => new(-1); |
| | | 58 | | |
| | | 59 | | /// <summary> |
| | | 60 | | /// Determines whether this mask includes the specified layer. |
| | | 61 | | /// </summary> |
| | | 62 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 6664517 | 63 | | public readonly bool Includes(PhysicsLayer layer) => (_bits & layer.MaskBit) != 0; |
| | | 64 | | |
| | | 65 | | /// <summary> |
| | | 66 | | /// Determines whether this mask includes the specified layer index. |
| | | 67 | | /// </summary> |
| | | 68 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1 | 69 | | public readonly bool Includes(int layerIndex) => Includes(new PhysicsLayer(layerIndex)); |
| | | 70 | | |
| | | 71 | | /// <inheritdoc /> |
| | 316 | 72 | | public readonly bool Equals(PhysicsLayerMask other) => _bits == other._bits; |
| | | 73 | | |
| | | 74 | | /// <inheritdoc /> |
| | 27 | 75 | | public override readonly bool Equals(object? obj) => obj is PhysicsLayerMask other && Equals(other); |
| | | 76 | | |
| | | 77 | | /// <inheritdoc /> |
| | 1 | 78 | | public override readonly int GetHashCode() => _bits; |
| | | 79 | | |
| | | 80 | | /// <inheritdoc /> |
| | 1 | 81 | | public override readonly string ToString() => _bits.ToString(); |
| | | 82 | | |
| | | 83 | | /// <summary> |
| | | 84 | | /// Creates an include mask containing one layer index. |
| | | 85 | | /// </summary> |
| | | 86 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1643 | 87 | | public static PhysicsLayerMask FromLayer(int layerIndex) => FromLayer(new PhysicsLayer(layerIndex)); |
| | | 88 | | |
| | | 89 | | /// <summary> |
| | | 90 | | /// Creates an include mask containing one layer. |
| | | 91 | | /// </summary> |
| | | 92 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1720 | 93 | | public static PhysicsLayerMask FromLayer(PhysicsLayer layer) => new(layer.MaskBit); |
| | | 94 | | |
| | | 95 | | /// <summary> |
| | | 96 | | /// Creates an include mask containing the specified layers. |
| | | 97 | | /// </summary> |
| | | 98 | | public static PhysicsLayerMask FromLayers(params PhysicsLayer[] layers) |
| | | 99 | | { |
| | 5 | 100 | | int bits = 0; |
| | 30 | 101 | | for (int i = 0; i < layers.Length; i++) |
| | 10 | 102 | | bits |= layers[i].MaskBit; |
| | | 103 | | |
| | 5 | 104 | | return new PhysicsLayerMask(bits); |
| | | 105 | | } |
| | | 106 | | |
| | | 107 | | /// <summary> |
| | | 108 | | /// Creates an include mask containing every layer except those specified. |
| | | 109 | | /// </summary> |
| | | 110 | | public static PhysicsLayerMask Excluding(params PhysicsLayer[] excludedLayers) |
| | | 111 | | { |
| | 1 | 112 | | int bits = -1; |
| | 6 | 113 | | for (int i = 0; i < excludedLayers.Length; i++) |
| | 2 | 114 | | bits &= ~excludedLayers[i].MaskBit; |
| | | 115 | | |
| | 1 | 116 | | return new PhysicsLayerMask(bits); |
| | | 117 | | } |
| | | 118 | | |
| | | 119 | | /// <summary> |
| | | 120 | | /// Determines whether two layer masks contain the same bits. |
| | | 121 | | /// </summary> |
| | 122 | 122 | | public static bool operator ==(PhysicsLayerMask left, PhysicsLayerMask right) => left.Equals(right); |
| | | 123 | | |
| | | 124 | | /// <summary> |
| | | 125 | | /// Determines whether two layer masks contain different bits. |
| | | 126 | | /// </summary> |
| | 1 | 127 | | public static bool operator !=(PhysicsLayerMask left, PhysicsLayerMask right) => !left.Equals(right); |
| | | 128 | | } |