Ejemplo n.º 1
0
        /// <summary>
        /// Adds a score to the leaderboard save file
        /// </summary>
        /// <param name="nameToAdd">Name to add new score under</param>
        /// <param name="scoreToAdd">Score of new Score</param>
        /// <param name="levelName">Name of level to save score for</param>
        /// <param name="overrideScoreCountLimit">Add score if even if it excedes the number of allowed scores to save</param>
        /// <returns>If score was added to file</returns>
        public static bool AddScore(string nameToAdd, float scoreToAdd, string levelName, bool overrideScoreCountLimit = false)
        {
            //Get current score infomation
            ScoreSaveData loadedScoresInfo = LoadScoresInfo(levelName);

            //Check if we have less than the score count limit and can just save the score to the file
            //or we are overriding the limit
            if (loadedScoresInfo.scores.Count <= localScoreCountLimit || overrideScoreCountLimit)
            {
                loadedScoresInfo.scores.Add(new KeyValuePair <string, float>(nameToAdd, scoreToAdd));
                SaveScoresInfo(loadedScoresInfo, levelName);
                return(true);
            }
            else
            {
                //Work out if this score is greater than the lowest score, if it is
                //remove the last value and add this new value to the list
                if (scoreToAdd >= loadedScoresInfo.scores[loadedScoresInfo.scores.Count - 1].Value)
                {
                    loadedScoresInfo.scores.Remove(loadedScoresInfo.scores[loadedScoresInfo.scores.Count - 1]);
                    loadedScoresInfo.scores.Add(new KeyValuePair <string, float>(nameToAdd, scoreToAdd));
                    SaveScoresInfo(loadedScoresInfo, levelName);
                    return(true);
                }
            }

            return(false);
        }
Ejemplo n.º 2
0
    /// <summary>
    /// We check if the scores have been added. If they have not been added we add the entry by looping through and finding an empy entry
    /// </summary>
    /// <param name="scoreBoardEntryData"></param>
    public void AddEntry(ScoreBoardEntryData scoreBoardEntryData)
    {
        ScoreSaveData savedScores = GetSavedScores();
        bool          scoreAdded  = false;

        for (int i = 0; i < savedScores.highScores.Count; i++)
        {
            if (scoreBoardEntryData.entryScore > savedScores.highScores[i].entryScore)
            {
                savedScores.highScores.Insert(i, scoreBoardEntryData);
                scoreAdded = true;
                break;
            }
        }

        if (!scoreAdded && savedScores.highScores.Count < MaxEnteries)
        {
            savedScores.highScores.Add(scoreBoardEntryData);
        }

        if (savedScores.highScores.Count > MaxEnteries)
        {
            savedScores.highScores.RemoveRange(MaxEnteries, savedScores.highScores.Count - MaxEnteries);
        }

        UpdateUI(savedScores);
        SaveScores(savedScores);
    }
Ejemplo n.º 3
0
        /// <summary>
        /// Saves score info to scores file
        /// </summary>
        /// <param name="data">Score data to save</param>
        private static void SaveScoresInfo(ScoreSaveData data, string levelName)
        {
            //Sort Scores
            data.scores = SortScores(data.scores);
            string saveData = JsonConvert.SerializeObject(data, Formatting.Indented);

            File.WriteAllText(GetScoreSaveLocation(levelName), saveData);
        }
Ejemplo n.º 4
0
    private void Start()
    {
        ScoreSaveData savedScores = GetSavedScores();

        SaveScores(savedScores);

        UpdateUI(savedScores);
    }
Ejemplo n.º 5
0
 /// <summary>
 /// We save the data using streamwritter to the file format by parsing in the data from the ScoreSaveData script
 /// We are enabling pretty print to make it easier to read if we need to.
 /// </summary>
 /// <param name="scoreSaveData"></param> Our Data that needs to save
 void SaveScores(ScoreSaveData scoreSaveData)
 {
     using (StreamWriter stream = new StreamWriter(savePath))
     {
         string json = JsonUtility.ToJson(scoreSaveData, true);
         stream.Write(json);
     }
 }
Ejemplo n.º 6
0
    /// <summary>
    /// We update the UI with the saved data. Destroy the child gameobjects to refresh and repopulate as child
    /// </summary>
    /// <param name="savedScores"></param>
    void UpdateUI(ScoreSaveData savedScores)
    {
        foreach (Transform child in entryContainer)
        {
            Destroy(child.gameObject);
        }

        foreach (ScoreBoardEntryData hs in savedScores.highScores)
        {
            Instantiate(entryTemplate, entryContainer).GetComponent <ScoreBoardEntryUI>().Initialise(hs);
        }
    }
 // Input a new Score to the File and waits for a name to be inputed
 public void InputNewScore(int score)
 {
     newestData = new ScoreSaveData("", score);
     currentScores.Add(newestData);
     // Sort it
     SortDescending();
     // Update UI
     if (!UpdateUI(newestData))
     {
         // Buttons
         clearButton.SetActive(true);
         continueButton.SetActive(true);
     }
 }
    // Load Scores to UI from currentScores List
    // Pass in the newest score to attach a Input field to that name
    // Returns whether the newest Data is on the HighScore or if it's too low to even appear
    bool UpdateUI(ScoreSaveData newestData = null)
    {
        // Deactivate all existing texts first
        DeactivateAllText();

        // Add everything back in
        TextMeshProUGUI textCom    = null;
        bool            foundScore = false;

        // Loop through the scores
        for (int i = 0; i < currentScores.Count; ++i)
        {
            if (i >= maxCount)
            {
                break;
            }

            // If found the newest score
            if (currentScores[i] == newestData)
            {
                inputField.gameObject.SetActive(true);  // Enable the Input Field
                inputField.SetParent(nameContainer.transform);
                foundScore = true;
            }
            else
            {   // Means that the newest score is just too low
                sb.Clear();
                // Get the text component NAME
                textCom = GetNameText();
                textCom.transform.SetParent(nameContainer.transform);
                // Assign data
                textCom.text = sb.Append("-  " + currentScores[i].plyrName).ToString();
            }


            sb.Clear();
            // Get the text component SCORE
            textCom = GetScoreText();
            textCom.transform.SetParent(scoreContainer.transform);
            // Assign data
            textCom.text = sb.Append("-  " + currentScores[i].plyrScore).ToString();
        }

        return(foundScore);
    }
    // Saving Score into file path
    public void Save(string newName, int newScore)
    {
        // If File already exists, then load existing data first
        if (File.Exists(saveFilePath))
        {
            Load();
        }

        // BinaryFormatter and FileStream
        BinaryFormatter bf     = new BinaryFormatter();
        FileStream      stream = new FileStream(saveFilePath, FileMode.Create);
        // Convert raw data and add into list
        ScoreSaveData newData = new ScoreSaveData(newName, newScore);

        listOfScores.Add(newData);
        // Serialise into Binary File
        bf.Serialize(stream, listOfScores);

        // Close the stream
        stream.Close();
        return;
    }
 // -1 if second is smaller
 // 0 if equal
 // 1 if second is larger
 int CompareScore(ScoreSaveData first, ScoreSaveData second)
 {
     return(-(first.plyrScore.CompareTo(second.plyrScore)));
 }