IEnumerator RemoveAgents(QtAgent[] agents) { Debug.Log("Removing... " + quadtree.TotalElementCount); foreach (QtAgent agent in agents) { quadtree.Remove(agent); GameObject.Destroy(agent.agGameObject); yield return(new WaitForSeconds(addInterval)); } Debug.Log("Finish remove " + quadtree.TotalElementCount); quadtree.LevelDesc(); }
/** * Method to update agent in quadtree especially * when agent moving around **/ void UpdateAgentInQuadtree() { if (lastPosition != newPosition) { if (currentNode != null) { //check collision between agent and node boundary CollisionResult result = this.GetShape().IntersectWithShape(currentNode.Boundary); switch (result) { case CollisionResult.Fit: if (!currentNode.IsLeaf) //has child node //for all child nodes { IEnumerator er = currentNode.AllNodes.GetEnumerator(); while (er.MoveNext()) { //if agent fit in this child if (this.GetShape().IntersectWithShape((er.Current as QuadtreeNode).Boundary) == CollisionResult.Fit) { //move to child node currentNode.Remove(this); (er.Current as QuadtreeNode).Add(this); } } } break; case CollisionResult.Overlap: //find parent until agent complete fit in QuadtreeNode pNode = currentNode.Parent; while (pNode != null) { if (this.GetShape().IntersectWithShape(pNode.Boundary) == CollisionResult.Fit) { break; } pNode = pNode.Parent; } currentNode.Remove(this); if (pNode == null) //root node { currentNode.rootQuadtree().Add(this); } else { pNode.Add(this); } break; case CollisionResult.None: currentNode.Remove(this); currentNode.rootQuadtree().Add(this); //add from root quadtree break; } } lastPosition = newPosition; } }