public bool Add(CompositePhysicalObject unit)
 {
     if(root.Add(unit))
     {
         return true;
     }
     else
     {
         //throw new Exception("add failed");
         return false;
     }
 }
 public bool Remove(CompositePhysicalObject unit)
 {
     Leaf removeFrom = root.Remove(unit);
     if (removeFrom != null)
     {
         removeFrom.Collapse();
         return true;
     }
     else
     {
         //throw new Exception("No object to remove");
         return false;
     }
 }
        public Leaf GetLeaf(CompositePhysicalObject obj)
        {
            //this.Invariant();
            if (leafDictionary.ContainsKey(obj))
            {
                if (!leafDictionary[obj].Contains(obj))
                {
                    throw new Exception("Incorrect leaf");
                }

                return leafDictionary[obj];
            }
            throw new Exception("object does not have leaf");
        }
        public override bool Add(CompositePhysicalObject unit)
        {
            if (this.Contains(unit.Position))
            {
                unitList.Add(unit);
                leafDictionary.SetLeaf(unit, this);

                if (ObjectCount() > max_count)
                {
                    this.Expand();
                }
                return true;
            }
            return false;
        }
 public void SetLeaf(CompositePhysicalObject obj, Leaf leaf)
 {
     //this.Invariant();
     if (leaf != null)
     {
         if (!leafDictionary.ContainsKey(obj))
         {
             leafDictionary.Add(obj, leaf);
         }
         leafDictionary[obj] = leaf;
     }
     else
     {
         leafDictionary.Remove(obj);
     }
     //this.Invariant();
 }
        public override bool Add(CompositePhysicalObject obj)
        {
            if (this.Contains(obj.Position))
            {
                foreach (Node child in new List<Node>(children))
                {
                    if (child.Add(obj))
                    {
                        unitCount++;
                        return true;
                    }
                }
                throw new Exception("failed adds to QuadTree");
            }
            if (Parent == null)
            {
                //throw new Exception("move out of bounds");
            }

            return false;
        }
 public override Leaf Remove(CompositePhysicalObject unit)
 {
     if (unitList.Contains(unit))
     {
         leafDictionary.SetLeaf(unit, null);
         unitList.Remove(unit);
         return this;
     }
     return null;
 }
 public Boolean CollidesWith(CompositePhysicalObject other)
 {
     return this.Collidable.CollidesWith(this.WorldPosition(), this.WorldDirection(), other.Collidable, other.WorldPosition(), other.WorldDirection());
 }
 public static void ServerInitialize(CompositePhysicalObject obj, Vector2 position, float direction)
 {
     obj.position.Value = position;
     obj.direction.Value = direction;
 }
 public abstract void Move(CompositePhysicalObject obj);
 public static void ServerInitialize(CompositePhysicalObject obj, Vector2 position, float direction)
 {
     obj.position.Value  = position;
     obj.direction.Value = direction;
 }
        public override Leaf Remove(CompositePhysicalObject obj)
        {
            if (this.Contains(obj.Position))
            {
                if (children.Count != 4)
                {
                    throw new Exception("child");
                }

                Leaf removedFrom = null;
                foreach (Node child in children)
                {
                    removedFrom = child.Remove(obj);
                    if (removedFrom != null)
                    {
                        break;
                    }
                }

                if (removedFrom != null)
                {
                    unitCount--;
                    return removedFrom;
                }
                else
                {
                    return null;
                }
            }
            else
            {
                return null;
            }
        }
 public override void Move(CompositePhysicalObject obj)
 {
     unitCount--;
     if (this.Contains(obj.Position))
     {
         this.Add(obj);
     }
     else
     {
         if (this.Parent != null)
         {
             this.Parent.Move(obj);
         }
         else
         {
             throw new Exception("move out of bounds");
         }
     }
 }
 public abstract Leaf Remove(CompositePhysicalObject unit);
 public Boolean Contains(CompositePhysicalObject obj)
 {
     return this.unitList.Contains(obj);
 }
 public override void Move(CompositePhysicalObject obj)
 {
     if(unitList.Contains(obj))
     {
         if (!this.Contains(obj.Position))
         {
             this.Remove(obj);
             this.Parent.Move(obj);
             if (unitList.Contains(obj))
             {
                 throw new Exception("Move failed");
             }
             if (!this.Parent.IsChild(this))
             {
                 throw new Exception("incorrect child/parent");
             }
             this.Parent.Collapse();
         }
     }
     else
     {
         throw new Exception("No such object");
     }
 }
 public void Move(CompositePhysicalObject obj)
 {
     leafDictionary.GetLeaf(obj).Move(obj);
 }
 public Boolean CollidesWith(CompositePhysicalObject other)
 {
     return(this.Collidable.CollidesWith(this.WorldPosition(), this.WorldDirection(), other.Collidable, other.WorldPosition(), other.WorldDirection()));
 }
 public abstract bool Add(CompositePhysicalObject unit);