Beispiel #1
0
        /*
         *  碰撞检测,加了一个传叶子检测碰撞的方法,这样碰撞器就可以自己检测碰撞了。
         *  原理很简单,先检测碰撞,之后把叶子自己剔除出去就行了。
         */
        public T[] CheckCollision(QuadtreeLeafEvent <T> leaf)
        {
            List <T> objs = new List <T>(CheckCollision(leaf.position, leaf.radius));

            objs.Remove(leaf.obj);
            return(objs.ToArray());
        }
Beispiel #2
0
 bool RemoveLeafFromSelf(QuadtreeLeafEvent <T> leaf)
 {
     if (DoRemoveLeafFromSelf(leaf))
     {
         return(true);
     }
     return(_root.RemoveLeafInTotalTree(leaf));
 }
Beispiel #3
0
        void UpdateMaxRadiusWhenSetLeaf(QuadtreeLeafEvent <T> leaf)
        {
            if (leaf.radius > _maxRadius)       //只有存入的叶子的半径超过了现在节点的最大半径才需要更新最大半径,存入更小的叶子并不会影响到检测。
            {
                _maxRadius = leaf.radius;

                CallParentUpdateMaxRadius();
            }
        }
Beispiel #4
0
 bool DoRemoveLeafFromSelf(QuadtreeLeafEvent <T> leaf)
 {
     if (_leafs.Remove(leaf))
     {
         UpdateMaxRadiusWhenRemoveLeaf();
         Debug.Log("<color=#802030>位置在" + _field.top + "," + _field.right + "," + _field.bottom + "," + _field.left + "的树梢节点移除位置在" + leaf.position + "半径是" + leaf.radius + "的叶子,移除后的最大半径是" + _maxRadius + "</color>");
         return(true);
     }
     return(false);
 }
Beispiel #5
0
 //存入
 public bool SetLeaf(QuadtreeLeafEvent <T> leaf)
 {
     if (DontHaveChildren())
     {
         return(SetLeafToSelf(leaf));
     }
     else
     {
         return(SetLeafToChildren(leaf));
     }
 }
Beispiel #6
0
 bool RemoveLeafInTotalTree(QuadtreeLeafEvent <T> leaf)
 {
     if (DontHaveChildren())
     {
         return(DoRemoveLeafFromSelf(leaf));
     }
     else
     {
         return(RemoveLeafInTotalTreeFromChildren(leaf));
     }
 }
Beispiel #7
0
 //移除
 public bool RemoveLeaf(QuadtreeLeafEvent <T> leaf)
 {
     if (DontHaveChildren())
     {
         return(RemoveLeafFromSelf(leaf));
     }
     else
     {
         return(RemoveLeafFromChildren(leaf));
     }
 }
Beispiel #8
0
        bool SetLeafToSelf(QuadtreeLeafEvent <T> leaf)
        {
            if (this == _root && !_field.Contains(leaf.position))
            {
                Debug.LogError("存入叶子失败,叶子不在四叉树范围内");
                return(false);
            }

            _leafs.Add(leaf);
            UpdateMaxRadiusWhenSetLeaf(leaf);
            Debug.Log("<color=#0040A0>位置在" + _field.top + "," + _field.right + "," + _field.bottom + "," + _field.left + "的树梢节点存入位置在" + leaf.position + "半径是" + leaf.radius + "的叶子,存入后的最大半径是" + _maxRadius + "</color>");
            //是的!Log输出同样支持HTML标签,颜色、粗体、斜体等都可以做到
            CheckAndDoSplit();
            return(true);
        }
Beispiel #9
0
 bool RemoveLeafInTotalTreeFromChildren(QuadtreeLeafEvent <T> leaf)
 {
     if (_upperRightChild.RemoveLeafInTotalTree(leaf))
     {
         return(true);                                    //如果子节点移除成功了,那就说明不需要继续遍历剩下的节点了,直接返回 true
     }
     if (_lowerRightChild.RemoveLeafInTotalTree(leaf))
     {
         return(true);
     }
     if (_lowerLeftChild.RemoveLeafInTotalTree(leaf))
     {
         return(true);
     }
     if (_upperLeftChild.RemoveLeafInTotalTree(leaf))
     {
         return(true);
     }
     return(false);
 }
Beispiel #10
0
 bool RemoveLeafFromChildren(QuadtreeLeafEvent <T> leaf)
 {
     Debug.Log("<color=#802030>位置在" + _field.top + "," + _field.right + "," + _field.bottom + "," + _field.left + "的树枝节点从子节点移除位置在" + leaf.position + "半径是" + leaf.radius + "的叶子</color>");
     if (_upperRightChild._field.Contains(leaf.position))
     {
         return(_upperRightChild.RemoveLeaf(leaf));
     }
     if (_lowerRightChild._field.Contains(leaf.position))
     {
         return(_lowerRightChild.RemoveLeaf(leaf));
     }
     if (_lowerLeftChild._field.Contains(leaf.position))
     {
         return(_lowerLeftChild.RemoveLeaf(leaf));
     }
     if (_upperLeftChild._field.Contains(leaf.position))
     {
         return(_upperLeftChild.RemoveLeaf(leaf));
     }
     return(_root.RemoveLeafInTotalTree(leaf));
 }
Beispiel #11
0
        bool SetLeafToChildren(QuadtreeLeafEvent <T> leaf)
        {
            Debug.Log("<color=#0040A0>位置在" + _field.top + "," + _field.right + "," + _field.bottom + "," + _field.left + "的树枝节点向子节点存入位置在" + leaf.position + "半径是" + leaf.radius + "的叶子</color>");
            if (_upperRightChild._field.Contains(leaf.position))
            {
                return(_upperRightChild.SetLeaf(leaf));
            }
            if (_lowerRightChild._field.Contains(leaf.position))
            {
                return(_lowerRightChild.SetLeaf(leaf));
            }
            if (_lowerLeftChild._field.Contains(leaf.position))
            {
                return(_lowerLeftChild.SetLeaf(leaf));
            }
            if (_upperLeftChild._field.Contains(leaf.position))
            {
                return(_upperLeftChild.SetLeaf(leaf));
            }

            Debug.LogError("向位置在" + _field.top + "," + _field.right + "," + _field.bottom + "," + _field.left + "的节点存入叶子时发生错误:叶子不在所有子节点的范围里。");   //Debug.LogError:在Console面板输出Error,就是红色那种消息
            return(false);
        }
 public static bool RemoveLeaf(QuadtreeLeafEvent <GameObject> leaf)
 {
     return(_quadtree.RemoveLeaf(leaf));
 }
 public static GameObject[] CheckCollision(QuadtreeLeafEvent <GameObject> leaf)
 {
     return(_quadtree.CheckCollision(leaf));
 }
Beispiel #14
0
 private void Awake()
 {
     _transform = transform;
     _leaf      = new QuadtreeLeafEvent <GameObject>(gameObject, GetLeafPosition(), _radius);
 }
Beispiel #15
0
 void ResetLeaf(QuadtreeLeafEvent <T> leaf)
 {
     Debug.Log("<color=#800080>位置在" + _field.top + "," + _field.right + "," + _field.bottom + "," + _field.left + "的树梢节点移除位置在" + leaf.position + "半径是" + leaf.radius + "的叶子,重新存入树</color>");
     RemoveLeafFromSelf(leaf);
     _root.SetLeaf(leaf);
 }