public List <ViewGeometry> OnCheckCollision()
        {
            List <ViewGeometry> collisions = new List <ViewGeometry>();

            foreach (var collision in CollisionMap.Where(p => Name != p.Name).ToDictionary(p => p.Geometry, o => o))
            {
                bool isCollision = CollisionManager.CalculateCollision(Geometry, collision.Key);
                if (isCollision)
                {
                    collisions.Add(collision.Value);
                }
            }

            foreach (var item in collisions)
            {
                if (!CurrentCollisions.Any(p => p == item))
                {
                    CurrentCollisions.Add(item);
                }
                if (!item.CurrentCollisions.Any(p => p == this))
                {
                    item.CurrentCollisions.Add(this);
                }

                if (item.CheckCollision != null)
                {
                    item.CheckCollision(item.CurrentCollisions);
                }
            }
            foreach (var item in PreviousCollisions)
            {
                if (!collisions.Any(p => p == item))
                {
                    CurrentCollisions.Remove(item);
                    item.CurrentCollisions.Remove(this);
                    if (item.CheckCollision != null)
                    {
                        item.CheckCollision(item.CurrentCollisions);
                    }
                }
            }
            PreviousCollisions = CurrentCollisions.ToList();

            if (CheckCollision != null)
            {
                CheckCollision(CurrentCollisions);
            }
            return(collisions);
        }
Esempio n. 2
0
        /// <summary>
        /// Called every frame by the CollidableObject, which is called every frame by the Game1 class.
        /// Updates a Rectangle at the location of the Host GameObject each frame.
        /// </summary>
        /// <param name="gameTime"></param>
        public void Update(GameTime gameTime)
        {
            //Don't bother checking collisions if not active
            if (!Host.Active)
            {
                return;
            }

            UpdateHitbox();

            if (!ChecksCollisions)
            {
                return;
            }

            foreach (CollidableObject c in GameManager.CollidableObjectsInView)
            {
                if (!c.Active)
                {
                    continue;
                }
                //Don't check collisions with yourself!
                if (c.Name == Host.Name)
                {
                    continue;
                }
                //If we intersect with a new object
                if (Hitbox.Intersects(c.Coll.Hitbox) && !CurrentCollisions.Contains(c.Coll))
                {
                    CurrentCollisions.Add(c.Coll);
                    OnCollision(c.Coll);
                }
                //If stop intersecting with an object we just were intersecting with
                if (!Hitbox.Intersects(c.Coll.Hitbox) && CurrentCollisions.Contains(c.Coll))
                {
                    OnCollisionExit(c.Coll);
                    CurrentCollisions.Remove(c.Coll);
                }
            }
        }