MeteorsManager.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #region Using Statements
  2. using System;
  3. using System.Collections.Generic;
  4. using Microsoft.Xna.Framework;
  5. using Microsoft.Xna.Framework.Graphics;
  6. using RockRainIphone.Core;
  7. #endregion
  8. namespace RockRainIphone
  9. {
  10. /// <summary>
  11. /// This game component implements a manager for all Meteors in the game.
  12. /// </summary>
  13. public class MeteorsManager : DrawableGameComponent
  14. {
  15. // List of active meteors
  16. protected List<Meteor> meteors;
  17. // Constant for initial meteor count
  18. private const int STARTMETEORCOUNT = 5;
  19. // Time for a new meteor
  20. private const int ADDMETEORTIME = 6000;
  21. protected Texture2D meteorTexture;
  22. protected TimeSpan elapsedTime = TimeSpan.Zero;
  23. protected AudioLibrary audio;
  24. public MeteorsManager(Game game, ref Texture2D theTexture)
  25. : base(game)
  26. {
  27. meteorTexture = theTexture;
  28. meteors = new List<Meteor>();
  29. }
  30. /// <summary>
  31. /// Allows the game component to perform any initialization it needs to
  32. /// before starting to run. This is where it can query for any required
  33. /// services and load content.
  34. /// </summary>
  35. public override void Initialize()
  36. {
  37. // Get the current audiocomponent and play the background music
  38. audio = (AudioLibrary)Game.Services.GetService(typeof(AudioLibrary));
  39. meteors.Clear();
  40. Start();
  41. for (int i = 0; i < meteors.Count; i++)
  42. {
  43. meteors[i].Initialize();
  44. }
  45. base.Initialize();
  46. }
  47. /// <summary>
  48. /// Start the Meteors Rain
  49. /// </summary>
  50. public void Start()
  51. {
  52. // Initialize a counter
  53. elapsedTime = TimeSpan.Zero;
  54. // Add the meteors
  55. for (int i = 0; i < STARTMETEORCOUNT; i++)
  56. {
  57. AddNewMeteor();
  58. }
  59. }
  60. /// <summary>
  61. /// All Meteors in the game
  62. /// </summary>
  63. public List<Meteor> AllMeteors
  64. {
  65. get { return meteors; }
  66. }
  67. /// <summary>
  68. /// Check if is a moment for a new meteor
  69. /// </summary>
  70. private void CheckforNewMeteor(GameTime gameTime)
  71. {
  72. // Add a rock each ADDMETEORTIME
  73. elapsedTime += gameTime.ElapsedGameTime;
  74. if (elapsedTime > TimeSpan.FromMilliseconds(ADDMETEORTIME))
  75. {
  76. elapsedTime -= TimeSpan.FromMilliseconds(ADDMETEORTIME);
  77. AddNewMeteor();
  78. // Play a sound for a new meteor
  79. audio.NewMeteor.Play();
  80. }
  81. }
  82. /// <summary>
  83. /// Add a new meteor in the scene
  84. /// </summary>
  85. private Meteor AddNewMeteor()
  86. {
  87. Meteor newMeteor = new Meteor(Game, ref meteorTexture);
  88. newMeteor.Initialize();
  89. meteors.Add(newMeteor);
  90. // Set the meteor identifier
  91. newMeteor.Index = meteors.Count - 1;
  92. return newMeteor;
  93. }
  94. /// <summary>
  95. /// Allows the game component to update itself.
  96. /// </summary>
  97. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  98. public override void Update(GameTime gameTime)
  99. {
  100. CheckforNewMeteor(gameTime);
  101. // Update Meteors
  102. for (int i = 0; i < meteors.Count; i++)
  103. {
  104. meteors[i].Update(gameTime);
  105. }
  106. base.Update(gameTime);
  107. }
  108. /// <summary>
  109. /// Check if the ship collide with a meteor
  110. /// <returns>true, if has a collision</returns>
  111. /// </summary>
  112. public bool CheckForCollisions(Rectangle rect)
  113. {
  114. for (int i = 0; i < meteors.Count; i++)
  115. {
  116. if (meteors[i].CheckCollision(rect))
  117. {
  118. // BOM !!
  119. audio.Explosion.Play();
  120. // Put the meteor back to your initial position
  121. meteors[i].PutinStartPosition();
  122. return true;
  123. }
  124. }
  125. return false;
  126. }
  127. /// <summary>
  128. /// Allows the game component draw your content in game screen
  129. /// </summary>
  130. public override void Draw(GameTime gameTime)
  131. {
  132. // Draw the meteors
  133. for (int i = 0; i < meteors.Count; i++)
  134. {
  135. meteors[i].Draw(gameTime);
  136. }
  137. base.Draw(gameTime);
  138. }
  139. }
  140. }