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