#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using RockRainIphone.Core;
#endregion
namespace RockRainIphone
{
///
/// This game component implements a manager for all Meteors in the game.
///
public class MeteorsManager : DrawableGameComponent
{
// List of active meteors
protected List meteors;
// Constant for initial meteor count
private const int STARTMETEORCOUNT = 5;
// Time for a new meteor
private const int ADDMETEORTIME = 6000;
protected Texture2D meteorTexture;
protected TimeSpan elapsedTime = TimeSpan.Zero;
protected AudioLibrary audio;
public MeteorsManager(Game game, ref Texture2D theTexture)
: base(game)
{
meteorTexture = theTexture;
meteors = new List();
}
///
/// Allows the game component to perform any initialization it needs to
/// before starting to run. This is where it can query for any required
/// services and load content.
///
public override void Initialize()
{
// Get the current audiocomponent and play the background music
audio = (AudioLibrary)Game.Services.GetService(typeof(AudioLibrary));
meteors.Clear();
Start();
for (int i = 0; i < meteors.Count; i++)
{
meteors[i].Initialize();
}
base.Initialize();
}
///
/// Start the Meteors Rain
///
public void Start()
{
// Initialize a counter
elapsedTime = TimeSpan.Zero;
// Add the meteors
for (int i = 0; i < STARTMETEORCOUNT; i++)
{
AddNewMeteor();
}
}
///
/// All Meteors in the game
///
public List AllMeteors
{
get { return meteors; }
}
///
/// Check if is a moment for a new meteor
///
private void CheckforNewMeteor(GameTime gameTime)
{
// Add a rock each ADDMETEORTIME
elapsedTime += gameTime.ElapsedGameTime;
if (elapsedTime > TimeSpan.FromMilliseconds(ADDMETEORTIME))
{
elapsedTime -= TimeSpan.FromMilliseconds(ADDMETEORTIME);
AddNewMeteor();
// Play a sound for a new meteor
audio.NewMeteor.Play();
}
}
///
/// Add a new meteor in the scene
///
private Meteor AddNewMeteor()
{
Meteor newMeteor = new Meteor(Game, ref meteorTexture);
newMeteor.Initialize();
meteors.Add(newMeteor);
// Set the meteor identifier
newMeteor.Index = meteors.Count - 1;
return newMeteor;
}
///
/// Allows the game component to update itself.
///
/// Provides a snapshot of timing values.
public override void Update(GameTime gameTime)
{
CheckforNewMeteor(gameTime);
// Update Meteors
for (int i = 0; i < meteors.Count; i++)
{
meteors[i].Update(gameTime);
}
base.Update(gameTime);
}
///
/// Check if the ship collide with a meteor
/// true, if has a collision
///
public bool CheckForCollisions(Rectangle rect)
{
for (int i = 0; i < meteors.Count; i++)
{
if (meteors[i].CheckCollision(rect))
{
// BOM !!
audio.Explosion.Play();
// Put the meteor back to your initial position
meteors[i].PutinStartPosition();
return true;
}
}
return false;
}
///
/// Allows the game component draw your content in game screen
///
public override void Draw(GameTime gameTime)
{
// Draw the meteors
for (int i = 0; i < meteors.Count; i++)
{
meteors[i].Draw(gameTime);
}
base.Draw(gameTime);
}
}
}