Advertisement
doozerdude

xna scoring problem

Oct 23rd, 2011
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.61 KB | None | 0 0
  1. // in Constructor ****
  2.  
  3. public struct HighScoreData
  4. {
  5. public string[] PlayerName;
  6. public int[] Score;
  7.  
  8. public int Count;
  9.  
  10. public HighScoreData(int count)
  11. {
  12. PlayerName = new string[count];
  13. Score = new int[count];
  14.  
  15. Count = count;
  16. }
  17. }
  18.  
  19. private HighScoreData entries = new HighScoreData(20);
  20.  
  21. //Classes ***
  22.  
  23.  
  24.  
  25. private void PromptMe()
  26. {
  27. // we can set our supported languages explicitly or we can allow the
  28. // game to support all the languages. the first language given will
  29. // be the default if the current language is not one of the supported
  30. // languages. this only affects the text found in message boxes shown
  31. // by EasyStorage and does not have any affect on the rest of the game.
  32. EasyStorageSettings.SetSupportedLanguages(Language.French, Language.Spanish);
  33. // on Windows Phone we use a save device that uses IsolatedStorage
  34. // on Windows and Xbox 360, we use a save device that gets a
  35. //shared StorageDevice to handle our file IO.
  36. #if WINDOWS_PHONE
  37. saveDevice = new IsolatedStorageSaveDevice();
  38. Global.SaveDevice = saveDevice;
  39. // we use the tap gesture for input on the phone
  40. TouchPanel.EnabledGestures = GestureType.Tap;
  41. #else
  42. // create and add our SaveDevice
  43. SharedSaveDevice sharedSaveDevice = new SharedSaveDevice();
  44. Components.Add(sharedSaveDevice);
  45. // ScreenManager.Game.Components.Add(sharedSaveDevice);
  46. // make sure we hold on to the device
  47. saveDevice = sharedSaveDevice;
  48. // hook two event handlers to force the user to choose a new device if they cancel the
  49. // device selector or if they disconnect the storage device after selecting it
  50. sharedSaveDevice.DeviceSelectorCanceled += (s, e) => e.Response = SaveDeviceEventResponse.Force;
  51. sharedSaveDevice.DeviceDisconnected += (s, e) => e.Response = SaveDeviceEventResponse.Force;
  52. // prompt for a device on the first Update we can
  53. sharedSaveDevice.PromptForDevice();
  54. sharedSaveDevice.DeviceSelected += (s, e) =>
  55. {
  56. //Save our save device to the global counterpart, so we can access it
  57. //anywhere we want to save/load
  58. Global.SaveDevice = (SaveDevice)s;
  59. //Once they select a storage device, we can load the main menu.
  60.  
  61. };
  62.  
  63. #endif
  64.  
  65. }
  66.  
  67. public void CreateScores()
  68. {
  69. // if (!Global.SaveDevice.FileExists(Global.containerName, Global.fileName_options))
  70. // {
  71.  
  72. HighScoreData data = new HighScoreData(20);
  73. data.PlayerName[0] = "Name1";
  74. data.Level[0] = 1;
  75. data.Score[0] = 1;
  76.  
  77. //......1-18 are here also
  78.  
  79. data.PlayerName[19] = "Name19";
  80. data.Level[19] = 19;
  81. data.Score[91] = 19;
  82.  
  83. SaveHighScores(data, HighScoresContainer, HighScoresFilename);
  84. // }
  85. }
  86.  
  87. public static void SaveHighScores(HighScoreData data, string container, string filename)
  88. { // Get the path of the save game
  89. // string fullpath = Path.Combine(StorageContainer.TitleLocation, filename);
  90. // make sure the device is ready
  91. if (Global.SaveDevice.IsReady)
  92. { // save a file asynchronously. this will trigger IsBusy to return true
  93. // for the duration of the save process.
  94. Global.SaveDevice.SaveAsync(
  95. container,
  96. filename,
  97. stream =>
  98. {
  99. File.Open(filename, FileMode.OpenOrCreate);
  100. //File.Create(filename); //File.OpenWrite(filename);
  101. try
  102. {
  103. // Convert the object to XML data and put it in the stream
  104. XmlSerializer serializer = new XmlSerializer(typeof(HighScoreData));
  105. serializer.Serialize(stream, data);
  106. }
  107. finally
  108. { // Close the file
  109. stream.Close();
  110. // stream.Dispose();
  111. }
  112. });
  113.  
  114. }
  115. }
  116.  
  117. public static HighScoreData LoadHighScores(string container, string filename)
  118. {
  119. HighScoreData data = new HighScoreData();
  120.  
  121. if (Global.SaveDevice.FileExists(container, filename))
  122. {
  123. Global.SaveDevice.Load(container, filename, stream =>
  124. {
  125. File.Open(Global.fileName_options, FileMode.OpenOrCreate,
  126. FileAccess.Read);
  127. try
  128. {
  129. // Read the data from the file
  130. XmlSerializer serializer = new XmlSerializer(typeof(HighScoreData));
  131. data = (HighScoreData)serializer.Deserialize(stream);
  132. }
  133. finally
  134. {
  135. // Close the file
  136.  
  137. stream.Close();
  138. // stream.Dispose();
  139.  
  140. }
  141.  
  142. });
  143.  
  144. }
  145.  
  146. return (data);
  147. }
  148.  
  149.  
  150. private void SaveHighScore()
  151. {
  152.  
  153. SignedInGamer gamer = Gamer.SignedInGamers[CurrentPlayer];
  154.  
  155. if (gamer != null)
  156. {
  157. playerName = gamer.Gamertag;
  158. }
  159. else
  160. {
  161. playerName = "Guest";
  162. }
  163.  
  164. // Create the data to save
  165. HighScoreData data = LoadHighScores(HighScoresContainer, HighScoresFilename);
  166.  
  167. int scoreIndex = -1; for (int i = 0; i < data.Count; i++)
  168. {
  169. if (playerScore > data.Score[i])
  170. {
  171. scoreIndex = i;
  172. break;
  173. }
  174. }
  175.  
  176. if (scoreIndex > -1)
  177. {
  178. for (int i = data.Count - 1; i > scoreIndex; i--)
  179. {
  180. data.PlayerName[i] = data.PlayerName[i - 1];
  181. data.Score[i] = data.Score[i - 1];
  182. }
  183. data.PlayerName[scoreIndex] = playerName;
  184. data.Score[scoreIndex] = playerScore;
  185.  
  186. SaveHighScores(data, HighScoresContainer, HighScoresFilename);
  187. }
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement