/// <summary>
        /// Uppdaterar objekt samt hanterar nya objekt samt döda objekt
        /// </summary>
        /// <param name="gameTime"></param>
        public static void Update(GameTime gameTime)
        {
            foreach (BaseObject obj in Allobjects)
            {
                if (!obj.dead)
                {
                    obj.Update(gameTime);
                }
                else
                {
                    ObjectsToRemove.Add(obj);
                }
            }

            for (int i = 0; i < collidableObjects.Count; i++)
            {
                ICollision colObj1 = collidableObjects[i];

                for (int j = i + 1; j < collidableObjects.Count; j++)
                {
                    ICollision colObj2 = collidableObjects[j];
                    if (colObj1.CollisionBox.Intersects(colObj2.CollisionBox))
                    {
                        colObj1.Collision(colObj2 as BaseObject);
                        colObj2.Collision(colObj1 as BaseObject);
                    }
                }
            }

            foreach (BaseObject obj in ObjectsToAdd)
            {
                Allobjects.Add(obj);
                if (obj is ICollision)
                {
                    collidableObjects.Add(obj as ICollision);
                }
            }

            ObjectsToAdd.Clear();

            foreach (BaseObject obj in ObjectsToRemove)
            {
                Allobjects.Remove(obj);
                if (obj is ICollision)
                {
                    collidableObjects.Remove(obj as ICollision);
                }
            }

            ObjectsToRemove.Clear();
        }
Beispiel #2
0
 //Kollar vilka objekt som kolliderar.
 //Om dom kolliderar säger vi till objekten vilket objekt dom kolliderar med
 private static void Collision()
 {
     for (int i = 0; i < collidableObjects.Count; i++)
     {
         ICollision colObj1 = collidableObjects[i];
         for (int j = i + 1; j < collidableObjects.Count; j++)
         {
             ICollision colObj2 = collidableObjects[j];
             if (colObj1.CollisionBox.Intersects(colObj2.CollisionBox) && (!colObj1.Remove && !colObj2.Remove))
             {
                 colObj1.Collision(colObj2 as BaseObject);
                 colObj2.Collision(colObj1 as BaseObject);
             }
         }
         if (colObj1 is BaseBullet)
         {
             if (Vector2.Distance(colObj1.CollisionBox.Location.ToVector2(), PlayerPosition) >= bulletRemoveDistance)
             {
                 colObj1.Remove = true;
             }
         }
     }
 }