//Returns sorted list of high scores public static highScoreList getAndSortHighScores() { highScoreList highScores; //Makes sure scores key is stored if (PlayerPrefs.HasKey("scores") == false) { //If not, creates new list for scores to be added List <scoreEntry> entryList = new List <scoreEntry>(); highScores = new highScoreList { highScoreEntryList = entryList }; string json = JsonUtility.ToJson(highScores); PlayerPrefs.SetString("scores", json); PlayerPrefs.Save(); } else { //Loads current stored scores string currentScores = PlayerPrefs.GetString("scores"); highScores = JsonUtility.FromJson <highScoreList>(currentScores); //scoreEntry newEntry = new scoreEntry { name = "KIR", score = 600000000 }; //highScores.highScoreEntryList.Add(newEntry); //Bubble sort to order ranks for (int i = 0; i < highScores.highScoreEntryList.Count; i++) { for (int j = i + 1; j < highScores.highScoreEntryList.Count; j++) { if (highScores.highScoreEntryList[j].score > highScores.highScoreEntryList[i].score) { scoreEntry temp = highScores.highScoreEntryList[i]; highScores.highScoreEntryList[i] = highScores.highScoreEntryList[j]; highScores.highScoreEntryList[j] = temp; } } } //Trims list so only 15 remain if (highScores.highScoreEntryList.Count > 15) { for (int i = highScores.highScoreEntryList.Count; i > 15; i--) { highScores.highScoreEntryList.RemoveAt(15); if (highScores.highScoreEntryList.Count < 15) { break; } } } } return(highScores); }
//Resets the leaderboard public void clearScoreList() { string currentScores = PlayerPrefs.GetString("scores"); highScoreList highScores = JsonUtility.FromJson <highScoreList>(currentScores); highScores.highScoreEntryList.Clear(); string json = JsonUtility.ToJson(highScores); PlayerPrefs.SetString("scores", json); PlayerPrefs.Save(); }
//Used to create a new entry in the high-score table public static void createScore(long score, string name) { scoreEntry newEntry = new scoreEntry { score = score, name = name }; //Load current list string currentScores = PlayerPrefs.GetString("scores"); highScoreList highScores = JsonUtility.FromJson <highScoreList>(currentScores); //Adds new score and saves list highScores.highScoreEntryList.Add(newEntry); string json = JsonUtility.ToJson(highScores); PlayerPrefs.SetString("scores", json); PlayerPrefs.Save(); }
private void Awake() { scoreContainer = transform.Find("Entry Container"); scoreTemplate = scoreContainer.Find("Entry Template"); //Hides template scoreTemplate.gameObject.SetActive(false); //Grabs list of scores highScoreList highScores = getAndSortHighScores(); //Adds values to UI container scoreListTransform = new List <Transform>(); foreach (scoreEntry scoreEntry in highScores.highScoreEntryList) { createScoreEntryTransform(scoreEntry, scoreContainer, scoreListTransform); } }
void Start() { // if the previous scene was single player or single AI, then use the SinglePlayerStats static variables // for the display and playerScore variable if (mainButtons.sceneName == "Single Player" || mainButtons.sceneName == "Single AI") { //Updates UI with user score and number of bricks destroyed endScore.GetComponent <TextMeshProUGUI>().text = SinglePlayerStats.playerScore.ToString(); endBricks.GetComponent <TextMeshProUGUI>().text = SinglePlayerStats.playerBricksDestroyed.ToString(); // define playerScore variable playerScore = SinglePlayerStats.playerScore; } // if the previous scene was two player, then update the game over scene to be formatted to display // the scores and bricks destroyed for both players if (mainButtons.sceneName == "Two Player") { machineScore.SetActive(true); machineBricks.SetActive(true); playerHeader.SetActive(true); machineHeader.SetActive(true); // get the player's score and bricks destroyed from the two player stats static variables endScore.GetComponent <TextMeshProUGUI>().text = TwoPlayerStats.playerScore.ToString(); endBricks.GetComponent <TextMeshProUGUI>().text = TwoPlayerStats.playerBricksDestroyed.ToString(); // get the machine's score and bricks destroyed from the two player stats static variables machineScore.transform.GetChild(0).gameObject.GetComponent <TextMeshProUGUI>().text = TwoPlayerStats.machineScore.ToString(); machineBricks.transform.GetChild(0).gameObject.GetComponent <TextMeshProUGUI>().text = TwoPlayerStats.machineBricksDestroyed.ToString(); // define playerScore variable playerScore = TwoPlayerStats.playerScore; } // if the current scene is single player, then update the leaderboard if (mainButtons.sceneName == "Single Player" || mainButtons.sceneName == "Two Player") { //Grabs list of high-scores highScoreList highScores = getAndSortHighScores(); //List isn't full if (highScores.highScoreEntryList.Count < 15) { showNewScorePrompts(); } //List is full else { for (int i = 0; i < highScores.highScoreEntryList.Count; i++) { //Creates new high-score and breaks loop to prevent multiple entries if (playerScore > highScores.highScoreEntryList[i].score) { showNewScorePrompts(); break; } } } } }