Example #1
0
 public bool Contains(CenteredRectangle other)
 {
     if (topLeft.x <= other.topLeft.x && topLeft.y >= other.topLeft.y &&
         botRight.x >= other.botRight.x && botRight.y <= other.botRight.y)
     {
         return(true);
     }
     return(false);
 }
Example #2
0
 public QuadTreeNode(CenteredRectangle bounds, QuadTreeNode parent, int leafNumber)
 {
     quadTreeRootID          = 4 * parent.quadTreeRootID + leafNumber;
     this.parent             = parent;
     centeredRect            = bounds;
     leaves                  = new List <Entity>();
     maxLeavesBeforeSubTrees = Constants.QuadTreeMaxReferences;
     subNodes                = new List <QuadTreeNode>(4);
     QuadTreeSystem.quadTreeDict.Add(quadTreeRootID, this);
     //QuadTreeJobSystem.quadTreeHashMap.Add(quadTreeRootID, new QuadNode { qtn = this });
 }
Example #3
0
 public QuadTreeNode(CenteredRectangle bounds)
 {
     quadTreeRootID          = 0;
     parent                  = null;
     centeredRect            = bounds;
     leaves                  = new List <Entity>();
     maxLeavesBeforeSubTrees = Constants.QuadTreeMaxReferences;
     subNodes                = new List <QuadTreeNode>(4);
     QuadTreeSystem.quadTreeDict.Add(0, this);
     //QuadTreeJobSystem.quadTreeHashMap.Add(quadTreeRootID, new QuadNode { qtn = this });
 }
Example #4
0
        public bool Intersects(CenteredRectangle other)
        {
            if (topLeft.x > other.botRight.x || other.topLeft.x > botRight.x)
            {
                return(false);
            }
            if (topLeft.y < other.botRight.y || other.topLeft.y < botRight.y)
            {
                return(false);
            }

            return(true);
        }
Example #5
0
        /*
         * AddReference always seeks to add an entity to the QuadTree starting at the root node
         * Any code that therefore immediately adds to the leaves of the current node (the first is parent)
         * will therefore be added to the root node immediately
         */
        public void AddReference(Entity entity, CollisionComponent coll, Translation translation)
        {
            /* Temp Solution, ALL BOUNDARIES ARE ADDED TO THE ROOT NODE'S LEAVES */
            if (World.Active.EntityManager.HasComponent <PlayerBoundaryComponent>(entity) || World.Active.EntityManager.HasComponent <ProjectileBoundaryComponent>(entity))
            {
                leaves.Add(entity);
                World.Active.EntityManager.SetComponentData(entity, new QuadTreeReferenceComponent(quadTreeRootID));
            }
            else if (!allNodes && leaves.Count < maxLeavesBeforeSubTrees)
            {
                leaves.Add(entity);
                World.Active.EntityManager.SetComponentData(entity, new QuadTreeReferenceComponent(quadTreeRootID));
            }
            else
            {
                if (!allNodes)
                {
                    subNodes.Add(new QuadTreeNode(new CenteredRectangle(centeredRect.width / 2, centeredRect.height / 2,
                                                                        new float3(centeredRect.center.x - centeredRect.width / 4, centeredRect.center.y - centeredRect.height / 4, 0)),
                                                  this, 1));
                    subNodes.Add(new QuadTreeNode(new CenteredRectangle(centeredRect.width / 2, centeredRect.height / 2,
                                                                        new float3(centeredRect.center.x + centeredRect.width / 4, centeredRect.center.y - centeredRect.height / 4, 0)),
                                                  this, 2));
                    subNodes.Add(new QuadTreeNode(new CenteredRectangle(centeredRect.width / 2, centeredRect.height / 2,
                                                                        new float3(centeredRect.center.x - centeredRect.width / 4, centeredRect.center.y + centeredRect.height / 4, 0)),
                                                  this, 3));
                    subNodes.Add(new QuadTreeNode(new CenteredRectangle(centeredRect.width / 2, centeredRect.height / 2,
                                                                        new float3(centeredRect.center.x + centeredRect.width / 4, centeredRect.center.y + centeredRect.height / 4, 0)),
                                                  this, 4));
                    allNodes = true;

                    List <Entity> temp = new List <Entity>();
                    temp.AddRange(leaves);
                    leaves.Clear();
                    AddReference(entity, coll, translation);
                    foreach (Entity e in temp)
                    {
                        AddReference(e, World.Active.EntityManager.GetComponentData <CollisionComponent>(e), World.Active.EntityManager.GetComponentData <Translation>(e));
                    }
                }
                else
                {
                    bool noContains = true;
                    foreach (QuadTreeNode qtn in subNodes)
                    {
                        CenteredRectangle entityBounds = new CenteredRectangle(coll.width, coll.collisionRadius, translation.Value);
                        if (qtn.centeredRect.Contains(entityBounds))
                        {
                            qtn.AddReference(entity, coll, translation);
                            noContains = false;
                            break;
                        }
                    }
                    if (noContains)
                    {
                        leaves.Add(entity);
                        World.Active.EntityManager.SetComponentData(entity, new QuadTreeReferenceComponent(quadTreeRootID));
                    }
                }
            }
        }