Esempio n. 1
0
        /// <summary>
        /// constructor
        /// </summary>
        public Node(int parentNodeId, REGION region, Box boundaryBox)
        {
            _Tl = null;
            _Tr = null;
            _Bl = null;
            _Br = null;

            _Region = region;
            _NodeId = parentNodeId * 10 + (int)_Region;
            _ListObject = new List<TileObject>();
            //node root
            if (parentNodeId == 0)
                _BoundaryBox = boundaryBox;
            else
            {
                switch (_Region)
                {
                    case REGION.TOP_LEFT:
                        _BoundaryBox = new Box(
                            boundaryBox.X - boundaryBox.Width/4,
                            boundaryBox.Y + boundaryBox.Height/4,
                            boundaryBox.Width/2,
                            boundaryBox.Height/2
                            );
                        break;
                    case REGION.TOP_RIGHT:
                        _BoundaryBox = new Box(
                            boundaryBox.X + boundaryBox.Width / 4,
                            boundaryBox.Y + boundaryBox.Height / 4,
                            boundaryBox.Width / 2,
                            boundaryBox.Height / 2
                            );
                        break;
                    case REGION.BOT_LEFT:
                        _BoundaryBox = new Box(
                            boundaryBox.X - boundaryBox.Width / 4,
                            boundaryBox.Y - boundaryBox.Height / 4,
                            boundaryBox.Width / 2,
                            boundaryBox.Height / 2
                            );
                        break;
                    case REGION.BOT_RIGHT:
                        _BoundaryBox = new Box(
                            boundaryBox.X + boundaryBox.Width / 4,
                            boundaryBox.Y - boundaryBox.Height / 4,
                            boundaryBox.Width / 2,
                            boundaryBox.Height / 2
                            );
                        break;
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// insert node
        /// </summary>
        public static void InsertNode(Node node, TileObject obj)
        {
            //if object doesn't belong to node, then return
            if (!Box.IsIntersect(node.BoundaryBox, obj.MovementRangeBox))
                return;

            //if node covers all screenhight and screenwidth, then divide it into 4 subnodes
            if (node.BoundaryBox.Width >= SCREEN_WIDTH + 6 && node.BoundaryBox.Height >= SCREEN_HEIGHT + 6)
            {
                if (node._Tl == null)
                    node._Tl = new Node(node._NodeId, REGION.TOP_LEFT, node.BoundaryBox);
                if (node._Tr == null)
                    node._Tr = new Node(node._NodeId, REGION.TOP_RIGHT, node.BoundaryBox);
                if (node._Bl == null)
                    node._Bl = new Node(node._NodeId, REGION.BOT_LEFT, node.BoundaryBox);
                if (node._Br == null)
                    node._Br = new Node(node._NodeId, REGION.BOT_RIGHT, node.BoundaryBox);

                int left, right, top, bot;
                left = obj.MovementRangeBox.X - obj.MovementRangeBox.Width / 2;
                right = obj.MovementRangeBox.X + obj.MovementRangeBox.Width / 2;
                top = obj.MovementRangeBox.Y + obj.MovementRangeBox.Height / 2;
                bot = obj.MovementRangeBox.Y - obj.MovementRangeBox.Height / 2;
                //if object places in two diagonals, then add it to node
                if ((left <= node.BoundaryBox.X && right >= node.BoundaryBox.X)
                    || (bot <= node.BoundaryBox.Y && top >= node.BoundaryBox.Y))
                {
                    node.ListObject.Add(obj);
                }
                else
                {
                    //insert each subnode to node
                    InsertNode(node._Tl, obj);
                    InsertNode(node._Tr, obj);
                    InsertNode(node._Bl, obj);
                    InsertNode(node._Br, obj);
                }
            }
            //else if node just covers enough screenwidth or screenheight, then add obj to node
            else
                node.ListObject.Add(obj);
        }