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

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

            CallParentUpdateMaxRadius();
        }
    }
 bool DoRemoveLeafFromSelf(QuadtreeWithEventDelegateLeaf <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);
 }
 //存入
 public bool SetLeaf(QuadtreeWithEventDelegateLeaf <T> leaf)
 {
     if (DontHaveChildren())
     {
         return(SetLeafToSelf(leaf));
     }
     else
     {
         return(SetLeafToChildren(leaf));
     }
 }
 bool RemoveLeafInTotalTree(QuadtreeWithEventDelegateLeaf <T> leaf)
 {
     if (DontHaveChildren())
     {
         return(DoRemoveLeafFromSelf(leaf));
     }
     else
     {
         return(RemoveLeafInTotalTreeFromChildren(leaf));
     }
 }
    bool SetLeafToSelf(QuadtreeWithEventDelegateLeaf <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);
    }
 bool RemoveLeafInTotalTreeFromChildren(QuadtreeWithEventDelegateLeaf <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);
 }
 bool RemoveLeafFromChildren(QuadtreeWithEventDelegateLeaf <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));
 }
    bool SetLeafToChildren(QuadtreeWithEventDelegateLeaf <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);
    }
 void ResetLeaf(QuadtreeWithEventDelegateLeaf <T> leaf)
 {
     Debug.Log("<color=#800080>位置在" + _field.top + "," + _field.right + "," + _field.bottom + "," + _field.left + "的树梢节点移除位置在" + leaf.position + "半径是" + leaf.radius + "的叶子,重新存入树</color>");
     RemoveLeafFromSelf(leaf);
     _root.SetLeaf(leaf);
 }
コード例 #12
0
 private void Awake()
 {
     _transform = transform;
     _leaf      = new QuadtreeWithEventDelegateLeaf <GameObject>(gameObject, GetLeafPosition(), _radius);
 }
 public static bool RemoveLeaf(QuadtreeWithEventDelegateLeaf <GameObject> leaf)
 {
     return(_quadtree.RemoveLeaf(leaf));
 }
 public static GameObject[] CheckCollision(QuadtreeWithEventDelegateLeaf <GameObject> leaf)
 {
     return(_quadtree.CheckCollision(leaf));
 }