< Summary

Information
Class: Gravitas.Colliders.ColliderHierarchyState
Assembly: Gravitas
File(s): /home/runner/work/Gravitas/Gravitas/src/Gravitas/Colliders/Hierarchy/ColliderHierarchyState.cs
Line coverage
100%
Covered lines: 84
Uncovered lines: 0
Coverable lines: 84
Total lines: 167
Line coverage: 100%
Branch coverage
100%
Covered branches: 40
Total branches: 40
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
get_TopParentKey()100%22100%
get_ChildCount()100%22100%
get_Children()100%11100%
Initialize(...)100%22100%
SetParent(...)100%44100%
ClearParent(...)100%22100%
ClearParentReference()100%11100%
ClearChildren()100%22100%
AddChild(...)100%44100%
RemoveChild(...)100%88100%
ExcludesCollisionWith(...)100%1212100%
FindTopParent(...)100%22100%

File(s)

/home/runner/work/Gravitas/Gravitas/src/Gravitas/Colliders/Hierarchy/ColliderHierarchyState.cs

#LineLine coverage
 1//=======================================================================
 2// ColliderHierarchyState.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.Runtime.CompilerServices;
 9using SwiftCollections;
 10
 11namespace Gravitas.Colliders;
 12
 13/// <summary>
 14/// Stores parent and child relationships used to filter collisions within a collider hierarchy.
 15/// </summary>
 16internal struct ColliderHierarchyState
 17{
 18    private bool _configuredAsParent;
 19    private SwiftHashSet<ulong>? _children;
 20
 21    public bool IsChild { get; private set; }
 22
 23    public bool IsParent { get; private set; }
 24
 25    public ColliderHierarchyKey ParentKey { get; private set; }
 26
 1677427    public ColliderHierarchyKey TopParentKey => TopParent?.HierarchyKey ?? ColliderHierarchyKey.None;
 28
 29    public IColliderHierarchyNode? Parent { get; private set; }
 30
 31    public IColliderHierarchyNode? TopParent { get; private set; }
 32
 1679433    public int ChildCount => _children?.Count ?? 0;
 34
 95535    public SwiftHashSet<ulong>? Children => _children;
 36
 37    public void Initialize(bool isParent)
 38    {
 2271739        _configuredAsParent = isParent;
 2271740        IsParent = isParent;
 2271741        IsChild = !isParent;
 2271742        ParentKey = ColliderHierarchyKey.None;
 2271743        Parent = null;
 2271744        TopParent = null;
 2271745        _children?.Clear();
 246    }
 47
 48    public bool SetParent(IColliderHierarchyNode owner, IColliderHierarchyNode parent)
 49    {
 8750        ChronicleGuard.ThrowIfNull(parent, nameof(parent));
 8651        ChronicleGuard.ThrowIfArgument(
 8652            ReferenceEquals(owner, parent),
 8653            nameof(parent),
 8654            "Collider cannot be parented to itself.");
 8455        ChronicleGuard.ThrowIfArgument(
 8456            !owner.HierarchyKey.IsValid,
 8457            nameof(owner),
 8458            "Owner collider must have a valid hierarchy key before setting a parent.");
 8359        ChronicleGuard.ThrowIfArgument(
 8360            !parent.HierarchyKey.IsValid,
 8361            nameof(parent),
 8362            "Parent collider must have a valid hierarchy key before being assigned.");
 63
 8264        GravitasWorldContext ownerContext = owner.Context;
 8265        ChronicleGuard.ThrowIfArgument(
 8266            !ReferenceEquals(ownerContext, parent.Context),
 8267            nameof(parent),
 8268            "Parent collider must belong to the same GravitasWorldContext.");
 69
 8070        IColliderHierarchyNode topParent = FindTopParent(owner, parent);
 7871        ColliderHierarchyKey topParentKey = topParent.HierarchyKey;
 7872        bool topParentChanged = ParentKey != topParentKey;
 7873        if (ParentKey.IsValid && topParentChanged)
 274            TopParent!.RemoveChild(owner.HierarchyKey);
 75
 7876        Parent = parent;
 7877        TopParent = topParent;
 7878        ParentKey = topParentKey;
 7879        IsChild = true;
 7880        if (topParentChanged)
 7281            topParent.AddChild(owner.HierarchyKey);
 7882        return topParentChanged;
 83    }
 84
 85    public bool ClearParent(IColliderHierarchyNode owner)
 86    {
 96487        bool changed = ParentKey.IsValid;
 96488        if (ParentKey.IsValid)
 989            TopParent!.RemoveChild(owner.HierarchyKey);
 90
 96491        Parent = null;
 96492        TopParent = null;
 96493        ParentKey = ColliderHierarchyKey.None;
 96494        IsChild = !_configuredAsParent;
 96495        return changed;
 96    }
 97
 98    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 99    public bool ClearParentReference()
 100    {
 11101        bool changed = ParentKey.IsValid;
 11102        Parent = null;
 11103        TopParent = null;
 11104        ParentKey = ColliderHierarchyKey.None;
 11105        IsChild = !_configuredAsParent;
 11106        return changed;
 107    }
 108
 109    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 110    public void ClearChildren()
 111    {
 8112        _children?.Clear();
 8113        IsParent = _configuredAsParent;
 8114    }
 115
 116    public bool AddChild(ColliderHierarchyKey key)
 117    {
 77118        ChronicleGuard.ThrowIfArgument(!key.IsValid, nameof(key), "Child collider key must be valid.");
 76119        _children ??= new();
 76120        if (!_children.Add(key.Packed))
 1121            return false;
 122
 75123        IsParent = true;
 75124        return true;
 125    }
 126
 127    public bool RemoveChild(ColliderHierarchyKey key)
 128    {
 16129        if (!key.IsValid || _children?.Remove(key.Packed) != true)
 3130            return false;
 131
 13132        if (_children.Count == 0)
 10133            IsParent = _configuredAsParent;
 134
 13135        return true;
 136    }
 137
 138    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 139    public bool ExcludesCollisionWith(in ColliderHierarchyState other, ColliderHierarchyKey ownerKey, ColliderHierarchyK
 140    {
 2883357141        if (!ownerKey.IsValid || !otherKey.IsValid)
 3142            return false;
 143
 2883354144        if (ownerKey == otherKey)
 7718145            return true;
 146
 2875636147        if (ParentKey == otherKey || other.ParentKey == ownerKey)
 106148            return true;
 149
 2875530150        return ParentKey.IsValid && ParentKey == other.ParentKey;
 151    }
 152
 153    private static IColliderHierarchyNode FindTopParent(IColliderHierarchyNode owner, IColliderHierarchyNode parent)
 154    {
 80155        IColliderHierarchyNode current = parent;
 92156        while (current.HierarchyParent != null)
 157        {
 14158            current = current.HierarchyParent;
 14159            ChronicleGuard.ThrowIfArgument(
 14160                ReferenceEquals(owner, current),
 14161                nameof(parent),
 14162                "Collider hierarchy cannot contain cycles.");
 163        }
 164
 78165        return current;
 166    }
 167}