LoadingAndInstructionsScreen.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // LoadingAndInstructionScreen.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.Graphics;
  12. using Microsoft.Xna.Framework;
  13. using GameStateManagement;
  14. using System.Threading;
  15. using Microsoft.Xna.Framework.Input.Touch;
  16. #endregion
  17. namespace MemoryMadness
  18. {
  19. class LoadingAndInstructionsScreen : GameScreen
  20. {
  21. #region Fields
  22. Texture2D background;
  23. SpriteFont font;
  24. bool isLoading;
  25. GameplayScreen gameplayScreen;
  26. Thread thread;
  27. int levelNumber;
  28. int movesPerformed;
  29. bool isResuming;
  30. #endregion
  31. #region Initialization
  32. public LoadingAndInstructionsScreen(bool isResuming)
  33. {
  34. levelNumber = 1;
  35. movesPerformed = 0;
  36. TransitionOnTime = TimeSpan.FromSeconds(0);
  37. TransitionOffTime = TimeSpan.FromSeconds(0.5);
  38. EnabledGestures = GestureType.Tap;
  39. // Initialize the current level and number of moves already performed
  40. // according to the game state information
  41. if (PhoneApplicationService.Current.State.ContainsKey("CurrentLevel"))
  42. {
  43. this.levelNumber =
  44. (int)PhoneApplicationService.Current.State["CurrentLevel"];
  45. PhoneApplicationService.Current.State.Remove("CurrentLevel");
  46. }
  47. if (PhoneApplicationService.Current.State.ContainsKey("MovesPerformed"))
  48. {
  49. this.movesPerformed =
  50. (int)PhoneApplicationService.Current.State["MovesPerformed"];
  51. PhoneApplicationService.Current.State.Remove("MovesPerformed");
  52. }
  53. this.isResuming = isResuming;
  54. }
  55. #endregion
  56. #region Loading
  57. /// <summary>
  58. /// Load the screen resources
  59. /// </summary>
  60. public override void LoadContent()
  61. {
  62. if (!isResuming)
  63. {
  64. background = Load<Texture2D>(@"Textures\Backgrounds\Instructions");
  65. }
  66. else
  67. {
  68. background = Load<Texture2D>(@"Textures\Backgrounds\Resuming");
  69. }
  70. font = Load<SpriteFont>(@"Fonts\MenuFont");
  71. // Create a new instance of the gameplay screen
  72. gameplayScreen = new GameplayScreen(levelNumber, movesPerformed);
  73. gameplayScreen.ScreenManager = ScreenManager;
  74. }
  75. #endregion
  76. #region Update
  77. /// <summary>
  78. /// Exit the screen after a tap gesture.
  79. /// </summary>
  80. /// <param name="input"></param>
  81. public override void HandleInput(InputState input)
  82. {
  83. if (!isLoading)
  84. {
  85. if (input.Gestures.Count > 0)
  86. {
  87. if (input.Gestures[0].GestureType == GestureType.Tap)
  88. {
  89. // Start loading the gameplay resources in an additional thread
  90. thread = new Thread(
  91. new ThreadStart(gameplayScreen.LoadAssets));
  92. isLoading = true;
  93. thread.Start();
  94. }
  95. }
  96. }
  97. base.HandleInput(input);
  98. }
  99. /// <summary>
  100. /// Screen update logic.
  101. /// </summary>
  102. /// <param name="gameTime"></param>
  103. /// <param name="otherScreenHasFocus"></param>
  104. /// <param name="coveredByOtherScreen"></param>
  105. public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
  106. {
  107. // If the additional thread is running, wait for it to finish
  108. if (null != thread)
  109. {
  110. // If the additional thread finished loading and the screen is not
  111. // exiting, exit it
  112. if (thread.ThreadState == ThreadState.Stopped && !IsExiting)
  113. {
  114. // Move on to the gameplay screen
  115. foreach (GameScreen screen in ScreenManager.GetScreens())
  116. screen.ExitScreen();
  117. gameplayScreen.IsActive = true;
  118. ScreenManager.AddScreen(gameplayScreen, null);
  119. }
  120. }
  121. // if resuming, don't wait for the user to launch the loading thread
  122. else if (isResuming)
  123. {
  124. thread = new Thread(
  125. new ThreadStart(gameplayScreen.LoadAssets));
  126. isLoading = true;
  127. thread.Start();
  128. }
  129. base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
  130. }
  131. #endregion
  132. #region Render
  133. /// <summary>
  134. /// Render screen
  135. /// </summary>
  136. /// <param name="gameTime"></param>
  137. public override void Draw(GameTime gameTime)
  138. {
  139. SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
  140. spriteBatch.Begin();
  141. // Draw Background
  142. spriteBatch.Draw(background, new Vector2(0, 0),
  143. Color.White * TransitionAlpha);
  144. // If loading gameplay screen resource in the
  145. // background show "Loading..." text
  146. if (isLoading && !isResuming)
  147. {
  148. string text = "Loading...";
  149. Vector2 size = font.MeasureString(text);
  150. Vector2 position = new Vector2(
  151. (ScreenManager.GraphicsDevice.Viewport.Width - size.X) / 2,
  152. (ScreenManager.GraphicsDevice.Viewport.Height - size.Y) / 2);
  153. spriteBatch.DrawString(font, text, position, Color.White);
  154. }
  155. spriteBatch.End();
  156. }
  157. #endregion
  158. }
  159. }