diff --git a/Scripts/BoundsOctreeNode.cs b/Scripts/BoundsOctreeNode.cs index 1ffd68d..bb94408 100644 --- a/Scripts/BoundsOctreeNode.cs +++ b/Scripts/BoundsOctreeNode.cs @@ -28,9 +28,37 @@ public class BoundsOctreeNode { const int numObjectsAllowed = 8; // An object in the octree - class OctreeObject { + struct OctreeObject { public T Obj; public Bounds Bounds; + + public override int GetHashCode() + { + var hashCode = Bounds.GetHashCode(); + + if (Obj != null) + hashCode ^= Obj.GetHashCode(); + + return hashCode; + } + + public override bool Equals(object obj) + { + if (obj == null) + return false; + + if (!(obj is OctreeObject)) + return false; + + var other = (OctreeObject)obj; + if (Bounds != other.Bounds) + return false; + + if (Obj == null && other.Obj == null) + return true; + + return Obj.Equals(other.Obj); + } } /// @@ -69,8 +97,10 @@ public bool Remove(T obj) { bool removed = false; for (int i = 0; i < objects.Count; i++) { - if (objects[i].Obj.Equals(obj)) { - removed = objects.Remove(objects[i]); + if (objects[i].Obj.Equals(obj)) + { + removed = true; + objects.RemoveAt(i); break; } } @@ -363,7 +393,7 @@ public int GetTotalObjects(int startingNum = 0) { // #### PRIVATE METHODS #### /// - /// Set values for this node. + /// Set values for this node. /// /// Length of this node, not taking looseness into account. /// Minimum size of nodes in this octree. @@ -427,7 +457,7 @@ void SubAdd(T obj, Bounds objBounds) { bestFitChild = BestFitChild(existingObj.Bounds); // Does it fit? if (Encapsulates(children[bestFitChild].bounds, existingObj.Bounds)) { - children[bestFitChild].SubAdd(existingObj.Obj, existingObj.Bounds); // Go a level deeper + children[bestFitChild].SubAdd(existingObj.Obj, existingObj.Bounds); // Go a level deeper objects.Remove(existingObj); // Remove from here } } @@ -524,7 +554,7 @@ bool ShouldMerge() { /// Checks if this node or anything below it has something in it. /// /// True if this node or any of its children, grandchildren etc have something in them - public bool HasAnyObjects() { + internal bool HasAnyObjects() { if (objects.Count > 0) return true; if (children != null) { @@ -535,4 +565,4 @@ public bool HasAnyObjects() { return false; } -} \ No newline at end of file +}