Game1.cs 8.3 KB

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