< Summary

Information
Class: Gravitas.Support.PhysicsLayerMask
Assembly: Gravitas
File(s): /home/runner/work/Gravitas/Gravitas/src/Gravitas/CollisionHandling/Filtering/PhysicsLayerMask.cs
Line coverage
100%
Covered lines: 23
Uncovered lines: 0
Coverable lines: 23
Total lines: 128
Line coverage: 100%
Branch coverage
100%
Covered branches: 6
Total branches: 6
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
get_Bits()100%11100%
get_None()100%11100%
get_All()100%11100%
Includes(...)100%11100%
Includes(...)100%11100%
Equals(...)100%11100%
Equals(...)100%22100%
GetHashCode()100%11100%
ToString()100%11100%
FromLayer(...)100%11100%
FromLayer(...)100%11100%
FromLayers(...)100%22100%
Excluding(...)100%22100%
op_Equality(...)100%11100%
op_Inequality(...)100%11100%

File(s)

/home/runner/work/Gravitas/Gravitas/src/Gravitas/CollisionHandling/Filtering/PhysicsLayerMask.cs

#LineLine coverage
 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
 8using System;
 9using System.Runtime.CompilerServices;
 10using System.Text.Json.Serialization;
 11using MemoryPack;
 12
 13namespace Gravitas.Support;
 14
 15/// <summary>
 16/// Represents an include mask for physics layer queries and filters.
 17/// </summary>
 18[Serializable]
 19[MemoryPackable]
 20public 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    {
 5314831        _bits = bits;
 5314832    }
 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)]
 5016242        get => _bits;
 43    }
 44
 45    /// <summary>
 46    /// Gets a mask that includes no physics layers.
 47    /// </summary>
 48    [JsonIgnore]
 49    [MemoryPackIgnore]
 2535150    public static PhysicsLayerMask None => new(0);
 51
 52    /// <summary>
 53    /// Gets a mask that includes every physics layer.
 54    /// </summary>
 55    [JsonIgnore]
 56    [MemoryPackIgnore]
 2602657    public static PhysicsLayerMask All => new(-1);
 58
 59    /// <summary>
 60    /// Determines whether this mask includes the specified layer.
 61    /// </summary>
 62    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 666451763    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)]
 169    public readonly bool Includes(int layerIndex) => Includes(new PhysicsLayer(layerIndex));
 70
 71    /// <inheritdoc />
 31672    public readonly bool Equals(PhysicsLayerMask other) => _bits == other._bits;
 73
 74    /// <inheritdoc />
 2775    public override readonly bool Equals(object? obj) => obj is PhysicsLayerMask other && Equals(other);
 76
 77    /// <inheritdoc />
 178    public override readonly int GetHashCode() => _bits;
 79
 80    /// <inheritdoc />
 181    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)]
 164387    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)]
 172093    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    {
 5100        int bits = 0;
 30101        for (int i = 0; i < layers.Length; i++)
 10102            bits |= layers[i].MaskBit;
 103
 5104        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    {
 1112        int bits = -1;
 6113        for (int i = 0; i < excludedLayers.Length; i++)
 2114            bits &= ~excludedLayers[i].MaskBit;
 115
 1116        return new PhysicsLayerMask(bits);
 117    }
 118
 119    /// <summary>
 120    /// Determines whether two layer masks contain the same bits.
 121    /// </summary>
 122122    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>
 1127    public static bool operator !=(PhysicsLayerMask left, PhysicsLayerMask right) => !left.Equals(right);
 128}