< Summary

Information
Class: Gravitas.Support.PhysicsLayer
Assembly: Gravitas
File(s): /home/runner/work/Gravitas/Gravitas/src/Gravitas/CollisionHandling/Filtering/PhysicsLayer.cs
Line coverage
100%
Covered lines: 31
Uncovered lines: 0
Coverable lines: 31
Total lines: 138
Line coverage: 100%
Branch coverage
100%
Covered branches: 12
Total branches: 12
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%22100%
get_Index()100%11100%
get_MaskBit()100%11100%
Set(...)100%11100%
Equals(...)100%11100%
Equals(...)100%22100%
GetHashCode()100%11100%
ToString()100%11100%
op_Equality(...)100%11100%
op_Inequality(...)100%11100%
.cctor()100%11100%
LayerToName(...)100%22100%
NameToLayer(...)100%44100%
ValidateIndex(...)100%22100%

File(s)

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

#LineLine coverage
 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
 8using System;
 9using System.Runtime.CompilerServices;
 10using System.Text.Json.Serialization;
 11using MemoryPack;
 12using SwiftCollections;
 13
 14namespace Gravitas.Support;
 15
 16/// <summary>
 17/// Identifies one physics layer by index.
 18/// </summary>
 19[Serializable]
 20[MemoryPackable]
 21public 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    {
 1872642        ValidateIndex(index, nameof(index));
 1872543        _index = index;
 1872544        if (layerName != null)
 545            LayerNamesCache[index] = layerName;
 1872546    }
 47
 48    /// <summary>
 49    /// Gets the layer index.
 50    /// </summary>
 51    [JsonIgnore]
 52    [MemoryPackIgnore]
 53    public readonly int Index
 54    {
 55        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 639435756        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)]
 666625067        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    {
 275        ValidateIndex(index, nameof(index));
 176        _index = index;
 177    }
 78
 79    /// <inheritdoc />
 336880    public readonly bool Equals(PhysicsLayer other) => _index == other._index;
 81
 82    /// <inheritdoc />
 1483    public override readonly bool Equals(object? obj) => obj is PhysicsLayer other && Equals(other);
 84
 85    /// <inheritdoc />
 186    public override readonly int GetHashCode() => _index;
 87
 88    /// <inheritdoc />
 189    public override readonly string ToString() => _index.ToString();
 90
 91    /// <summary>
 92    /// Determines whether two physics layers have the same index.
 93    /// </summary>
 318794    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>
 199    public static bool operator !=(PhysicsLayer left, PhysicsLayer right) => !left.Equals(right);
 100
 101    /// <summary>
 102    /// Maps registered layer indices to their names.
 103    /// </summary>
 1104    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    {
 351810111        if (LayerNamesCache.TryGetValue(layer, out string name))
 5076112            return name;
 346734113        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    {
 7121        foreach (var kvp in LayerNamesCache)
 122        {
 2123            if (kvp.Value == layerName)
 1124                return kvp.Key;
 125        }
 126
 1127        return -1;
 1128    }
 129
 130    private static void ValidateIndex(int index, string paramName)
 131    {
 18728132        ChronicleGuard.ThrowIfArgumentOutOfRange(
 18728133            index < MinIndex || index > MaxIndex,
 18728134            index,
 18728135            paramName,
 18728136            "Physics layer index must be between 0 and 31 inclusive.");
 18726137    }
 138}