// Loads the save game for the player. public void LoadSavedGames() { VariablesForSaving variablesToSave = new VariablesForSaving(); ReadHighScoreDictFromFile(HIGH_SCORE_FILE_NAME, variablesToSave); ReadNameDictFromFile(USER_NAME_FILE_NAME, variablesToSave); LevelScoresDict = variablesToSave.LevelScoresDictToSave; NameToLevelDict = variablesToSave.NameToLevelDictToSave; }
/* For user names. Writes each dictionary entry on a single line. The saved document should have as many lines as there are dictionary entries and assigns a dictionary key to a corresponding line in the file e.g. (LevelScoreDict[Level_1] = first line written to the file. */ public void WriteNameDictToFile(string filename, VariablesForSaving variablesToSave) { #if !WEB_BUILD // Path to the specified file name. string path = PathForDocumentFile(filename); // Using file stream to actually find and open/create the file which we are writing to. FileStream file = new FileStream(path, FileMode.Create, FileAccess.Write); // Stream writer allows us to actually read the file. StreamWriter sw = new StreamWriter(file); // Temp string to write each entry to the saved location. string str; // While foreach is bad to use, the dict we use it not that big and this ensures we don't grab an empty // entry and start at level 1, not level 0. foreach(int key in variablesToSave.LevelScoresDictToSave.Keys){ str = variablesToSave.NameToLevelDictToSave[key].ToString(); sw.WriteLine(str); } sw.Close(); file.Close(); #endif }
// Saves the game and all stats needed for next relaunch. /* Input: 'currentLevel': The current level being saved. 'name': The name of the player who had played this level. 'score': The score to be saved. */ public void SaveGame(int currentLevel, string name, int score) { VariablesForSaving variablesToSave = new VariablesForSaving(); if(NameToLevelDict.ContainsKey(currentLevel)){ NameToLevelDict.Remove(currentLevel); LevelScoresDict.Remove(currentLevel); } NameToLevelDict.Add(currentLevel, name); LevelScoresDict.Add(currentLevel, score); variablesToSave.LevelScoresDictToSave = LevelScoresDict; variablesToSave.NameToLevelDictToSave = NameToLevelDict; WriteHighScoreDictToFile(HIGH_SCORE_FILE_NAME, variablesToSave); WriteNameDictToFile(USER_NAME_FILE_NAME, variablesToSave); /*Debug.Log( "Player: " + name + "\nLevel: " + currentLevel + "\nScore: " + score);*/ LoadSavedGames(); }
/* For high score names: Iterates through the saved file specified from the input paramater one line at a time. The saved document should have as many lines as there are dictionary entries and assigns a dictionary key to a corresponding line in the file e.g. (LevelScoreDict[Level_1] = first line read from the file. */ public string ReadNameDictFromFile(string filename, VariablesForSaving variablesToSave) { #if !WEB_BUILD // Path to the specified file name. string path = PathForDocumentFile(filename); // Check to see if the path exists, otherwise return null. if(File.Exists(path)){ // Using file stream to actually find and open/create the file which we are reading from. FileStream file = new FileStream(path, FileMode.Open, FileAccess.Read); // Stream writer allows us to actually read the file. StreamReader sr = new StreamReader(file); // Temporary string to read from string str = null; // Dictionary starts at entry 1, corresponding to level number int key = 1; // Read a single line at a time, ensuring we don't read past the end of the file. while((str = sr.ReadLine()) != null){ variablesToSave.NameToLevelDictToSave[key] = str; key++; } sr.Close(); file.Close(); return str; } else{ return null; } #else return null; #endif }
/// <summary> /// Loads the save game for the player /// </summary> public void LoadSaveGame() { VariablesForSaving variablesToSave = new VariablesForSaving(); ReadDictFromFile(FILE_NAME, variablesToSave); Debug.Log("Info Loaded"); Debug.Log("Level One Score " + variablesToSave.LevelScoresDictToSave[1].ToString()); LevelScoresDict = variablesToSave.LevelScoresDictToSave; }
/// <summary> /// Saves the game and all stats needed for next relaunch, TODO: IMPLEMENT! /// </summary> /// <param name='currentLevel'> /// Current level represents the current level being saved /// </param> /// <param name='score'> /// Score represents the score of the player to be saved /// </param> public void SaveGame(int currentLevel, int score) { VariablesForSaving variablesToSave = new VariablesForSaving(); variablesToSave.LevelScoresDictToSave[currentLevel] = score; WriteDictToFile(FILE_NAME, variablesToSave); Debug.Log("Info Saved"); }