internal void SetData(StatType stat, Difficulty SelectedDifficulty) { _label.text = stat.Label + ":"; if (stat.DataType == statTypesEnum.Int) { _data.text = PlayerPrefs.GetInt(StatManager.GenerateStatKey(stat, SelectedDifficulty)).ToString(); } else if (stat.DataType == statTypesEnum.Float) { _data.text = StatManager.FormatTime(PlayerPrefs.GetFloat(StatManager.GenerateStatKey(stat, SelectedDifficulty))); } }
/// <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); }
private void InitialiseStatPlayerPrefs() { StatsData statsData = new StatsData(); foreach (StatType stat in statsData.Stats.StatTypes) { foreach (Mode mode in statsData.Stats.Modes) { string key = StatManager.GenerateStatKey(stat, mode.ModeName); if (!PlayerPrefs.HasKey(key)) { if (stat.DataType == statTypesEnum.Int) { PlayerPrefs.SetInt(key, statsData.Stats.GetStatDefualt(statTypesEnum.Int).Value); } else if (stat.DataType == statTypesEnum.Float) { PlayerPrefs.SetFloat(key, statsData.Stats.GetStatDefualt(statTypesEnum.Float).Value); } } } } }