/// <summary>
        ///     Inserts the data into the tree.
        /// </summary>
        /// <param name="data">Data being inserted.</param>
        public void Insert(QuadtreeData data)
        {
            var actorBounds = data.Target.Bounds;

            // Object doesn't fit into this node.
            if (!NodeBounds.Intersects(actorBounds))
            {
                return;
            }

            if (IsLeaf && Contents.Count >= MaxObjectsPerNode)
            {
                Split();
            }

            if (IsLeaf)
            {
                Contents.Add(data);
            }
            else
            {
                foreach (var child in Children)
                {
                    child.Insert(data);
                }
            }
        }
        /// <summary>
        ///     Removes data from the Quadtree
        /// </summary>
        /// <param name="data">The data to be removed.</param>
        public void Remove(QuadtreeData data)
        {
            if (IsLeaf)
            {
                var removeIndex = -1;

                for (int i = 0, size = Contents.Count; i < size; i++)
                {
                    if (Contents[i].Target == data.Target)
                    {
                        removeIndex = i;
                        break;
                    }
                }

                if (removeIndex != -1)
                {
                    Contents.RemoveAt(removeIndex);
                }
            }
            else
            {
                foreach (var quadTree in Children)
                {
                    quadTree.Remove(data);
                }
            }

            Shake();
        }
예제 #3
0
 /// <summary>
 /// Inserts the target into the collision tree.
 /// The target will have its OnCollision called when collisions occur.
 /// </summary>
 /// <param name="target">Target to insert.</param>
 public void Insert(ICollisionActor target)
 {
     if (!_targetDataDictionary.ContainsKey(target))
     {
         var data = new QuadtreeData(target);
         _targetDataDictionary.Add(target, data);
         _collisionTree.Insert(data);
     }
 }
예제 #4
0
 /// <summary>
 ///     Removes data from the Quadtree
 /// </summary>
 /// <param name="data">The data to be removed.</param>
 public void Remove(QuadtreeData data)
 {
     if (IsLeaf)
     {
         data.RemoveParent(this);
         Contents.Remove(data);
     }
     else
     {
         throw new InvalidOperationException($"Cannot remove from a non leaf {nameof(Quadtree)}");
     }
 }
예제 #5
0
 private void AddToLeaf(QuadtreeData data)
 {
     data.AddParent(this);
     Contents.Add(data);
 }