Ejemplo n.º 1
0
        public void Collision()
        {
            for (int i = 0; i < platforms.Count; i++)
            {
                if (player.Rectangle.Intersects(platforms[i].Rectangle) && oldPlayerPos.X + player.Rectangle.Width < platforms[i].Position.X) // Player collision with left wall
                {
                    player.WallCollision();
                    player.Position = new Vector2(platforms[i].Position.X - player.Rectangle.Width - 1, player.Position.Y);
                } // Platform & Player collision

                else if (player.Rectangle.Intersects(platforms[i].Rectangle) && oldPlayerPos.X > platforms[i].Position.X + platforms[i].Rectangle.Width) // Player collision with right wall
                {
                    player.WallCollision();
                    player.Position = new Vector2(platforms[i].Position.X + platforms[i].Rectangle.Width + 1, player.Position.Y);
                } // Platform & Player collision

                else if (player.Rectangle.Intersects(platforms[i].Rectangle) && oldPlayerPos.Y < platforms[i].Position.Y) // Player collision with floor
                {
                    player.FloorCollision();
                    player.Position = new Vector2(player.Position.X, platforms[i].Position.Y - player.Rectangle.Height + 1);
                } // Platform & Player collision

                else if (player.Rectangle.Intersects(platforms[i].Rectangle) && oldPlayerPos.Y + player.Rectangle.Height > platforms[i].Position.Y + platforms[i].Rectangle.Height) // Player collision with ceiling
                {
                    player.RoofCollision();
                    player.Position = new Vector2(player.Position.X, platforms[i].Position.Y + platforms[i].Rectangle.Height);
                } // Platform & Player collision

                for (int j = 0; j < bullets.Count; j++)
                {
                    if (platforms[i].Rectangle.Intersects(bullets[j].Rectangle)) // Platform & Bullet collision
                    {
                        bullets.RemoveAt(j);
                        j--;
                    }
                }
            }

            for (int i = 0; i < enemies.Count; i++)
            {
                if (enemies[i].Rectangle.Intersects(player.Rectangle)) // Enemy & Player collision
                {
                    Exit();
                }

                for (int j = 0; j < bullets.Count; j++)
                {
                    if (enemies[i].Rectangle.Intersects(bullets[j].Rectangle)) // Enemy & Bullet collision
                    {
                        bullets.RemoveAt(j);
                        j--;

                        enemies[i].Health -= gun.Damage;
                        if (enemies[i].Health <= 0)
                        {
                            enemies.RemoveAt(i);
                            healthBars.RemoveAt(i);
                            i--;
                        }
                    }
                }
            }
        }