< 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: 82
Uncovered lines: 0
Coverable lines: 82
Total lines: 159
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 SwiftCollections;
 9using System.Runtime.CompilerServices;
 10
 11namespace Gravitas.Colliders;
 12
 13internal struct ColliderHierarchyState
 14{
 15    private bool _configuredAsParent;
 16    private SwiftHashSet<ulong>? _children;
 17
 18    public bool IsChild { get; private set; }
 19
 20    public bool IsParent { get; private set; }
 21
 22    public ColliderHierarchyKey ParentKey { get; private set; }
 23
 303124    public ColliderHierarchyKey TopParentKey => TopParent?.HierarchyKey ?? ColliderHierarchyKey.None;
 25
 26    public IColliderHierarchyNode? Parent { get; private set; }
 27
 28    public IColliderHierarchyNode? TopParent { get; private set; }
 29
 305130    public int ChildCount => _children?.Count ?? 0;
 31
 48332    public SwiftHashSet<ulong>? Children => _children;
 33
 34    public void Initialize(bool isParent)
 35    {
 1085536        _configuredAsParent = isParent;
 1085537        IsParent = isParent;
 1085538        IsChild = !isParent;
 1085539        ParentKey = ColliderHierarchyKey.None;
 1085540        Parent = null;
 1085541        TopParent = null;
 1085542        _children?.Clear();
 243    }
 44
 45    public void SetParent(IColliderHierarchyNode owner, IColliderHierarchyNode parent)
 46    {
 7247        SwiftThrowHelper.ThrowIfNull(parent, nameof(parent));
 7148        SwiftThrowHelper.ThrowIfArgument(
 7149            ReferenceEquals(owner, parent),
 7150            nameof(parent),
 7151            "Collider cannot be parented to itself.");
 6952        SwiftThrowHelper.ThrowIfArgument(
 6953            !owner.HierarchyKey.IsValid,
 6954            nameof(owner),
 6955            "Owner collider must have a valid hierarchy key before setting a parent.");
 6856        SwiftThrowHelper.ThrowIfArgument(
 6857            !parent.HierarchyKey.IsValid,
 6858            nameof(parent),
 6859            "Parent collider must have a valid hierarchy key before being assigned.");
 60
 6761        GravitasWorldContext ownerContext = owner.Context;
 6762        SwiftThrowHelper.ThrowIfArgument(
 6763            !ReferenceEquals(ownerContext, parent.Context),
 6764            nameof(parent),
 6765            "Parent collider must belong to the same GravitasWorldContext.");
 66
 6567        IColliderHierarchyNode topParent = FindTopParent(owner, parent);
 6368        ColliderHierarchyKey topParentKey = topParent.HierarchyKey;
 6369        bool topParentChanged = ParentKey != topParentKey;
 6370        if (ParentKey.IsValid && topParentChanged)
 271            TopParent!.RemoveChild(owner.HierarchyKey);
 72
 6373        Parent = parent;
 6374        TopParent = topParent;
 6375        ParentKey = topParentKey;
 6376        IsChild = true;
 6377        if (topParentChanged)
 6178            topParent.AddChild(owner.HierarchyKey);
 6379    }
 80
 81    public void ClearParent(IColliderHierarchyNode owner)
 82    {
 49283        if (ParentKey.IsValid)
 984            TopParent!.RemoveChild(owner.HierarchyKey);
 85
 49286        Parent = null;
 49287        TopParent = null;
 49288        ParentKey = ColliderHierarchyKey.None;
 49289        IsChild = !_configuredAsParent;
 49290    }
 91
 92    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 93    public void ClearParentReference()
 94    {
 495        Parent = null;
 496        TopParent = null;
 497        ParentKey = ColliderHierarchyKey.None;
 498        IsChild = !_configuredAsParent;
 499    }
 100
 101    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 102    public void ClearChildren()
 103    {
 5104        _children?.Clear();
 5105        IsParent = _configuredAsParent;
 5106    }
 107
 108    public bool AddChild(ColliderHierarchyKey key)
 109    {
 66110        SwiftThrowHelper.ThrowIfArgument(!key.IsValid, nameof(key), "Child collider key must be valid.");
 65111        _children ??= new();
 65112        if (!_children.Add(key.Packed))
 1113            return false;
 114
 64115        IsParent = true;
 64116        return true;
 117    }
 118
 119    public bool RemoveChild(ColliderHierarchyKey key)
 120    {
 16121        if (!key.IsValid || _children?.Remove(key.Packed) != true)
 3122            return false;
 123
 13124        if (_children.Count == 0)
 10125            IsParent = _configuredAsParent;
 126
 13127        return true;
 128    }
 129
 130    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 131    public bool ExcludesCollisionWith(in ColliderHierarchyState other, ColliderHierarchyKey ownerKey, ColliderHierarchyK
 132    {
 117415133        if (!ownerKey.IsValid || !otherKey.IsValid)
 2134            return false;
 135
 117413136        if (ownerKey == otherKey)
 6637137            return true;
 138
 110776139        if (ParentKey == otherKey || other.ParentKey == ownerKey)
 80140            return true;
 141
 110696142        return ParentKey.IsValid && ParentKey == other.ParentKey;
 143    }
 144
 145    private static IColliderHierarchyNode FindTopParent(IColliderHierarchyNode owner, IColliderHierarchyNode parent)
 146    {
 65147        IColliderHierarchyNode current = parent;
 74148        while (current.HierarchyParent != null)
 149        {
 11150            current = current.HierarchyParent;
 11151            SwiftThrowHelper.ThrowIfArgument(
 11152                ReferenceEquals(owner, current),
 11153                nameof(parent),
 11154                "Collider hierarchy cannot contain cycles.");
 155        }
 156
 63157        return current;
 158    }
 159}