Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // in Constructor ****
- public struct HighScoreData
- {
- public string[] PlayerName;
- public int[] Score;
- public int Count;
- public HighScoreData(int count)
- {
- PlayerName = new string[count];
- Score = new int[count];
- Count = count;
- }
- }
- private HighScoreData entries = new HighScoreData(20);
- //Classes ***
- private void PromptMe()
- {
- // we can set our supported languages explicitly or we can allow the
- // game to support all the languages. the first language given will
- // be the default if the current language is not one of the supported
- // languages. this only affects the text found in message boxes shown
- // by EasyStorage and does not have any affect on the rest of the game.
- EasyStorageSettings.SetSupportedLanguages(Language.French, Language.Spanish);
- // on Windows Phone we use a save device that uses IsolatedStorage
- // on Windows and Xbox 360, we use a save device that gets a
- //shared StorageDevice to handle our file IO.
- #if WINDOWS_PHONE
- saveDevice = new IsolatedStorageSaveDevice();
- Global.SaveDevice = saveDevice;
- // we use the tap gesture for input on the phone
- TouchPanel.EnabledGestures = GestureType.Tap;
- #else
- // create and add our SaveDevice
- SharedSaveDevice sharedSaveDevice = new SharedSaveDevice();
- Components.Add(sharedSaveDevice);
- // ScreenManager.Game.Components.Add(sharedSaveDevice);
- // make sure we hold on to the device
- saveDevice = sharedSaveDevice;
- // hook two event handlers to force the user to choose a new device if they cancel the
- // device selector or if they disconnect the storage device after selecting it
- sharedSaveDevice.DeviceSelectorCanceled += (s, e) => e.Response = SaveDeviceEventResponse.Force;
- sharedSaveDevice.DeviceDisconnected += (s, e) => e.Response = SaveDeviceEventResponse.Force;
- // prompt for a device on the first Update we can
- sharedSaveDevice.PromptForDevice();
- sharedSaveDevice.DeviceSelected += (s, e) =>
- {
- //Save our save device to the global counterpart, so we can access it
- //anywhere we want to save/load
- Global.SaveDevice = (SaveDevice)s;
- //Once they select a storage device, we can load the main menu.
- };
- #endif
- }
- public void CreateScores()
- {
- // if (!Global.SaveDevice.FileExists(Global.containerName, Global.fileName_options))
- // {
- HighScoreData data = new HighScoreData(20);
- data.PlayerName[0] = "Name1";
- data.Level[0] = 1;
- data.Score[0] = 1;
- //......1-18 are here also
- data.PlayerName[19] = "Name19";
- data.Level[19] = 19;
- data.Score[91] = 19;
- SaveHighScores(data, HighScoresContainer, HighScoresFilename);
- // }
- }
- public static void SaveHighScores(HighScoreData data, string container, string filename)
- { // Get the path of the save game
- // string fullpath = Path.Combine(StorageContainer.TitleLocation, filename);
- // make sure the device is ready
- if (Global.SaveDevice.IsReady)
- { // save a file asynchronously. this will trigger IsBusy to return true
- // for the duration of the save process.
- Global.SaveDevice.SaveAsync(
- container,
- filename,
- stream =>
- {
- File.Open(filename, FileMode.OpenOrCreate);
- //File.Create(filename); //File.OpenWrite(filename);
- try
- {
- // Convert the object to XML data and put it in the stream
- XmlSerializer serializer = new XmlSerializer(typeof(HighScoreData));
- serializer.Serialize(stream, data);
- }
- finally
- { // Close the file
- stream.Close();
- // stream.Dispose();
- }
- });
- }
- }
- public static HighScoreData LoadHighScores(string container, string filename)
- {
- HighScoreData data = new HighScoreData();
- if (Global.SaveDevice.FileExists(container, filename))
- {
- Global.SaveDevice.Load(container, filename, stream =>
- {
- File.Open(Global.fileName_options, FileMode.OpenOrCreate,
- FileAccess.Read);
- try
- {
- // Read the data from the file
- XmlSerializer serializer = new XmlSerializer(typeof(HighScoreData));
- data = (HighScoreData)serializer.Deserialize(stream);
- }
- finally
- {
- // Close the file
- stream.Close();
- // stream.Dispose();
- }
- });
- }
- return (data);
- }
- private void SaveHighScore()
- {
- SignedInGamer gamer = Gamer.SignedInGamers[CurrentPlayer];
- if (gamer != null)
- {
- playerName = gamer.Gamertag;
- }
- else
- {
- playerName = "Guest";
- }
- // Create the data to save
- HighScoreData data = LoadHighScores(HighScoresContainer, HighScoresFilename);
- int scoreIndex = -1; for (int i = 0; i < data.Count; i++)
- {
- if (playerScore > data.Score[i])
- {
- scoreIndex = i;
- break;
- }
- }
- if (scoreIndex > -1)
- {
- for (int i = data.Count - 1; i > scoreIndex; i--)
- {
- data.PlayerName[i] = data.PlayerName[i - 1];
- data.Score[i] = data.Score[i - 1];
- }
- data.PlayerName[scoreIndex] = playerName;
- data.Score[scoreIndex] = playerScore;
- SaveHighScores(data, HighScoresContainer, HighScoresFilename);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement