Beispiel #1
0
        public void Update()
        {
            //things that move out the area get removed from that area and readded to the tree
            if (Children[0] == null)
            {
                for (int i = Count - 1; i > -1; i--)
                {
                    if (!Items[i].Position.Intersects(Region))
                    {
                        //GameState.quadTree.AddItem(Items[i]);
                        Items.RemoveAt(i);
                    }
                }
                //when we force an update of the quad tree remove duplicates
                List<IQuadItem> dupe_free = new List<IQuadItem>();
                for (int i = 0; i < Count; i++)
                    if (!dupe_free.Contains(Items[i]))
                        dupe_free.Add(Items[i]);
                Items = dupe_free;
            }
            else
            {
                for (int i = 0; i < 4; i++)
                {
                    Children[i].Update();
                }
            }

            //if this leaf gets too big, we split it up
            if ((Count > Parent.ChildSize) && (Width > Parent.RegionWidthMinimum))
            {
                for (int i = 0; i < 4; i++)
                {
                    MakeSplit(i);
                    Children[i] = new QuadTreeLeaf(split_rect.X, split_rect.Y, split_rect.Width, split_rect.Height, this.Parent);
                }
                for (int i = 0; i < Count; i++)
                {
                    for (int j = 0; j < 4; j++)
                    {
                        Children[j].AddItem(Items[i]);
                    }
                }
                Items.Clear();
            }
        }
Beispiel #2
0
        public void AddItem(IQuadItem toAdd)
        {
            //if the item isnt in the super region it isnt in the child regions either
            if (toAdd.Position.Intersects(Region))
            {
                if (Children[0] == null)
                {
                    //if (!Items.Contains(toAdd))
                    Items.Add(toAdd);
                }
                else
                {
                    for (int i = 0; i < 4; i++)
                        Children[i].AddItem(toAdd);
                }
            }

            //if this leaf gets too big, we split it up
            if ((Count > Parent.ChildSize) && (Width > Parent.RegionWidthMinimum))
            {
                for (int i = 0; i < 4; i++)
                {
                    MakeSplit(i);
                    Children[i] = new QuadTreeLeaf(split_rect.X, split_rect.Y, split_rect.Width, split_rect.Height, this.Parent);
                }
                for (int i = 0; i < Count; i++)
                {
                    for (int j = 0; j < 4; j++)
                    {
                        Children[j].AddItem(Items[i]);
                    }
                }
                Items.Clear();
            }
        }