/// <summary> /// Create an asteroid /// </summary> /// <param name="content"></param> /// <param name="graphicsDevice"></param> /// <returns></returns> private Asteroid CreateAsteroid(ContentManager content, GraphicsDevice graphicsDevice) { /// Create an asteroid var asteroid = new Asteroid(this, player); asteroid.Initialize(content, graphicsDevice); /// Initilizes asteroid default values float speed = 1.0f; float x = 0.0f; float y = 0.0f; Vector2 direction = Vector2.Zero; var corner = random.Next(0, 3); /// Determine what direction the asteroid comes from. switch (corner) { case 0: x = graphicsDevice.Viewport.X + asteroid.Width / 2; y = graphicsDevice.Viewport.Y + asteroid.Height / 2; asteroid.CurrentSpeed = new Vector2(1.0f * speed, 1.0f * speed); break; case 1: x = graphicsDevice.Viewport.Width - asteroid.Width / 2; y = graphicsDevice.Viewport.Y + asteroid.Height / 2; asteroid.CurrentSpeed = new Vector2(-1.0f * speed, 1.0f * speed); break; case 2: x = graphicsDevice.Viewport.X + asteroid.Width / 2; y = graphicsDevice.Viewport.Height - asteroid.Height / 2; asteroid.CurrentSpeed = new Vector2(1.0f * speed, -1.0f * speed); break; case 3: x = graphicsDevice.Viewport.Width - asteroid.Width / 2; y = graphicsDevice.Viewport.Height - asteroid.Height / 2; asteroid.CurrentSpeed = new Vector2(-1.0f * speed, -1.0f * speed); break; default: break; } asteroid.Position = new Vector2(x, y); asteroids.Add(asteroid); return asteroid; }
/// <summary> /// Remove dead asteroids from the map /// </summary> /// <param name="content">The content manager</param> /// <param name="graphicsDevice">The graphics device</param> /// <param name="player">The player</param> private void RemoveDeadAsteroids(ContentManager content, GraphicsDevice graphicsDevice, Player player) { /// Remove dead asteroids for (int i = asteroids.Count - 1; i >= 0; i--) { var asteroid = asteroids[i]; if (!(asteroid.Killer is EnemyBullet) && asteroid.Dead) { /// Add to the player's score player.Score += asteroid.ScoreWorth; } if (asteroid.Dead) { var nextSize = (asteroid.Size == Sizes.Large) ? Sizes.Medium : Sizes.Small; var explosion = CreateExplosion(nextSize, asteroid.Position, content, graphicsDevice); entityManager.Add(explosion); explosion.PlayExplosionSound(); /// Split the asteroid into smaller asteroids. var splitAsteroids = new List<Asteroid>(); if (asteroid.Size != Sizes.Small) { var a1 = new Asteroid(this, player, nextSize); var a2 = new Asteroid(this, player, nextSize); a1.Position = new Vector2(asteroid.Position.X, asteroid.Position.Y + 15); a1.CurrentSpeed = asteroid.CurrentSpeed; a2.Position = asteroid.Position; a2.CurrentSpeed = new Vector2(-asteroid.CurrentSpeed.X, -asteroid.CurrentSpeed.Y); splitAsteroids.AddRange(new Asteroid[] { a1, a2 }); } /// Remove the asteroid Remove(asteroid); foreach (var newAsteroid in splitAsteroids) { entityManager.Add(newAsteroid); asteroids.Add(newAsteroid); } } } }
/// <summary> /// Remove an asteroid from the asteroid manager /// </summary> /// <param name="asteroid"></param> public void Remove(Asteroid asteroid) { asteroids.Remove(asteroid); freshAsteroids.Remove(asteroid); entityManager.Remove(asteroid); }