| | | 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 | | |
| | | 8 | | using System.Runtime.CompilerServices; |
| | | 9 | | using SwiftCollections; |
| | | 10 | | |
| | | 11 | | namespace Gravitas.Colliders; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Stores parent and child relationships used to filter collisions within a collider hierarchy. |
| | | 15 | | /// </summary> |
| | | 16 | | internal 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 | | |
| | 16774 | 27 | | public ColliderHierarchyKey TopParentKey => TopParent?.HierarchyKey ?? ColliderHierarchyKey.None; |
| | | 28 | | |
| | | 29 | | public IColliderHierarchyNode? Parent { get; private set; } |
| | | 30 | | |
| | | 31 | | public IColliderHierarchyNode? TopParent { get; private set; } |
| | | 32 | | |
| | 16794 | 33 | | public int ChildCount => _children?.Count ?? 0; |
| | | 34 | | |
| | 955 | 35 | | public SwiftHashSet<ulong>? Children => _children; |
| | | 36 | | |
| | | 37 | | public void Initialize(bool isParent) |
| | | 38 | | { |
| | 22717 | 39 | | _configuredAsParent = isParent; |
| | 22717 | 40 | | IsParent = isParent; |
| | 22717 | 41 | | IsChild = !isParent; |
| | 22717 | 42 | | ParentKey = ColliderHierarchyKey.None; |
| | 22717 | 43 | | Parent = null; |
| | 22717 | 44 | | TopParent = null; |
| | 22717 | 45 | | _children?.Clear(); |
| | 2 | 46 | | } |
| | | 47 | | |
| | | 48 | | public bool SetParent(IColliderHierarchyNode owner, IColliderHierarchyNode parent) |
| | | 49 | | { |
| | 87 | 50 | | ChronicleGuard.ThrowIfNull(parent, nameof(parent)); |
| | 86 | 51 | | ChronicleGuard.ThrowIfArgument( |
| | 86 | 52 | | ReferenceEquals(owner, parent), |
| | 86 | 53 | | nameof(parent), |
| | 86 | 54 | | "Collider cannot be parented to itself."); |
| | 84 | 55 | | ChronicleGuard.ThrowIfArgument( |
| | 84 | 56 | | !owner.HierarchyKey.IsValid, |
| | 84 | 57 | | nameof(owner), |
| | 84 | 58 | | "Owner collider must have a valid hierarchy key before setting a parent."); |
| | 83 | 59 | | ChronicleGuard.ThrowIfArgument( |
| | 83 | 60 | | !parent.HierarchyKey.IsValid, |
| | 83 | 61 | | nameof(parent), |
| | 83 | 62 | | "Parent collider must have a valid hierarchy key before being assigned."); |
| | | 63 | | |
| | 82 | 64 | | GravitasWorldContext ownerContext = owner.Context; |
| | 82 | 65 | | ChronicleGuard.ThrowIfArgument( |
| | 82 | 66 | | !ReferenceEquals(ownerContext, parent.Context), |
| | 82 | 67 | | nameof(parent), |
| | 82 | 68 | | "Parent collider must belong to the same GravitasWorldContext."); |
| | | 69 | | |
| | 80 | 70 | | IColliderHierarchyNode topParent = FindTopParent(owner, parent); |
| | 78 | 71 | | ColliderHierarchyKey topParentKey = topParent.HierarchyKey; |
| | 78 | 72 | | bool topParentChanged = ParentKey != topParentKey; |
| | 78 | 73 | | if (ParentKey.IsValid && topParentChanged) |
| | 2 | 74 | | TopParent!.RemoveChild(owner.HierarchyKey); |
| | | 75 | | |
| | 78 | 76 | | Parent = parent; |
| | 78 | 77 | | TopParent = topParent; |
| | 78 | 78 | | ParentKey = topParentKey; |
| | 78 | 79 | | IsChild = true; |
| | 78 | 80 | | if (topParentChanged) |
| | 72 | 81 | | topParent.AddChild(owner.HierarchyKey); |
| | 78 | 82 | | return topParentChanged; |
| | | 83 | | } |
| | | 84 | | |
| | | 85 | | public bool ClearParent(IColliderHierarchyNode owner) |
| | | 86 | | { |
| | 964 | 87 | | bool changed = ParentKey.IsValid; |
| | 964 | 88 | | if (ParentKey.IsValid) |
| | 9 | 89 | | TopParent!.RemoveChild(owner.HierarchyKey); |
| | | 90 | | |
| | 964 | 91 | | Parent = null; |
| | 964 | 92 | | TopParent = null; |
| | 964 | 93 | | ParentKey = ColliderHierarchyKey.None; |
| | 964 | 94 | | IsChild = !_configuredAsParent; |
| | 964 | 95 | | return changed; |
| | | 96 | | } |
| | | 97 | | |
| | | 98 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 99 | | public bool ClearParentReference() |
| | | 100 | | { |
| | 11 | 101 | | bool changed = ParentKey.IsValid; |
| | 11 | 102 | | Parent = null; |
| | 11 | 103 | | TopParent = null; |
| | 11 | 104 | | ParentKey = ColliderHierarchyKey.None; |
| | 11 | 105 | | IsChild = !_configuredAsParent; |
| | 11 | 106 | | return changed; |
| | | 107 | | } |
| | | 108 | | |
| | | 109 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 110 | | public void ClearChildren() |
| | | 111 | | { |
| | 8 | 112 | | _children?.Clear(); |
| | 8 | 113 | | IsParent = _configuredAsParent; |
| | 8 | 114 | | } |
| | | 115 | | |
| | | 116 | | public bool AddChild(ColliderHierarchyKey key) |
| | | 117 | | { |
| | 77 | 118 | | ChronicleGuard.ThrowIfArgument(!key.IsValid, nameof(key), "Child collider key must be valid."); |
| | 76 | 119 | | _children ??= new(); |
| | 76 | 120 | | if (!_children.Add(key.Packed)) |
| | 1 | 121 | | return false; |
| | | 122 | | |
| | 75 | 123 | | IsParent = true; |
| | 75 | 124 | | return true; |
| | | 125 | | } |
| | | 126 | | |
| | | 127 | | public bool RemoveChild(ColliderHierarchyKey key) |
| | | 128 | | { |
| | 16 | 129 | | if (!key.IsValid || _children?.Remove(key.Packed) != true) |
| | 3 | 130 | | return false; |
| | | 131 | | |
| | 13 | 132 | | if (_children.Count == 0) |
| | 10 | 133 | | IsParent = _configuredAsParent; |
| | | 134 | | |
| | 13 | 135 | | return true; |
| | | 136 | | } |
| | | 137 | | |
| | | 138 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 139 | | public bool ExcludesCollisionWith(in ColliderHierarchyState other, ColliderHierarchyKey ownerKey, ColliderHierarchyK |
| | | 140 | | { |
| | 2883357 | 141 | | if (!ownerKey.IsValid || !otherKey.IsValid) |
| | 3 | 142 | | return false; |
| | | 143 | | |
| | 2883354 | 144 | | if (ownerKey == otherKey) |
| | 7718 | 145 | | return true; |
| | | 146 | | |
| | 2875636 | 147 | | if (ParentKey == otherKey || other.ParentKey == ownerKey) |
| | 106 | 148 | | return true; |
| | | 149 | | |
| | 2875530 | 150 | | return ParentKey.IsValid && ParentKey == other.ParentKey; |
| | | 151 | | } |
| | | 152 | | |
| | | 153 | | private static IColliderHierarchyNode FindTopParent(IColliderHierarchyNode owner, IColliderHierarchyNode parent) |
| | | 154 | | { |
| | 80 | 155 | | IColliderHierarchyNode current = parent; |
| | 92 | 156 | | while (current.HierarchyParent != null) |
| | | 157 | | { |
| | 14 | 158 | | current = current.HierarchyParent; |
| | 14 | 159 | | ChronicleGuard.ThrowIfArgument( |
| | 14 | 160 | | ReferenceEquals(owner, current), |
| | 14 | 161 | | nameof(parent), |
| | 14 | 162 | | "Collider hierarchy cannot contain cycles."); |
| | | 163 | | } |
| | | 164 | | |
| | 78 | 165 | | return current; |
| | | 166 | | } |
| | | 167 | | } |