Exemplo n.º 1
0
        //初始化,在作为组件被挂载时读取设置创建四叉树
        private void Awake()
        {
            QuadtreeSettingSingleton setting = Resources.Load <QuadtreeSettingSingleton>("QuadtreeWithSingletonSetting");

            //根据官方所说,Resources的最佳使用方法就是【别用它】,我个人也不喜欢这种把设置数据和游戏本体分到天涯海角的方法,所以我建议在设置完成后改成硬编码
            _quadtree = new QuadtreeDataSingleton <GameObject>(setting.top, setting.right, setting.bottom, setting.left, setting.maxLeafsNumber, setting.minSideLength);
        }
Exemplo n.º 2
0
 //移除
 public static bool RemoveLeaf(QuadtreeDataSingleton <GameObject> .Leaf leaf)
 {
     if (_quadtreeObject != null) //移除也是先检测四叉树物体是否存在,不存在的话肯定移除不了,返回 false
     {
         return(_quadtreeObject._quadtree.RemoveLeaf(leaf));
     }
     return(false);
 }
Exemplo n.º 3
0
 public static GameObject[] CheckCollision(QuadtreeDataSingleton <GameObject> .Leaf leaf)
 {
     if (_quadtreeObject != null)
     {
         return(quadtreeObject._quadtree.CheckCollision(leaf));
     }
     return(new GameObject[0]);
 }
Exemplo n.º 4
0
 T[] GetCollisionObjectsFromAChild(Vector2 checkPoint, float checkRadius, QuadtreeDataSingleton <T> child)
 {
     if (child._field.PointToFieldDistance(checkPoint) <= _maxRadius + checkRadius) //这里不光要考虑到检测半径,还要考虑到节点最大半径
     {
         return(child.CheckCollision(checkPoint, checkRadius));
     }
     return(new T[] { });
 }
Exemplo n.º 5
0
        public QuadtreeDataSingleton(float top, float right, float bottom, float left, int maxLeafNumber, float minSideLength, QuadtreeDataSingleton <T> root = null, QuadtreeDataSingleton <T> parent = null)
        {
            _field = new Field(top, right, bottom, left);

            _maxLeafsNumber = maxLeafNumber;
            _minSideLength  = minSideLength;

            _root = root != null ? root : this;

            _parent = parent;
        }
Exemplo n.º 6
0
        void Split()
        {
            Debug.Log("<color=#808000>位置在" + _field.top + "," + _field.right + "," + _field.bottom + "," + _field.left + "的树梢节点达到分割条件,进行分割</color>");

            Update();

            float xCenter = (_field.left + _field.right) / 2;
            float yCenter = (_field.bottom + _field.top) / 2;

            _upperRightChild = new QuadtreeDataSingleton <T>(_field.top, _field.right, yCenter, xCenter, _maxLeafsNumber, _minSideLength, _root, this);
            _lowerRightChild = new QuadtreeDataSingleton <T>(yCenter, _field.right, _field.bottom, xCenter, _maxLeafsNumber, _minSideLength, _root, this);
            _lowerLeftChild  = new QuadtreeDataSingleton <T>(yCenter, xCenter, _field.bottom, _field.left, _maxLeafsNumber, _minSideLength, _root, this);
            _upperLeftChild  = new QuadtreeDataSingleton <T>(_field.top, xCenter, yCenter, _field.left, _maxLeafsNumber, _minSideLength, _root, this);

            foreach (Leaf leaf in _leafs)
            {
                SetLeafToChildren(leaf);
            }
            _leafs = null;
        }
 private void Awake()
 {
     _transform = transform;
     _leaf      = new QuadtreeDataSingleton <GameObject> .Leaf(gameObject, GetLeafPosition(), _radius);
 }
Exemplo n.º 8
0
 //存入
 public static bool SetLeaf(QuadtreeDataSingleton <GameObject> .Leaf leaf)
 {
     return(quadtreeObject._quadtree.SetLeaf(leaf)); //存入时通过接口获取四叉树物体,这样能保证四叉树一直存在,不会发生存入空树
 }