ActionScene.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. using System;
  2. using Microsoft.Xna.Framework;
  3. using Microsoft.Xna.Framework.Audio;
  4. using Microsoft.Xna.Framework.Graphics;
  5. using RockRain.Core;
  6. using Microsoft.Xna.Framework.Media;
  7. using Microsoft.Xna.Framework.Input;
  8. namespace RockRain
  9. {
  10. /// <summary>
  11. /// This is a game component that implements the Action Scene.
  12. /// </summary>
  13. public class ActionScene : GameScene
  14. {
  15. // Basics
  16. protected Texture2D actionTexture;
  17. protected SpriteBatch spriteBatch = null;
  18. protected AudioLibrary audio;
  19. // Game Elements
  20. protected Player player;
  21. protected MeteorsManager meteors;
  22. protected PowerSource powerSource;
  23. protected ImageComponent background;
  24. protected Score score;
  25. protected ExplosionManager _explosions;
  26. // Gui Stuff
  27. protected Vector2 pausePosition;
  28. protected Vector2 gameoverPosition;
  29. protected Rectangle pauseRect = new Rectangle(84, 65, 99, 30);
  30. protected Rectangle gameoverRect = new Rectangle(53, 108, 186, 30);
  31. // GameState elements
  32. protected bool paused;
  33. protected bool gameOver;
  34. protected TimeSpan elapsedTime = TimeSpan.Zero;
  35. /// <summary>
  36. /// Default Constructor
  37. /// </summary>
  38. /// <param name="game">The main game object</param>
  39. /// <param name="theTexture">Texture with the sprite elements</param>
  40. /// <param name="backgroundTexture">Texture for the background</param>
  41. /// <param name="font">Font used in the score</param>
  42. public ActionScene(Game game, Texture2D theTexture,
  43. Texture2D backgroundTexture, SpriteFont font, ExplosionManager explosions)
  44. : base(game)
  45. {
  46. // Get the current audiocomponent and play the background music
  47. audio = (AudioLibrary)Game.Services.GetService(typeof(AudioLibrary));
  48. background = new ImageComponent(game, backgroundTexture,
  49. ImageComponent.DrawMode.Stretch);
  50. Components.Add(background);
  51. actionTexture = theTexture;
  52. spriteBatch = (SpriteBatch)
  53. Game.Services.GetService(typeof (SpriteBatch));
  54. meteors = new MeteorsManager(Game, ref actionTexture);
  55. Components.Add(meteors);
  56. player = new Player(Game, ref actionTexture);
  57. player.Initialize();
  58. Components.Add(player);
  59. score = new Score(game, font, Color.LightGray);
  60. score.Position = new Vector2(1, 1);
  61. Components.Add(score);
  62. powerSource = new PowerSource(game, ref actionTexture);
  63. powerSource.Initialize();
  64. Components.Add(powerSource);
  65. _explosions = explosions;
  66. }
  67. /// <summary>
  68. /// Show the action scene
  69. /// </summary>
  70. public override void Show()
  71. {
  72. // GamePad.Visible is not available in MonoGame 3.8.4
  73. // GamePad.Visible = true;
  74. MediaPlayer.Play(audio.BackMusic);
  75. meteors.Initialize();
  76. powerSource.PutinStartPosition();
  77. player.Reset();
  78. paused = false;
  79. pausePosition.X = (Game.Window.ClientBounds.Width -
  80. pauseRect.Width)/2;
  81. pausePosition.Y = (Game.Window.ClientBounds.Height -
  82. pauseRect.Height)/2;
  83. gameOver = false;
  84. gameoverPosition.X = (Game.Window.ClientBounds.Width -
  85. gameoverRect.Width)/2;
  86. gameoverPosition.Y = (Game.Window.ClientBounds.Height -
  87. gameoverRect.Height)/2;
  88. // Is a two-player game?
  89. player.Visible = true;
  90. base.Show();
  91. }
  92. /// <summary>
  93. /// Hide the scene
  94. /// </summary>
  95. public override void Hide()
  96. {
  97. // Stop the background music
  98. MediaPlayer.Stop();
  99. base.Hide();
  100. }
  101. /// <summary>
  102. /// True, if the game is in GameOver state
  103. /// </summary>
  104. public bool GameOver
  105. {
  106. get { return gameOver; }
  107. }
  108. /// <summary>
  109. /// Paused mode
  110. /// </summary>
  111. public bool Paused
  112. {
  113. get { return paused; }
  114. set
  115. {
  116. paused = value;
  117. if (paused)
  118. {
  119. //==================================
  120. //AUDIO IS NOT SUPPORTED IN XNATOUCH
  121. //==================================
  122. //MediaPlayer.Pause();
  123. }
  124. else
  125. {
  126. //==================================
  127. //AUDIO IS NOT SUPPORTED IN XNATOUCH
  128. //==================================
  129. //MediaPlayer.Resume();
  130. }
  131. }
  132. }
  133. /// <summary>
  134. /// Handle collisions with a meteor
  135. /// </summary>
  136. private void HandleDamages()
  137. {
  138. // Check Collision for player 1
  139. if (meteors.CheckForCollisions(player.GetBounds()))
  140. {
  141. // Player penalty
  142. player.Power -= 10;
  143. _explosions.AddExplosion(player.Position);
  144. }
  145. }
  146. /// <summary>
  147. /// Handle power-up stuff
  148. /// </summary>
  149. private void HandlePowerSourceSprite(GameTime gameTime)
  150. {
  151. // Player 1 get the power source
  152. if (powerSource.CheckCollision(player.GetBounds()))
  153. {
  154. audio.PowerGet.Play();
  155. elapsedTime = TimeSpan.Zero;
  156. powerSource.PutinStartPosition();
  157. player.Power += 50;
  158. }
  159. // Check for send a new Power source
  160. elapsedTime += gameTime.ElapsedGameTime;
  161. if (elapsedTime > TimeSpan.FromSeconds(15))
  162. {
  163. elapsedTime -= TimeSpan.FromSeconds(15);
  164. powerSource.Enabled = true;
  165. audio.PowerShow.Play();
  166. }
  167. }
  168. /// <summary>
  169. /// Allows the game component to update itself.
  170. /// </summary>
  171. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  172. public override void Update(GameTime gameTime)
  173. {
  174. // Update the explosions
  175. _explosions.Update(gameTime);
  176. if ((!paused) && (!gameOver))
  177. {
  178. // Check collisions with meteors
  179. HandleDamages();
  180. // Check if a player get a power boost
  181. HandlePowerSourceSprite(gameTime);
  182. // Update score
  183. score.Value = player.Score;
  184. score.Power = player.Power;
  185. // Check if player is dead
  186. gameOver = (player.Power <= 0);
  187. if (gameOver)
  188. {
  189. player.Visible = (player.Power > 0);
  190. // Stop the music
  191. MediaPlayer.Stop();
  192. }
  193. // Update all other game components
  194. base.Update(gameTime);
  195. }
  196. // In game over state, keep the meteors animation
  197. if (gameOver)
  198. {
  199. meteors.Update(gameTime);
  200. }
  201. }
  202. /// <summary>
  203. /// Allows the game component to draw itself.
  204. /// </summary>
  205. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  206. public override void Draw(GameTime gameTime)
  207. {
  208. // Draw all game components
  209. base.Draw(gameTime);
  210. if (paused)
  211. {
  212. // Draw the "pause" text
  213. spriteBatch.Draw(actionTexture, pausePosition, pauseRect,
  214. Color.White);
  215. }
  216. if (gameOver)
  217. {
  218. // Draw the "gameover" text
  219. spriteBatch.Draw(actionTexture, gameoverPosition, gameoverRect,
  220. Color.White);
  221. }
  222. }
  223. }
  224. }