2
0

ActionScene.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. using System;
  2. using Microsoft.Xna.Framework;
  3. using Microsoft.Xna.Framework.Audio;
  4. using Microsoft.Xna.Framework.Graphics;
  5. using RockRainIphone.Core;
  6. using Microsoft.Xna.Framework.Media;
  7. using Microsoft.Xna.Framework.Input;
  8. namespace RockRainIphone
  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 = true;
  73. MediaPlayer.Play(audio.BackMusic);
  74. meteors.Initialize();
  75. powerSource.PutinStartPosition();
  76. player.Reset();
  77. paused = false;
  78. pausePosition.X = (Game.Window.ClientBounds.Width -
  79. pauseRect.Width)/2;
  80. pausePosition.Y = (Game.Window.ClientBounds.Height -
  81. pauseRect.Height)/2;
  82. gameOver = false;
  83. gameoverPosition.X = (Game.Window.ClientBounds.Width -
  84. gameoverRect.Width)/2;
  85. gameoverPosition.Y = (Game.Window.ClientBounds.Height -
  86. gameoverRect.Height)/2;
  87. // Is a two-player game?
  88. player.Visible = true;
  89. base.Show();
  90. }
  91. /// <summary>
  92. /// Hide the scene
  93. /// </summary>
  94. public override void Hide()
  95. {
  96. // Stop the background music
  97. MediaPlayer.Stop();
  98. base.Hide();
  99. }
  100. /// <summary>
  101. /// True, if the game is in GameOver state
  102. /// </summary>
  103. public bool GameOver
  104. {
  105. get { return gameOver; }
  106. }
  107. /// <summary>
  108. /// Paused mode
  109. /// </summary>
  110. public bool Paused
  111. {
  112. get { return paused; }
  113. set
  114. {
  115. paused = value;
  116. if (paused)
  117. {
  118. //==================================
  119. //AUDIO IS NOT SUPPORTED IN XNATOUCH
  120. //==================================
  121. //MediaPlayer.Pause();
  122. }
  123. else
  124. {
  125. //==================================
  126. //AUDIO IS NOT SUPPORTED IN XNATOUCH
  127. //==================================
  128. //MediaPlayer.Resume();
  129. }
  130. }
  131. }
  132. /// <summary>
  133. /// Handle collisions with a meteor
  134. /// </summary>
  135. private void HandleDamages()
  136. {
  137. // Check Collision for player 1
  138. if (meteors.CheckForCollisions(player.GetBounds()))
  139. {
  140. // Player penalty
  141. player.Power -= 10;
  142. _explosions.AddExplosion(player.Position);
  143. }
  144. }
  145. /// <summary>
  146. /// Handle power-up stuff
  147. /// </summary>
  148. private void HandlePowerSourceSprite(GameTime gameTime)
  149. {
  150. // Player 1 get the power source
  151. if (powerSource.CheckCollision(player.GetBounds()))
  152. {
  153. audio.PowerGet.Play();
  154. elapsedTime = TimeSpan.Zero;
  155. powerSource.PutinStartPosition();
  156. player.Power += 50;
  157. }
  158. // Check for send a new Power source
  159. elapsedTime += gameTime.ElapsedGameTime;
  160. if (elapsedTime > TimeSpan.FromSeconds(15))
  161. {
  162. elapsedTime -= TimeSpan.FromSeconds(15);
  163. powerSource.Enabled = true;
  164. audio.PowerShow.Play();
  165. }
  166. }
  167. /// <summary>
  168. /// Allows the game component to update itself.
  169. /// </summary>
  170. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  171. public override void Update(GameTime gameTime)
  172. {
  173. // Update the explosions
  174. _explosions.Update(gameTime);
  175. if ((!paused) && (!gameOver))
  176. {
  177. // Check collisions with meteors
  178. HandleDamages();
  179. // Check if a player get a power boost
  180. HandlePowerSourceSprite(gameTime);
  181. // Update score
  182. score.Value = player.Score;
  183. score.Power = player.Power;
  184. // Check if player is dead
  185. gameOver = (player.Power <= 0);
  186. if (gameOver)
  187. {
  188. player.Visible = (player.Power > 0);
  189. // Stop the music
  190. MediaPlayer.Stop();
  191. }
  192. // Update all other game components
  193. base.Update(gameTime);
  194. }
  195. // In game over state, keep the meteors animation
  196. if (gameOver)
  197. {
  198. meteors.Update(gameTime);
  199. }
  200. }
  201. /// <summary>
  202. /// Allows the game component to draw itself.
  203. /// </summary>
  204. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  205. public override void Draw(GameTime gameTime)
  206. {
  207. // Draw all game components
  208. base.Draw(gameTime);
  209. if (paused)
  210. {
  211. // Draw the "pause" text
  212. spriteBatch.Draw(actionTexture, pausePosition, pauseRect,
  213. Color.White);
  214. }
  215. if (gameOver)
  216. {
  217. // Draw the "gameover" text
  218. spriteBatch.Draw(actionTexture, gameoverPosition, gameoverRect,
  219. Color.White);
  220. }
  221. }
  222. }
  223. }