public bool RemoveFromRoot(GameObject ParticleObject)
    {
        //for root node
        if (TotalLeafNodes == 1)
        {
            SMainInstance.ClearQuadtree();
            return(true);
        }

        //remove this child
        //QuadTree deleteChildOfParent = TempParent._ParentNode;
        _ChildNode = null;
        _ChildNodes.Remove(InQuadrant(ParticleObject));
        --TotalLeafNodes;

        //handle last two childrens condition
        if (TotalLeafNodes == 1)
        {
            ClearPartitionDrawn();
            for (int leaf = 0; leaf < 4; ++leaf)
            {
                if (_ChildNodes.ContainsKey(leaf))
                {
                    _ChildNode = _ChildNodes[leaf]._ChildNode;
                    _ChildNodes.Clear();
                    return(true);
                }
            }
        }

        return(true);
    }
 void DrawSplitSelf()
 {
     if (_SelfPartitionPrefab == null)
     {
         _SelfPartitionPrefab = SMainInstance._SpawnQuadTreePartitioner(_CurrentDepth, this.Center);
     }
 }
    public void Clear()
    {
        if (TotalLeafNodes != 0)
        {
            if (_ChildNode == null)
            {
                for (int leaf = 0; leaf < 4; ++leaf)
                {
                    if (_ChildNodes.ContainsKey(leaf))
                    {
                        _ChildNodes[leaf].Clear();
                        SMainInstance.DestroyQuadTreeObject(_SelfPartitionPrefab);
                    }
                }
            }
            else
            {
                _ChildNode = null;
            }
            TotalLeafNodes = 0;
            _CurrentDepth  = 0;
            SMaxDepth      = 0;

            _ParentNode          = null;
            _SelfPartitionPrefab = null;
            _ChildNode           = null;
            _SPreviousChildNode  = null;
            _ChildNodes          = new Dictionary <int, QuadTree>();
            _PartitionDrawn      = false;
        }
    }
    public bool RemoveParticle(GameObject ParticleObject)
    {
        QuadTree TempParent = FindParticleParent(ParticleObject);

        if (TempParent != null)
        {
            TempParent._ChildNode = null;
            //	if (TempParent._TotalLeafNodes == 1)
            {
                while (TempParent._ParentNode != null)
                {
                    --TempParent.TotalLeafNodes;


                    if (TempParent.TotalLeafNodes == 1)
                    {
                        SMainInstance.DestroyQuadTreeObject(TempParent._SelfPartitionPrefab);
                        TempParent._SelfPartitionPrefab = null;

                        //update childrens
                        for (int leaf = 0; leaf < 4; ++leaf)
                        {
                            if (TempParent._ChildNodes.ContainsKey(leaf))
                            {
                                TempParent._ChildNode = TempParent._ChildNodes[leaf]._ChildNode;

                                TempParent._ChildNodes[leaf]._ChildNode = null;
                                TempParent._ChildNodes[leaf].Clear();
                                TempParent._ChildNodes.Clear();
                            }
                        }
                    }

                    TempParent._PartitionDrawn = false;
                    TempParent = TempParent._ParentNode;
                }
                if (TempParent._ParentNode == null)
                {
                    if (TempParent.TotalLeafNodes == 1)
                    {
                        --TempParent.TotalLeafNodes;
                        SMainInstance.DestroyQuadTreeObject(TempParent._SelfPartitionPrefab);
                        TempParent._SelfPartitionPrefab = null;
                    }
                }
            }
            //if (temp._TotalLeafNodes == 0)
            //{
            //	MainInstance.DestroyObject(temp._SelfPartitionPrefab);
            //	temp._SelfPartitionPrefab = null;
            //}
            return(true);
        }

        //object not found
        return(false);
    }
    public void ClearPartitionDrawn()
    {
        SMainInstance.DestroyQuadTreeObject(_SelfPartitionPrefab);
//		Debug.Log("DEL8888888888888888888888888888888888888888888888888888888");
//		if (TotalLeafNodes > 0)
//		{
//			if (_PartitionDrawn)
//				_PartitionDrawn = false;
//			for (int leaf = 0; leaf < 4; ++leaf)
//			{
//				if (_ChildNodes.ContainsKey(leaf))
//					_ChildNodes[leaf].ClearPartitionDrawn();
//			}
//		}
    }
    public bool Remove(GameObject particleObject)
    {
        if (TotalLeafNodes == 0)
        {
            return(false);
        }

        //handle the root node
        QuadTree TempParent = FindParticleParent(particleObject);

        if (TempParent == null)
        {
            return(false);
        }

        //handle the root node
        if ((TempParent._CurrentDepth == 0) || (TempParent._ParentNode._CurrentDepth == 0))
        {
            RemoveFromRoot(particleObject);
            return(true);
        }

        //now there will be only one root node condition to handle after this
        QuadTree   TempParent1             = TempParent;
        QuadTree   parentOfChildToBeDelete = TempParent._ParentNode;
        int        FoundInQuadrant         = -1;
        GameObject TempParticle            = null;

        //
        {
            //store the quadrant in which the particle is
            FoundInQuadrant = TempParent._ParentNode.InQuadrant(particleObject);
            //remove this child
            TempParent._ChildNode = null;
            parentOfChildToBeDelete._ChildNodes.Remove(FoundInQuadrant);


            // decrement all the TotalLeafNodes up in tree
            while (TempParent1._ParentNode != null)
            {
                //Debug.Log("--TempParent1.TotalLeafNodes:" + --TempParent1.TotalLeafNodes);
                TempParent1 = TempParent1._ParentNode;
            }
            //decrement the TotalLeafNodes of parent
            --TotalLeafNodes;



            TempParent = parentOfChildToBeDelete;
            //if (TempParent._ParentNode != null)
            {
                TempParent1 = TempParent;                //._ParentNode;

                //handle the condition for second child
                //store the second particle in tempParticle
                if (TempParent1.TotalLeafNodes == 1)
                {
                    for (int leaf = 0; leaf < 4; ++leaf)
                    {
                        if (leaf == FoundInQuadrant)
                        {
                            continue;
                        }

                        if (TempParent1._ChildNodes.ContainsKey(leaf))
                        {
                            TempParticle = TempParent1._ChildNodes[leaf]._ChildNode;
                            if (TempParticle != null)
                            {
                                break;
                            }
                        }
                    }


                    //TempParticle should not be null
                    if (TempParticle != null)
                    {
                        while ((TempParent1.TotalLeafNodes == 1))
                        {
                            Debug.Log("^ depth: " + _CurrentDepth);
                            //handle the root condition
                            if (TempParent1._ParentNode == null)
                            {
                                SMainInstance.tempRef = TempParticle;
                                SMainInstance.ClearAndInsertQuadtree();
                                return(true);
                            }
                            TempParent1 = TempParent1._ParentNode;
                        }

                        //now tempParent has more than one leaf nodes
                        //one in the same quadrant as tempParticle and other in remaining quadrant
                        FoundInQuadrant = TempParent1.InQuadrant(TempParticle);
                        //Debug.Log("FoundInQuadrant:" + FoundInQuadrant);
                        //Debug.Log("TotalLeafNodes:" + TempParent1.TotalLeafNodes);
                        TempParent1._ChildNodes[FoundInQuadrant].Clear();
                        TempParent1._ChildNodes.Remove(FoundInQuadrant);


                        //if there are only two children in tempParent1 then we will again have to fix that for insertion
                        if (TempParent1.TotalLeafNodes == 2)
                        {
                            Debug.Assert((TempParent1._ChildNode == null), "CHILD FOR INSERTION NOT NULLL!!!!");
                            for (int leaf = 0; leaf < 4; ++leaf)
                            {
                                if (leaf == FoundInQuadrant)
                                {
                                    continue;
                                }

                                if (TempParent1._ChildNodes.ContainsKey(leaf))
                                {
                                    TempParent1._ChildNode = TempParent1._ChildNodes[leaf]._ChildNode;
                                    if (TempParent1._ChildNode != null)
                                    {
                                        //Debug.Log("1234 THIS WAS A MAJOR BUG1!");
                                        TempParent1._ChildNodes.Clear();
                                        break;
                                    }
                                }
                            }
                        }
                        //TempParent = TempParent1;

                        //while (TempParent._ParentNode != TempParent1)
                        //{
                        //	Debug.Log("--TempParent1.TotalLeafNodes:" + TempParent.TotalLeafNodes--);
                        //	TempParent = TempParent._ParentNode;
                        //}

                        --TempParent1.TotalLeafNodes;
                        TempParent1.Insert(TempParticle);

                        return(true);
                    }
                    else
                    {
                        throw new System.InvalidOperationException("CHILD FOUND NULLLLL!!!!!!");
                    }
                }
            }
        }

        return(false);
    }