2
0

LevelOverScreen.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // BackgroundScreen.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 Microsoft.Xna.Framework;
  12. using Microsoft.Xna.Framework.Graphics;
  13. using Microsoft.Xna.Framework.Input.Touch;
  14. using System.Threading;
  15. using Microsoft.Xna.Framework.Input;
  16. using Microsoft.Xna.Framework.GamerServices;
  17. #endregion
  18. namespace HoneycombRush
  19. {
  20. class LevelOverScreen : GameScreen
  21. {
  22. #region Fields
  23. SpriteFont font36px;
  24. SpriteFont font16px;
  25. Rectangle safeArea;
  26. string text;
  27. bool isLoading;
  28. Vector2 textSize;
  29. DifficultyMode? difficultyMode;
  30. Thread thread;
  31. GameplayScreen gameplayScreen;
  32. bool assetsLoaded = false;
  33. #endregion
  34. #region Initialization
  35. /// <summary>
  36. /// Ctor.
  37. /// </summary>
  38. /// <param name="text">The text to display</param>
  39. /// <param name="difficultyMode">The next level</param>
  40. public LevelOverScreen(string text, DifficultyMode? difficultyMode)
  41. {
  42. this.text = text;
  43. EnabledGestures = GestureType.Tap;
  44. this.difficultyMode = difficultyMode;
  45. }
  46. /// <summary>
  47. /// Load screen resources
  48. /// </summary>
  49. public override void LoadContent()
  50. {
  51. if (difficultyMode.HasValue)
  52. {
  53. gameplayScreen = new GameplayScreen(difficultyMode.Value);
  54. gameplayScreen.ScreenManager = ScreenManager;
  55. }
  56. font36px = ScreenManager.Game.Content.Load<SpriteFont>("Fonts/GameScreenFont36px");
  57. font16px = ScreenManager.Game.Content.Load<SpriteFont>("Fonts/GameScreenFont16px");
  58. textSize = font36px.MeasureString(text);
  59. safeArea = SafeArea;
  60. base.LoadContent();
  61. }
  62. #endregion
  63. #region Update
  64. /// <summary>
  65. /// Update the screen
  66. /// </summary>
  67. /// <param name="gameTime">Game time information.</param>
  68. /// <param name="otherScreenHasFocus">Whether another screen has the focus.</param>
  69. /// <param name="coveredByOtherScreen">Whether this screen is covered by another.</param>
  70. public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
  71. {
  72. // If null don't do anything
  73. if (null != thread)
  74. {
  75. // If we finishedloading the assets, add the game play screen
  76. if (thread.ThreadState == ThreadState.Stopped)
  77. {
  78. // Exit all the screen
  79. foreach (GameScreen screen in ScreenManager.GetScreens())
  80. {
  81. screen.ExitScreen();
  82. }
  83. // Add the gameplay screen
  84. if (difficultyMode.HasValue)
  85. {
  86. ScreenManager.AddScreen(gameplayScreen, null);
  87. }
  88. }
  89. }
  90. else if (assetsLoaded)
  91. {
  92. // Screen is not exiting
  93. if ( !IsExiting)
  94. {
  95. // Move on to the game play screen once highscore data is loaded
  96. foreach (GameScreen screen in ScreenManager.GetScreens())
  97. {
  98. screen.ExitScreen();
  99. }
  100. // Add the gameplay screen
  101. if (difficultyMode.HasValue)
  102. {
  103. ScreenManager.AddScreen(gameplayScreen, null);
  104. }
  105. }
  106. }
  107. base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
  108. }
  109. /// <summary>
  110. /// Handle any input from the user
  111. /// </summary>
  112. /// <param name="gameTime"></param>
  113. /// <param name="input"></param>
  114. public override void HandleInput(GameTime gameTime, InputState input)
  115. {
  116. if (input == null)
  117. {
  118. throw new ArgumentNullException("input");
  119. }
  120. PlayerIndex player;
  121. // Return to the main menu when a tap gesture is recognized
  122. if (input.Gestures.Count > 0)
  123. {
  124. GestureSample sample = input.Gestures[0];
  125. if (sample.GestureType == GestureType.Tap)
  126. {
  127. StartNewLevelOrExit(input);
  128. input.Gestures.Clear();
  129. }
  130. }
  131. // Handle keyboard
  132. else if (input.IsNewKeyPress(Keys.Enter, ControllingPlayer, out player) ||
  133. input.IsNewKeyPress(Keys.Space, ControllingPlayer, out player))
  134. {
  135. StartNewLevelOrExit(input);
  136. }
  137. base.HandleInput(gameTime, input);
  138. }
  139. #endregion
  140. #region Render
  141. /// <summary>
  142. /// Renders the screen
  143. /// </summary>
  144. /// <param name="gameTime"></param>
  145. public override void Draw(GameTime gameTime)
  146. {
  147. SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
  148. spriteBatch.Begin();
  149. // Draw the footer text
  150. if (difficultyMode.HasValue)
  151. {
  152. #if WINDOWS_PHONE
  153. string actionText = "Touch to start next level";
  154. #else
  155. string actionText = "Press space to start next level";
  156. #endif
  157. spriteBatch.DrawString(font16px, actionText,
  158. new Vector2(ScreenManager.GraphicsDevice.Viewport.Width / 2 -
  159. font16px.MeasureString(actionText).X / 2,
  160. safeArea.Bottom - font16px.MeasureString(actionText).Y - 4),
  161. Color.Black);
  162. }
  163. else
  164. {
  165. #if WINDOWS_PHONE
  166. string actionText = "Touch to end game";
  167. #else
  168. string actionText = "Press space to end game";
  169. #endif
  170. spriteBatch.DrawString(font16px, actionText,
  171. new Vector2(safeArea.Left + safeArea.Width / 2 - font16px.MeasureString(actionText).X / 2,
  172. safeArea.Top + safeArea.Height - font16px.MeasureString(actionText).Y - 4),
  173. Color.Black);
  174. }
  175. spriteBatch.End();
  176. }
  177. #endregion
  178. #region Private Functions
  179. /// <summary>
  180. /// Starts new level or exit to High Score
  181. /// </summary>
  182. /// <param name="input"></param>
  183. private void StartNewLevelOrExit(InputState input)
  184. {
  185. // If there is no next level - go to high score screen
  186. if (!difficultyMode.HasValue)
  187. {
  188. // If is in high score, gets is name
  189. if (GameplayScreen.FinalScore != 0 && HighScoreScreen.IsInHighscores(GameplayScreen.FinalScore))
  190. {
  191. Guide.BeginShowKeyboardInput(PlayerIndex.One,
  192. "Player Name", "What is your name (max 15 characters)?", "Player",
  193. AfterPlayerEnterName, null);
  194. }
  195. else
  196. {
  197. foreach (GameScreen screen in ScreenManager.GetScreens())
  198. {
  199. screen.ExitScreen();
  200. }
  201. ScreenManager.AddScreen(new BackgroundScreen("highScoreScreen"), null);
  202. ScreenManager.AddScreen(new HighScoreScreen(), null);
  203. }
  204. }
  205. // If not already loading
  206. else if (!isLoading)
  207. {
  208. #if MONOMAC
  209. // Start loading the resources on main thread
  210. // If not then all sorts of errors happen for
  211. // AutoReleasPools and OpenGL does not handle
  212. // multiple thread to well when using Thread
  213. MonoMac.AppKit.NSApplication.SharedApplication.BeginInvokeOnMainThread(delegate {
  214. gameplayScreen.LoadAssets ();
  215. isLoading = false;
  216. assetsLoaded = true;
  217. });
  218. #else
  219. // Start loading the resources in an additional thread
  220. thread = new Thread(new ThreadStart(gameplayScreen.LoadAssets));
  221. isLoading = true;
  222. thread.Start();
  223. #endif
  224. }
  225. }
  226. /// <summary>
  227. /// A handler invoked after the user has enter his name.
  228. /// </summary>
  229. /// <param name="result"></param>
  230. private void AfterPlayerEnterName(IAsyncResult result)
  231. {
  232. // Gets the name entered
  233. string playerName = Guide.EndShowKeyboardInput(result);
  234. if (!string.IsNullOrEmpty(playerName))
  235. {
  236. // Ensure that it is valid
  237. if (playerName != null && playerName.Length > 15)
  238. playerName = playerName.Substring(0, 15);
  239. // Puts it in high score
  240. HighScoreScreen.PutHighScore(playerName, GameplayScreen.FinalScore);
  241. HighScoreScreen.HighScoreChanged();
  242. }
  243. // Moves to the next screen
  244. foreach (GameScreen screen in ScreenManager.GetScreens())
  245. {
  246. screen.ExitScreen();
  247. }
  248. ScreenManager.AddScreen(new BackgroundScreen("highScoreScreen"), null);
  249. ScreenManager.AddScreen(new HighScoreScreen(), null);
  250. }
  251. #endregion
  252. }
  253. }