MarbletsGame.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // MarbletsGame.cs
  4. //
  5. // Microsoft XNA Community Game Platform
  6. // Copyright (C) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #endregion
  9. #region Using Statements
  10. using System;
  11. using System.Collections.Generic;
  12. using System.IO;
  13. using System.Xml.Serialization;
  14. #if ANDROID
  15. using Android.App;
  16. #endif
  17. using Microsoft.Xna.Framework;
  18. using Microsoft.Xna.Framework.Audio;
  19. using Microsoft.Xna.Framework.Graphics;
  20. using Microsoft.Xna.Framework.Input;
  21. using Microsoft.Xna.Framework.Content;
  22. #endregion
  23. namespace Marblets
  24. {
  25. /// <summary>
  26. /// This is the main type for your game
  27. /// </summary>
  28. partial class MarbletsGame : Game
  29. {
  30. /// <summary>
  31. /// A cache of content used by the game
  32. /// </summary>
  33. public new static ContentManager Content;
  34. /// <summary>
  35. /// The game settings from settings.xml
  36. /// </summary>
  37. public static Settings Settings = new Settings();
  38. /// <summary>
  39. /// The current game state
  40. /// </summary>
  41. public static GameState GameState = GameState.Started;
  42. /// <summary>
  43. /// The new game state
  44. /// </summary>
  45. public static GameState NextGameState = GameState.None;
  46. public static int Score; //= 0;
  47. public static List<int> HighScores;
  48. private GraphicsDeviceManager graphics;
  49. private GameScreen mainGame;
  50. private TitleScreen splashScreen;
  51. private InputHelper inputHelper;
  52. private KeyboardState keyState;
  53. #if ANDROID
  54. public MarbletsGame (Activity activity) : base (activity)
  55. #else
  56. public MarbletsGame()
  57. #endif
  58. {
  59. //Create the content pipeline manager.
  60. base.Content.RootDirectory = "Content";
  61. MarbletsGame.Content = base.Content;
  62. //Set up the device to be HD res. The RelativeSpriteBatch will handle
  63. //resizing for us
  64. graphics = new GraphicsDeviceManager(this);
  65. // If the window size changes, then our drawable area changes and the
  66. // game graphics might not fit.
  67. // Hook into DeviceReset event so we can resize the graphics.
  68. graphics.DeviceReset += new EventHandler<EventArgs>(OnGraphicsComponentDeviceReset);
  69. graphics.PreferredBackBufferWidth = 320;
  70. graphics.PreferredBackBufferHeight = 480;
  71. Window.AllowUserResizing = true;
  72. mainGame =
  73. new GameScreen(this, "Textures/play_frame", SoundEntry.MusicGame);
  74. mainGame.Enabled = false;
  75. mainGame.Visible = false;
  76. this.Components.Add(mainGame);
  77. splashScreen =
  78. new TitleScreen(this, "Textures/title_frame", SoundEntry.MusicTitle);
  79. splashScreen.Enabled = true;
  80. splashScreen.Visible = true;
  81. this.Components.Add(splashScreen);
  82. inputHelper = new InputHelper(this);
  83. inputHelper.UpdateOrder = int.MinValue;
  84. this.Components.Add(inputHelper);
  85. }
  86. /// <summary>
  87. /// Allows the game to perform any initialization it needs to before starting to
  88. /// run. This is where it can query for any required services and load any
  89. /// non-graphic related content. Calling base.Initialize will enumerate through
  90. /// any components and initialize them as well.
  91. /// </summary>
  92. protected override void Initialize()
  93. {
  94. //This will call initialize on all the game components
  95. base.Initialize();
  96. // create initial high scores
  97. HighScores = new List<int>();
  98. for(int i = 0; i < 5; i++)
  99. {
  100. HighScores.Add(500 - i * 100);
  101. }
  102. }
  103. /// <summary>
  104. /// Load your graphics content.
  105. /// </summary>
  106. protected override void LoadContent()
  107. {
  108. Sound.LoadContent(Content);
  109. IGraphicsDeviceService graphicsService =
  110. (IGraphicsDeviceService)Services.GetService(
  111. typeof(IGraphicsDeviceService));
  112. //Ask static helper objects to reload too
  113. Font.LoadContent();
  114. }
  115. /// <summary>
  116. /// Allows the game to run logic such as updating the world,
  117. /// checking for collisions, gathering input and playing audio.
  118. /// </summary>
  119. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  120. protected override void Update(GameTime gameTime)
  121. {
  122. base.Update(gameTime);
  123. //Handle FullScreen
  124. keyState = Keyboard.GetState();
  125. // Allows the default game to exit on Xbox 360 and Windows
  126. if(GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed ||
  127. AltComboPressed(keyState, Keys.F4) ||
  128. keyState.IsKeyDown(Keys.Escape))
  129. {
  130. this.Exit();
  131. }
  132. if(InputHelper.GamePads[PlayerIndex.One].StartPressed)
  133. {
  134. NextGameState = GameState.Started;
  135. }
  136. if(NextGameState != GameState.None)
  137. {
  138. ChangeGameState();
  139. }
  140. }
  141. /// <summary>
  142. /// This is called when the game should draw itself.
  143. /// </summary>
  144. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  145. protected override void Draw(GameTime gameTime)
  146. {
  147. graphics.GraphicsDevice.Clear(Color.Black);
  148. //Nothing to draw the components will handle it
  149. base.Draw(gameTime);
  150. }
  151. private void ChangeGameState()
  152. {
  153. //Marblets only has 3 game states - the splash screen and the game screen
  154. //in 2d and 3d Since they are both game components of type screen just
  155. //making the right one visible will cause the correct background to be shown
  156. //and the right music to be played
  157. if((GameState == GameState.Started) && (NextGameState == GameState.Play2D))
  158. {
  159. Score = 0;
  160. splashScreen.Enabled = false;
  161. splashScreen.Visible = false;
  162. mainGame.Enabled = true;
  163. mainGame.Visible = true;
  164. //Start a new game - reset score and board etc
  165. mainGame.NewGame();
  166. GameState = NextGameState;
  167. }
  168. else if(NextGameState == GameState.Started)
  169. {
  170. splashScreen.Enabled = true;
  171. splashScreen.Visible = true;
  172. mainGame.Enabled = false;
  173. mainGame.Visible = false;
  174. GameState = NextGameState;
  175. }
  176. }
  177. /* TODO protected override void OnExiting(object sender, EventArgs args)
  178. {
  179. splashScreen.Shutdown();
  180. mainGame.Shutdown();
  181. base.OnExiting(sender, args);
  182. } */
  183. /// <summary>
  184. /// Checks whether an alt+key combo is pressed.
  185. /// </summary>
  186. private static bool AltComboPressed(KeyboardState state, Keys key)
  187. {
  188. return state.IsKeyDown(key) &&
  189. (state.IsKeyDown(Keys.LeftAlt) ||
  190. state.IsKeyDown(Keys.RightAlt));
  191. }
  192. /// <summary>
  193. /// Resize the game graphics when the window size changes
  194. /// </summary>
  195. void OnGraphicsComponentDeviceReset(object sender, EventArgs e)
  196. {
  197. mainGame.SpriteBatch.Resize();
  198. }
  199. }
  200. /// <summary>
  201. /// This enum is for the state transitions.
  202. /// </summary>
  203. public enum GameState
  204. {
  205. /// <summary>
  206. /// Default value - means no state is set
  207. /// </summary>
  208. None,
  209. /// <summary>
  210. /// Nothing visible, game has just been run and nothing is initialized
  211. /// </summary>
  212. Started,
  213. /// <summary>
  214. /// Logo Screen is being displayed
  215. /// </summary>
  216. LogoSplash,
  217. /// <summary>
  218. /// Currently playing the 2d version
  219. /// </summary>
  220. Play2D,
  221. }
  222. }