RockRainGame.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. using System;
  2. using Microsoft.Xna.Framework;
  3. using Microsoft.Xna.Framework.Graphics;
  4. using Microsoft.Xna.Framework.Input;
  5. using RockRain.Core;
  6. namespace RockRain
  7. {
  8. /// <summary>
  9. /// This is the main type for your game
  10. /// </summary>
  11. public class RockRainGame : Game
  12. {
  13. private readonly GraphicsDeviceManager graphics;
  14. private SpriteBatch spriteBatch;
  15. private ExplosionManager explosions;
  16. // Textures
  17. protected Texture2D gamepadTexture,helpBackgroundTexture;
  18. protected Texture2D startBackgroundTexture, startElementsTexture;
  19. protected Texture2D actionElementsTexture, actionBackgroundTexture;
  20. // Game Scenes
  21. protected HelpScene helpScene;
  22. protected StartScene startScene;
  23. protected ActionScene actionScene;
  24. protected GameScene activeScene;
  25. // Audio Stuff
  26. private AudioLibrary audio;
  27. // Fonts
  28. private SpriteFont smallFont, largeFont, scoreFont;
  29. // Used for handle input
  30. protected GamePadState oldGamePadState;
  31. public RockRainGame()
  32. {
  33. graphics = new GraphicsDeviceManager(this);
  34. Content.RootDirectory = "Content";
  35. // Used for input handling
  36. oldGamePadState = GamePad.GetState(PlayerIndex.One);
  37. }
  38. /// <summary>
  39. /// LoadContent will be called once per game and is the place to load
  40. /// all of your content.
  41. /// </summary>
  42. protected override void LoadContent()
  43. {
  44. // Create a new SpriteBatch, which can be used to draw textures.
  45. spriteBatch = new SpriteBatch(GraphicsDevice);
  46. // TODO: use this.Content to load your game content here
  47. Services.AddService(typeof(SpriteBatch), spriteBatch);
  48. // Setup virtual gamepad - not available in MonoGame 3.8.4
  49. /*
  50. gamepadTexture = Content.Load<Texture2D>("gamepad.png");
  51. ButtonDefinition BButton = new ButtonDefinition();
  52. BButton.Texture = gamepadTexture;
  53. BButton.Position = new Vector2(100,420);
  54. BButton.Type = Buttons.Back;
  55. BButton.TextureRect = new Rectangle(72,77,36,36);
  56. ButtonDefinition AButton = new ButtonDefinition();
  57. AButton.Texture = gamepadTexture;
  58. AButton.Position = new Vector2(150,420);
  59. AButton.Type = Buttons.A;
  60. AButton.TextureRect = new Rectangle(73,114,36,36);
  61. GamePad.ButtonsDefinitions.Add(BButton);
  62. GamePad.ButtonsDefinitions.Add(AButton);
  63. ThumbStickDefinition thumbStick = new ThumbStickDefinition();
  64. thumbStick.Position = new Vector2(220,400);
  65. thumbStick.Texture = gamepadTexture;
  66. thumbStick.TextureRect = new Rectangle(2,2,68,68);
  67. GamePad.LeftThumbStickDefinition = thumbStick;
  68. */
  69. // Create the audio bank
  70. audio = new AudioLibrary(Content);
  71. Services.AddService(typeof(AudioLibrary), audio);
  72. // Create the Start Scene
  73. // Note: XNB files are incompatible with MonoGame 3.8.4
  74. // Commenting out font loading for compatibility
  75. // smallFont = Content.Load<SpriteFont>("menuSmall");
  76. // largeFont = Content.Load<SpriteFont>("menuLarge");
  77. startElementsTexture = Content.Load<Texture2D>("startsceneelements");
  78. startBackgroundTexture = Content.Load<Texture2D>("startbackground");
  79. startScene = new StartScene(this, null, null, startBackgroundTexture, startElementsTexture);
  80. Components.Add(startScene);
  81. // Start the game in the start Scene :)
  82. startScene.Show();
  83. activeScene = startScene;
  84. }
  85. private void CreateActionScene()
  86. {
  87. explosions = new ExplosionManager(this);
  88. actionElementsTexture = Content.Load<Texture2D>("rockrainenhanced.png");
  89. actionBackgroundTexture = Content.Load<Texture2D>("spacebackground.jpg");
  90. // Note: XNB files are incompatible with MonoGame 3.8.4
  91. // scoreFont = Content.Load<SpriteFont>("score");
  92. actionScene = new ActionScene(this, actionElementsTexture,actionBackgroundTexture, null, explosions);
  93. Components.Add(actionScene);
  94. }
  95. private void CreateHelpScene()
  96. {
  97. helpBackgroundTexture = Content.Load<Texture2D>("helpscreen.png");
  98. helpScene = new HelpScene(this, helpBackgroundTexture);
  99. Components.Add(helpScene);
  100. }
  101. /// <summary>
  102. /// Open a new scene
  103. /// </summary>
  104. /// <param name="scene">Scene to be opened</param>
  105. protected void ShowScene(GameScene scene)
  106. {
  107. activeScene.Hide();
  108. activeScene = scene;
  109. scene.Show();
  110. }
  111. /// <summary>
  112. /// Allows the game to run logic such as updating the world,
  113. /// checking for collisions, gathering input, and playing audio.
  114. /// </summary>
  115. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  116. protected override void Update(GameTime gameTime)
  117. {
  118. // Handle Game Inputs
  119. HandleScenesInput();
  120. base.Update(gameTime);
  121. }
  122. /// <summary>
  123. /// Handle input of all game scenes
  124. /// </summary>
  125. private void HandleScenesInput()
  126. {
  127. // Handle Start Scene Input
  128. if (activeScene == startScene)
  129. {
  130. HandleStartSceneInput();
  131. }
  132. // Handle Help Scene input
  133. else if (activeScene == helpScene)
  134. {
  135. if ((Mouse.GetState().X != 0) || (Mouse.GetState().Y != 0))
  136. {
  137. ShowScene(startScene);
  138. }
  139. }
  140. // Handle Action Scene Input
  141. else if (activeScene == actionScene)
  142. {
  143. HandleActionInput();
  144. }
  145. }
  146. /// <summary>
  147. /// Check if the Enter Key ou 'A' button was pressed
  148. /// </summary>
  149. /// <returns>true, if enter key ou 'A' button was pressed</returns>
  150. private void HandleActionInput()
  151. {
  152. // Get the Keyboard and GamePad state
  153. GamePadState gamepadState = GamePad.GetState(PlayerIndex.One);
  154. bool backKey = (oldGamePadState.Buttons.Back == ButtonState.Pressed) &&
  155. (gamepadState.Buttons.Back == ButtonState.Released);
  156. bool enterKey = (oldGamePadState.Buttons.A == ButtonState.Pressed) &&
  157. (gamepadState.Buttons.A == ButtonState.Released);
  158. oldGamePadState = gamepadState;
  159. if (enterKey)
  160. {
  161. if (actionScene.GameOver)
  162. {
  163. ShowScene(startScene);
  164. }
  165. else
  166. {
  167. audio.MenuBack.Play();
  168. actionScene.Paused = !actionScene.Paused;
  169. }
  170. }
  171. if (backKey)
  172. {
  173. ShowScene(startScene);
  174. }
  175. }
  176. /// <summary>
  177. /// Handle buttons and keyboard in StartScene
  178. /// </summary>
  179. private void HandleStartSceneInput()
  180. {
  181. if (startScene.MenuSelected)
  182. {
  183. //Mouse.SetPosition(0,0);
  184. audio.MenuSelect.Play();
  185. switch (startScene.SelectedMenuIndex)
  186. {
  187. case 0:
  188. if (actionScene == null)
  189. CreateActionScene();
  190. ShowScene(actionScene);
  191. break;
  192. case 1:
  193. if (helpScene == null)
  194. CreateHelpScene();
  195. ShowScene(helpScene);
  196. break;
  197. case 2:
  198. Exit();
  199. break;
  200. }
  201. }
  202. }
  203. /// <summary>
  204. /// This is called when the game should draw itself.
  205. /// </summary>
  206. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  207. protected override void Draw(GameTime gameTime)
  208. {
  209. graphics.GraphicsDevice.Clear(Color.CornflowerBlue);
  210. // Begin..
  211. spriteBatch.Begin();
  212. // Draw all Game Components..
  213. base.Draw(gameTime);
  214. // GamePad.Draw is not available in MonoGame 3.8.4
  215. // GamePad.Draw(gameTime,spriteBatch);
  216. // End.
  217. spriteBatch.End();
  218. // Draw particles
  219. if (activeScene == actionScene)
  220. explosions.Draw(gameTime);
  221. }
  222. }
  223. }