MeteorsManager.cs 4.5 KB

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