Ejemplo n.º 1
0
        private bool OverlapNode()
        {
            // Calculate bulk hull for _object
            _objectHullX      = (_object.X < _object.Last.X) ? _object.X : _object.Last.X;
            _objectHullY      = (_object.Y < _object.Last.Y) ? _object.Y : _object.Last.Y;
            _objectHullWidth  = _object.X - _object.Last.X;
            _objectHullWidth  = _object.Width + ((_objectHullWidth > 0) ? _objectHullWidth : -_objectHullWidth);
            _objectHullHeight = _object.Y - _object.Last.Y;
            _objectHullHeight = _object.Height + ((_objectHullHeight > 0) ? _objectHullHeight : -_objectHullHeight);

            // Walk the list and check for overlaps
            bool      overlapProcessed = false;
            FlxObject checkObject;

            while (_iterator != null)
            {
                checkObject = _iterator.FlxObject;
                if (_object == checkObject || !checkObject.Exists || checkObject.AllowCollisions <= 0)
                {
                    _iterator = _iterator.Next;
                    continue;
                }

                // Calculate bulk hull for checkObject
                _checkObjectHullX      = (checkObject.X < checkObject.Last.X) ? checkObject.X : checkObject.Last.X;
                _checkObjectHullY      = (checkObject.Y < checkObject.Last.Y) ? checkObject.Y : checkObject.Last.Y;
                _checkObjectHullWidth  = checkObject.X - checkObject.Last.X;
                _checkObjectHullWidth  = checkObject.Width + ((_checkObjectHullWidth > 0) ? _checkObjectHullWidth : -_checkObjectHullWidth);
                _checkObjectHullHeight = checkObject.Y - checkObject.Last.Y;
                _checkObjectHullHeight = checkObject.Height + ((_checkObjectHullHeight > 0) ? _checkObjectHullHeight : -_checkObjectHullHeight);

                // Check for intersection of the two hulls
                if ((_objectHullX + _objectHullWidth > _checkObjectHullX) &&
                    (_objectHullX < _checkObjectHullX + _checkObjectHullWidth) &&
                    (_objectHullY + _objectHullHeight > _checkObjectHullY) &&
                    (_objectHullY < _checkObjectHullY + _checkObjectHullHeight))
                {
                    // Execute callback functions if they exist
                    if (_processingCallback == null || _processingCallback(_object, checkObject))
                    {
                        overlapProcessed = true;
                        _notifyCallback?.Invoke(_object, checkObject);
                    }
                }
                if (_iterator != null)
                {
                    _iterator = _iterator.Next;
                }
            }

            return(overlapProcessed);
        }
Ejemplo n.º 2
0
        public bool Execute()
        {
            bool overlapProcessed = false;

            if (_headA.FlxObject != null)
            {
                var iterator = _headA;
                while (iterator != null)
                {
                    _object = iterator.FlxObject;
                    if (_useBothLists)
                    {
                        _iterator = _headB;
                    }
                    else
                    {
                        _iterator = iterator.Next;
                    }
                    if (_object != null && _object.Exists && _object.AllowCollisions > 0 && _iterator != null && _iterator.FlxObject != null && OverlapNode())
                    {
                        overlapProcessed = true;
                    }
                    iterator = iterator.Next;
                }
            }

            // Advance through the tree by calling overlap on each child
            if ((_northWestTree != null) && _northWestTree.Execute())
            {
                overlapProcessed = true;
            }
            if ((_northEastTree != null) && _northEastTree.Execute())
            {
                overlapProcessed = true;
            }
            if ((_southEastTree != null) && _southEastTree.Execute())
            {
                overlapProcessed = true;
            }
            if ((_southWestTree != null) && _southWestTree.Execute())
            {
                overlapProcessed = true;
            }

            return(overlapProcessed);
        }
Ejemplo n.º 3
0
        private void AddToList()
        {
            FlxLinkedList ot;

            if (_list == A_LIST)
            {
                if (_tailA.FlxObject != null)
                {
                    ot      = _tailA;
                    _tailA  = FlxLinkedList.Recycle();
                    ot.Next = _tailA;
                }
                _tailA.FlxObject = _object;
            }
            else
            {
                if (_tailB.FlxObject != null)
                {
                    ot      = _tailB;
                    _tailB  = FlxLinkedList.Recycle();
                    ot.Next = _tailB;
                }
                _tailB.FlxObject = _object;
            }
            if (!_canSubdivide)
            {
                return;
            }
            if (_northWestTree != null)
            {
                _northWestTree.AddToList();
            }
            if (_northEastTree != null)
            {
                _northEastTree.AddToList();
            }
            if (_southEastTree != null)
            {
                _southEastTree.AddToList();
            }
            if (_southWestTree != null)
            {
                _southWestTree.AddToList();
            }
        }
Ejemplo n.º 4
0
        public void Dispose()
        {
            _headA = null;
            _headB = null;

            _tailA = null;
            _tailB = null;

            _northWestTree = null;
            _northEastTree = null;

            _southEastTree = null;
            _southWestTree = null;

            _object             = null;
            _processingCallback = null;
            _notifyCallback     = null;

            Exists = false;

            next             = _cachedTreesHead;
            _cachedTreesHead = this;
            _NUM_CACHED_QUAD_TREES++;
        }
Ejemplo n.º 5
0
        public void Reset(float x, float y, float width, float height, FlxQuadTree parent = null)
        {
            Exists = true;
            Set(x, y, width, height);

            _headA = _tailA = FlxLinkedList.Recycle();
            _headB = _tailB = FlxLinkedList.Recycle();

            if (parent != null)
            {
                FlxLinkedList iterator;
                FlxLinkedList ot;
                if (parent._headA.FlxObject != null)
                {
                    iterator = parent._headA;
                    while (iterator != null)
                    {
                        if (_tailA.FlxObject != null)
                        {
                            ot      = _tailA;
                            _tailA  = FlxLinkedList.Recycle();
                            ot.Next = _tailA;
                        }
                        _tailA.FlxObject = iterator.FlxObject;
                        iterator         = iterator.Next;
                    }
                }
                if (parent._headB.FlxObject != null)
                {
                    iterator = parent._headB;
                    while (iterator != null)
                    {
                        if (_tailB.FlxObject != null)
                        {
                            ot      = _tailB;
                            _tailB  = FlxLinkedList.Recycle();
                            ot.Next = _tailB;
                        }
                        _tailB.FlxObject = iterator.FlxObject;
                        iterator         = iterator.Next;
                    }
                }
            }
            else
            {
                _min = (int)Math.Floor((Width + Height) / (2 * Divisions));
            }
            _canSubdivide = Width > _min || Height > _min;

            _northWestTree = null;
            _northEastTree = null;
            _southWestTree = null;
            _southEastTree = null;
            _leftEdge      = X;
            _rightEdge     = Right;
            _halfWidth     = Width / 2;
            _midpointX     = _leftEdge + _halfWidth;
            _topEdge       = Y;
            _bottomEdge    = Bottom;
            _halfHeight    = Height / 2;
            _midpointY     = _topEdge + _halfHeight;
        }