public void WriteAndLoadHighscore() { // create highscore Highscore highscoreOriginal = AddDebugHighscoreEntries(); WriteJSON.SaveHighscore(filename, highscoreOriginal); // load highscore Highscore highscore = WriteJSON.LoadHighscore(filename); // check if both highscores have the same amount of entries Assert.AreEqual(highscoreOriginal.GetLength(), highscore.GetLength(), "The original highscore and the saved and loaded highscore have different lengths: " + highscoreOriginal.GetLength() + "/" + highscore.GetLength() + "."); // check if all highscore entries are equal for (int i = 0; i < highscoreOriginal.GetLength(); i++) { Assert.IsTrue(highscoreOriginal.GetEntry(i).Equals(highscore.GetEntry(i)), "Entry " + i + " differs in the saved and read highscore from the original value. \n" + "original: " + highscoreOriginal.GetEntry(i).ToString() + "\n" + "new : " + highscore.GetEntry(i).ToString()); } // cleanup DeleteTestFiles(); }
public void WriteHighscoreToFile() { Highscore highscore = AddDebugHighscoreEntries(); WriteJSON.SaveHighscore(filename, highscore); // check if file was created succesfully FileAssert.Exists(Application.persistentDataPath + filename, "The highscore couldn't be written to the file: " + Application.persistentDataPath + filename); // cleanup DeleteTestFiles(); }
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"); } }