Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using ADS.Utility;
- using Engine;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Graphics;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ADS.Managers.High_Tier.State
- {
- class QuadtreeNode
- {
- private Rectangle bounds;
- public Rectangle Bounds { get { return bounds; } set { bounds = value; } }
- protected int maxItems;
- private bool Split;
- private List<QuadtreeNode> children;
- public List<QuadtreeNode> Children { get { return children; } set { children = value; } }
- private Quadtree tree;
- public Quadtree Tree { get { return tree; } set { tree = value; } }
- private QuadtreeNode parent;
- public QuadtreeNode Parent { get { return parent; } set { parent = value; } }
- public List<IEntity> items;
- public QuadtreeNode(QuadtreeNode _parent, int pMax, Quadtree pTree, Rectangle pBounds)
- {
- Parent = _parent;
- maxItems = pMax;
- Tree = pTree;
- items = new List<IEntity>();
- Bounds = pBounds;
- Tree.Nodes.Add(this);
- }
- public void Divide()
- {
- Children.Add(new QuadtreeNode(this, maxItems, Tree, new Rectangle(Bounds.Location.X, Bounds.Location.Y, Bounds.Width / 2, Bounds.Height / 2)));
- Children.Add(new QuadtreeNode(this, maxItems, Tree, new Rectangle(Bounds.Location.X + (Bounds.Width / 2), Bounds.Location.Y, Bounds.Width / 2, Bounds.Height / 2)));
- Children.Add(new QuadtreeNode(this, maxItems, Tree, new Rectangle(Bounds.Location.X, Bounds.Location.Y + (Bounds.Height / 2), Bounds.Width / 2, Bounds.Height / 2)));
- Children.Add(new QuadtreeNode(this, maxItems, Tree, new Rectangle(Bounds.Location.X + (Bounds.Width / 2), Bounds.Location.Y + (Bounds.Height / 2), Bounds.Width / 2, Bounds.Height / 2)));
- Split = true;
- }
- public void insert(IEntity ent)
- {
- if (Bounds.Contains(ent.Bounds))
- {
- items.Add(ent);
- if (items.Count >= maxItems && !Split)
- {
- Divide();
- insertInChild(ent);
- }
- }
- }
- public void insertInChild(IEntity ent)
- {
- bool insert = false;
- foreach (QuadtreeNode child in Children)
- {
- if (ent.Bounds.Contains(child.Bounds) && insert == false)
- {
- child.insert(ent);
- insert = true;
- }
- }
- }
- public List<IEntity> getItems()
- {
- List<IEntity> ents = new List<IEntity>();
- if (Split)
- {
- foreach (QuadtreeNode child in Children)
- {
- ents.AddRange(getItems());
- }
- }
- foreach(IEntity ent in items)
- {
- if (Bounds.Contains(ent.Bounds))
- {
- ents.Add(ent);
- }
- }
- return ents;
- }
- public void Collapse()
- {
- if (Split)
- {
- if (items.Count < maxItems)
- {
- foreach (QuadtreeNode child in Children)
- {
- child.Collapse();
- child.clear();
- Children[Children.IndexOf(child)] = null;
- }
- Split = false;
- }
- }
- }
- public void clear()
- {
- items.Clear();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement