PauseScreen.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // PauseScreen.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 GameStateManagement;
  13. using Microsoft.Xna.Framework.GamerServices;
  14. #endregion
  15. namespace MemoryMadness
  16. {
  17. class PauseScreen : MenuScreen
  18. {
  19. #region Fields
  20. bool isResuming;
  21. bool checkHighscore = false;
  22. bool moveToHighScore = false;
  23. bool moveToMainMenu = false;
  24. #endregion
  25. #region Initializations
  26. /// <summary>
  27. /// Creates a new instance of the pause screen.
  28. /// </summary>
  29. /// <param name="isResuming">Whether or not the screen is displayed as a
  30. /// response to resuming the game (returning to it after the win key has
  31. /// been pressed, for example).</param>
  32. public PauseScreen(bool isResuming)
  33. : base("Pause")
  34. {
  35. // Create our menu entries
  36. MenuEntry returnGameMenuEntry = new MenuEntry("Return");
  37. MenuEntry exitMenuEntry = new MenuEntry("Quit");
  38. // Hook up menu event handlers
  39. returnGameMenuEntry.Selected += ReturnGameMenuEntrySelected;
  40. exitMenuEntry.Selected += OnCancel;
  41. // Add entries to the menu
  42. MenuEntries.Add(returnGameMenuEntry);
  43. MenuEntries.Add(exitMenuEntry);
  44. this.isResuming = isResuming;
  45. if (!isResuming)
  46. AudioManager.PauseResumeSounds(false);
  47. }
  48. #endregion
  49. #region Loading
  50. /// <summary>
  51. /// Load screen resources
  52. /// </summary>
  53. public override void LoadContent()
  54. {
  55. if (isResuming && !AudioManager.IsInitialized)
  56. AudioManager.LoadSounds();
  57. AudioManager.PlaySound("menu");
  58. base.LoadContent();
  59. }
  60. #endregion
  61. #region Update
  62. public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
  63. {
  64. if (checkHighscore && (!Guide.IsVisible))
  65. {
  66. checkHighscore = false;
  67. var gameplayScreen = GetGameplayScreen();
  68. var levelNumber = gameplayScreen.currentLevel.levelNumber;
  69. if (HighScoreScreen.IsInHighscores(levelNumber))
  70. {
  71. // Show the device's keyboard to record a high score
  72. Guide.BeginShowKeyboardInput(PlayerIndex.One,
  73. Constants.HighscorePopupTitle, Constants.HighscorePopupText,
  74. Constants.HighscorePopupDefault, ShowHighscorePromptEnded,
  75. levelNumber);
  76. }
  77. else
  78. {
  79. moveToMainMenu = true;
  80. }
  81. }
  82. else if (moveToHighScore)
  83. {
  84. foreach (GameScreen screen in ScreenManager.GetScreens())
  85. screen.ExitScreen();
  86. ScreenManager.AddScreen(new BackgroundScreen(true), null);
  87. ScreenManager.AddScreen(new HighScoreScreen(), null);
  88. }
  89. else if (moveToMainMenu)
  90. {
  91. foreach (GameScreen screen in ScreenManager.GetScreens())
  92. screen.ExitScreen();
  93. ScreenManager.AddScreen(new BackgroundScreen(false), null);
  94. ScreenManager.AddScreen(new MainMenuScreen(), null);
  95. }
  96. base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
  97. }
  98. /// <summary>
  99. /// Asynchronous handler for the highscore player name popup messagebox.
  100. /// </summary>
  101. /// <param name="result">The popup messagebox result. The result's
  102. /// AsyncState should contain the level number which acts as the
  103. /// highscore.</param>
  104. private void ShowHighscorePromptEnded(IAsyncResult result)
  105. {
  106. string playerName = Guide.EndShowKeyboardInput(result);
  107. int levelNumber = (int)result.AsyncState;
  108. if (playerName != null)
  109. {
  110. if (playerName.Length > 15)
  111. playerName = playerName.Substring(0, 15);
  112. HighScoreScreen.PutHighScore(playerName, levelNumber);
  113. }
  114. moveToHighScore = true;
  115. }
  116. /// <summary>
  117. /// Respond to "Return" Item Selection
  118. /// </summary>
  119. /// <param name="sender"></param>
  120. /// <param name="e"></param>
  121. void ReturnGameMenuEntrySelected(object sender, EventArgs e)
  122. {
  123. if (!isResuming)
  124. {
  125. // Resume sounds and activate the gameplay screen
  126. AudioManager.PauseResumeSounds(true);
  127. var screens = ScreenManager.GetScreens();
  128. foreach (GameScreen screen in screens)
  129. {
  130. if (!(screen is GameplayScreen))
  131. {
  132. screen.ExitScreen();
  133. }
  134. }
  135. (ScreenManager.GetScreens()[0] as GameplayScreen).IsActive = true;
  136. }
  137. else
  138. {
  139. // Since we are resuming the game, go to the loading screen which will
  140. // in turn initialize the gameplay screen
  141. foreach (GameScreen screen in ScreenManager.GetScreens())
  142. screen.ExitScreen();
  143. ScreenManager.AddScreen(new LoadingAndInstructionsScreen(true), null);
  144. }
  145. }
  146. /// <summary>
  147. /// Respond to "Quit Game" Item Selection
  148. /// </summary>
  149. /// <param name="playerIndex"></param>
  150. protected override void OnCancel(PlayerIndex playerIndex)
  151. {
  152. AudioManager.StopSounds();
  153. // Give the user a chance to save his current progress
  154. Guide.BeginShowMessageBox("Save Game", "Do you want to save your progress?",
  155. new String[] { "Yes", "No" }, 0, MessageBoxIcon.Warning,
  156. ShowSaveDialogEnded, null);
  157. }
  158. /// <summary>
  159. /// Asynchronous handler for the game save popup messagebox.
  160. /// </summary>
  161. /// <param name="result">The popup messagebox result.</param>
  162. private void ShowSaveDialogEnded(IAsyncResult result)
  163. {
  164. int? res = Guide.EndShowMessageBox(result);
  165. if (res.HasValue)
  166. {
  167. // Store the user's progress
  168. if (res.Value == 0)
  169. {
  170. if (!PhoneApplicationService.Current.State.ContainsKey(
  171. "CurrentLevel"))
  172. {
  173. var gameplayScreen = GetGameplayScreen();
  174. PhoneApplicationService.Current.State["CurrentLevel"]
  175. = gameplayScreen.currentLevel.levelNumber;
  176. }
  177. foreach (GameScreen screen in ScreenManager.GetScreens())
  178. screen.ExitScreen();
  179. ScreenManager.AddScreen(new BackgroundScreen(false),
  180. null);
  181. ScreenManager.AddScreen(new MainMenuScreen(), null);
  182. }
  183. // The user really quit the game, see if he has a high score
  184. else
  185. {
  186. checkHighscore = true;
  187. }
  188. }
  189. }
  190. #endregion
  191. /// <summary>
  192. /// Finds a gameplay screen objects among all screens and returns it.
  193. /// </summary>
  194. /// <returns>A gameplay screen instance, or null if none
  195. /// are available.</returns>
  196. private GameplayScreen GetGameplayScreen()
  197. {
  198. var screens = ScreenManager.GetScreens();
  199. foreach (var screen in screens)
  200. {
  201. if (screen is GameplayScreen)
  202. {
  203. return screen as GameplayScreen;
  204. }
  205. }
  206. return null;
  207. }
  208. }
  209. }