MemoryMadnessGame.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // MemoryMadness.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 Microsoft.Xna.Framework;
  13. using Microsoft.Xna.Framework.Audio;
  14. using Microsoft.Xna.Framework.Content;
  15. using Microsoft.Xna.Framework.GamerServices;
  16. using Microsoft.Xna.Framework.Graphics;
  17. using Microsoft.Xna.Framework.Input;
  18. using Microsoft.Xna.Framework.Input.Touch;
  19. using Microsoft.Xna.Framework.Media;
  20. using GameStateManagement;
  21. using System.IO.IsolatedStorage;
  22. using System.IO;
  23. #endregion
  24. namespace MemoryMadness
  25. {
  26. /// <summary>
  27. /// This is the main type for your game
  28. /// </summary>
  29. public class MemoryMadnessGame : Microsoft.Xna.Framework.Game
  30. {
  31. #region Fields
  32. GraphicsDeviceManager graphics;
  33. ScreenManager screenManager;
  34. #endregion
  35. #region Initializations
  36. public MemoryMadnessGame()
  37. {
  38. graphics = new GraphicsDeviceManager(this);
  39. Content.RootDirectory = "Content";
  40. // Frame rate is 30 fps by default for Windows Phone.
  41. TargetElapsedTime = TimeSpan.FromTicks(333333);
  42. //Create a new instance of the Screen Manager
  43. screenManager = new ScreenManager(this);
  44. Components.Add(screenManager);
  45. // Switch to full screen for best game experience
  46. graphics.IsFullScreen = true;
  47. graphics.PreferredBackBufferHeight = 800;
  48. graphics.PreferredBackBufferWidth = 480;
  49. graphics.SupportedOrientations = DisplayOrientation.Portrait;
  50. // Initialize sound system
  51. AudioManager.Initialize(this);
  52. // Add two new screens
  53. screenManager.AddScreen(new BackgroundScreen(false), null);
  54. screenManager.AddScreen(new MainMenuScreen(), null);
  55. /* if (PhoneApplicationService.Current.StartupMode == StartupMode.Launch)
  56. screenManager.AddScreen(new MainMenuScreen(), null);
  57. else
  58. screenManager.AddScreen(new PauseScreen(true), null);
  59. // Subscribe to the application's lifecycle events
  60. PhoneApplicationService.Current.Activated += GameActivated;
  61. PhoneApplicationService.Current.Deactivated += GameDeactivated;
  62. PhoneApplicationService.Current.Closing += GameClosing;
  63. PhoneApplicationService.Current.Launching += GameLaunching;*/
  64. }
  65. #endregion
  66. #region Loading
  67. /// <summary>
  68. /// LoadContent will be called once per game and is the place to load
  69. /// all of your content.
  70. /// </summary>
  71. protected override void LoadContent()
  72. {
  73. AudioManager.LoadSounds();
  74. HighScoreScreen.LoadHighscores();
  75. base.LoadContent();
  76. }
  77. #endregion
  78. #region Tombstoning
  79. /// <summary>
  80. /// Saves the full state to the state object and the persistent state to
  81. /// isolated storage.
  82. /// </summary>
  83. /* void GameDeactivated(object sender, DeactivatedEventArgs e)
  84. {
  85. SaveToStateObject();
  86. if (PhoneApplicationService.Current.State.ContainsKey("CurrentLevel"))
  87. {
  88. SaveToIsolatedStorage((int)PhoneApplicationService.Current.State["CurrentLevel"]);
  89. PhoneApplicationService.Current.State.Remove("CurrentLevel");
  90. }
  91. }
  92. /// <summary>
  93. /// Loads the full state from the state object.
  94. /// </summary>
  95. void GameActivated(object sender, ActivatedEventArgs e)
  96. {
  97. LoadFromStateObject();
  98. LoadFromIsolatedStorage();
  99. }
  100. /// <summary>
  101. /// Saves persistent state to isolated storage.
  102. /// </summary>
  103. void GameClosing(object sender, ClosingEventArgs e)
  104. {
  105. if (PhoneApplicationService.Current.State.ContainsKey("CurrentLevel"))
  106. {
  107. SaveToIsolatedStorage((int)PhoneApplicationService.Current.State["CurrentLevel"]);
  108. PhoneApplicationService.Current.State.Remove("CurrentLevel");
  109. }
  110. else
  111. {
  112. CleanIsolatedStorage();
  113. }
  114. }*/
  115. /// <summary>
  116. /// Loads persistent state from isolated storage.
  117. /// </summary>
  118. /* void GameLaunching(object sender, LaunchingEventArgs e)
  119. {
  120. LoadFromIsolatedStorage();
  121. }*/
  122. #region Helpers functionality
  123. /// <summary>
  124. /// Saves current gameplay progress to the state object.
  125. /// </summary>
  126. private void SaveToStateObject()
  127. {
  128. // Get the gameplay screen object
  129. GameplayScreen gameplayScreen = GetGameplayScreen();
  130. if (null != gameplayScreen)
  131. {
  132. // If gameplay screen object found save current game progress to
  133. // the state object
  134. PhoneApplicationService.Current.State["MovesPerformed"] =
  135. gameplayScreen.currentLevel.MovesPerformed;
  136. PhoneApplicationService.Current.State["CurrentLevel"] =
  137. gameplayScreen.currentLevel.levelNumber;
  138. }
  139. }
  140. /// <summary>
  141. /// Loads the game progress from the state object if such information exits.
  142. /// </summary>
  143. private void LoadFromStateObject()
  144. {
  145. int sequenceProgress = 0;
  146. // Check state object for sequence progress and load it if found
  147. if (!PhoneApplicationService.Current.State.ContainsKey("MovesPerformed"))
  148. PhoneApplicationService.Current.State["MovesPerformed"] = sequenceProgress;
  149. }
  150. /// <summary>
  151. /// Saves the level progress to isolated storage
  152. /// </summary>
  153. /// <param name="currentLevel">The level number to save</param>
  154. private void SaveToIsolatedStorage(int currentLevel)
  155. {
  156. // Get Isolated Storage for current application
  157. using (IsolatedStorageFile isolatedStorageFile
  158. = IsolatedStorageFile.GetUserStoreForApplication())
  159. {
  160. // Create/overwrite file and save provided value
  161. /*using (IsolatedStorageFileStream fileStream
  162. = isolatedStorageFile.CreateFile("MemoryMadness.dat"))
  163. {
  164. using (StreamWriter streamWriter = new StreamWriter(fileStream))
  165. {
  166. streamWriter.WriteLine(currentLevel);
  167. }
  168. }*/
  169. }
  170. }
  171. /// <summary>
  172. /// Clean isolated storage from previously saved information.
  173. /// </summary>
  174. private void CleanIsolatedStorage()
  175. {
  176. using (IsolatedStorageFile isolatedStorageFile
  177. = IsolatedStorageFile.GetUserStoreForApplication())
  178. {
  179. isolatedStorageFile.DeleteFile("MemoryMadness.dat");
  180. }
  181. }
  182. /// <summary>
  183. /// Loads game progress from isolated storage file if such a file exits.
  184. /// </summary>
  185. private void LoadFromIsolatedStorage()
  186. {
  187. int currentLevel = 1;
  188. // Get the isolated storage for the current application
  189. using (IsolatedStorageFile isolatedStorageFile
  190. = IsolatedStorageFile.GetUserStoreForApplication())
  191. {
  192. // Check whether or not the data file exists
  193. /*if (isolatedStorageFile.FileExists("MemoryMadness.dat"))
  194. {
  195. // If the file exits, open it and read its information
  196. using (IsolatedStorageFileStream fileStream
  197. = isolatedStorageFile.OpenFile("MemoryMadness.dat", FileMode.Open))
  198. {
  199. using (StreamReader streamReader = new StreamReader(fileStream))
  200. {
  201. if (!int.TryParse(streamReader.ReadLine(), out currentLevel))
  202. currentLevel = 1;
  203. }
  204. }
  205. }*/
  206. }
  207. PhoneApplicationService.Current.State["CurrentLevel"] = currentLevel;
  208. }
  209. /// <summary>
  210. /// Finds a gameplay screen objects among all screens and returns it.
  211. /// </summary>
  212. /// <returns>A gameplay screen instance, or null if none
  213. /// are available.</returns>
  214. private GameplayScreen GetGameplayScreen()
  215. {
  216. var screens = screenManager.GetScreens();
  217. foreach (var screen in screens)
  218. {
  219. if (screen is GameplayScreen)
  220. {
  221. return screen as GameplayScreen;
  222. }
  223. }
  224. return null;
  225. }
  226. #endregion
  227. #endregion
  228. }
  229. }