private Highscore AddDebugHighscoreEntries() { Highscore highscore = new Highscore(); HighscoreEntry leet = new HighscoreEntry("1337H4x0r", 0.01f, 1); HighscoreEntry noob = new HighscoreEntry("xxNoobooNxx", 999.99f, 99); HighscoreEntry larry = new HighscoreEntry("Larry Laffer", 69.69f, 69); highscore.AddEntry(leet); highscore.AddEntry(noob); highscore.AddEntry(larry); return(highscore); }
public static Highscore LoadHighscore(string saveFileName) { // check if file exists // use empty highscore when no file was found FileInfo fileInfo = new FileInfo(Application.persistentDataPath + saveFileName); if (!fileInfo.Exists) { return(new Highscore()); } FileStream fileStream = File.Open(Application.persistentDataPath + saveFileName, FileMode.Open, FileAccess.Read); // overwrite highscore data in file with higscore data in the game StreamReader reader = new StreamReader(fileStream); Highscore highscore = new Highscore(); string entry; // read every line until the end of the file while ((entry = reader.ReadLine()) != null) { highscore.AddEntry(new HighscoreEntry(JsonUtility.FromJson <HighscoreEntry>(entry))); } reader.Close(); fileStream.Close(); return(highscore); }
public void PlayerEnteredGoal() { // stop the game stats.timer.PauseTimer(); controlsTutorial.enabled = false; EndscreenUI.SetActive(true); // hide the player (so the camera will still center) player.GetComponent <MeshRenderer>().enabled = false; player.GetComponent <Rigidbody>().velocity = Vector3.zero; player.GetComponent <Rigidbody>().useGravity = false; player.GetComponent <PlayerController>().enabled = false; player.transform.GetChild(0).gameObject.SetActive(false); // create highscore entry from current run HighscoreEntry entry = new HighscoreEntry("Player", stats.timer.currentTime, stats.hits); // add highscore entry to highscore Highscore highscore = WriteJSON.LoadHighscore(saveFilename); highscore.AddEntry(entry); // save new highscore entry to save file WriteJSON.SaveHighscore(saveFilename, highscore); // clear score board for (int i = 0; i < highscoreEntries.Length; i++) { highscoreEntries[i].transform.GetChild(0).GetComponent <Text>().text = ""; highscoreEntries[i].transform.GetChild(1).GetComponent <Text>().text = ""; highscoreEntries[i].transform.GetChild(2).GetComponent <Text>().text = ""; highscoreEntries[i].transform.GetChild(3).GetComponent <Text>().text = ""; } // get smallest needed max index to draw either all highscore entries (entries < first n entries) or to draw the first n entries int maxIndex = (highscore.GetLength() < highscoreEntries.Length) ? highscore.GetLength() : highscoreEntries.Length; highscore.Sort(); // show highscore for (int i = 0; i < maxIndex; i++) { HighscoreEntry highscoreEntry = highscore.GetEntry(i); highscoreEntries[i].transform.GetChild(0).GetComponent <Text>().text = highscoreEntry.name; highscoreEntries[i].transform.GetChild(1).GetComponent <Text>().text = highscoreEntry.time.ToString("0.00") + " sec"; highscoreEntries[i].transform.GetChild(2).GetComponent <Text>().text = highscoreEntry.strokes.ToString(); highscoreEntries[i].transform.GetChild(3).GetComponent <Text>().text = highscoreEntry.points.ToString("0.00"); } }