示例#1
0
    /// <summary>
    /// Updates the stats that are currently stored
    /// </summary>
    private void RecordStats()
    {
        StopTimmer();
        //uses the recorded start time for the game to calculate how long the game tuck
        //removes the time between the game actually finishing and the the starts being recorded
        float currentGameTime = Time.time - startTime - Time.deltaTime;

        Difficulty gameDifficulty = GetComponent <GameManager>().GetCurrentGameDifficulty();

        StatType bestTimeInfo    = statsData.Stats.GetStatInfo(statNames.BestTime);
        StatType averageTimeInfo = statsData.Stats.GetStatInfo(statNames.AverageTime);
        StatType timesPlayedInfo = statsData.Stats.GetStatInfo(statNames.TimesPlayed);

        ////////////////////////////////////////Times PLayed///////////////////////////////////////////////////
        string timesPLayedKey = StatManager.GenerateStatKey(timesPlayedInfo, gameDifficulty);
        int    oldTimesPlayed = PlayerPrefs.GetInt(timesPLayedKey);

        //increases the num of times played
        PlayerPrefs.SetInt(timesPLayedKey, PlayerPrefs.GetInt(timesPLayedKey) + 1);
        Debug.Log("Updated times played to: " + PlayerPrefs.GetInt(timesPLayedKey));

        ////////////////////////////////////////Best time//////////////////////////////////////////////////////
        string bestKey = StatManager.GenerateStatKey(bestTimeInfo, gameDifficulty);
        float  OldBest = PlayerPrefs.GetFloat(bestKey);

        //updates if new time is better of there isn't a best time set
        if (currentGameTime < OldBest || OldBest == 0)
        {
            PlayerPrefs.SetFloat(bestKey, currentGameTime);
            Debug.Log("Updated best time to: " + PlayerPrefs.GetFloat(bestKey));
        }

        ////////////////////////////////////////Average time///////////////////////////////////////////////////
        string averageKey     = StatManager.GenerateStatKey(averageTimeInfo, gameDifficulty);
        float  oldAverageTime = PlayerPrefs.GetFloat(averageKey);

        //the old average hasn't been set yet
        if (oldAverageTime == 0)
        {
            PlayerPrefs.SetFloat(averageKey, currentGameTime);
        }
        else
        {
            float newAverageTime = ((oldAverageTime * oldTimesPlayed) + currentGameTime) / (oldTimesPlayed + 1);
            PlayerPrefs.SetFloat(averageKey, newAverageTime);
        }
        Debug.Log("Updated average time to: " + PlayerPrefs.GetFloat(averageKey));

        _endScreen.SetText(gameDifficulty, currentGameTime, numOfErrors);
    }