|
@@ -0,0 +1,155 @@
|
|
|
+using System.Collections;
|
|
|
+using System.Collections.Generic;
|
|
|
+using UnityEngine;
|
|
|
+using KairoEngine.Core;
|
|
|
+using Sirenix.OdinInspector;
|
|
|
+
|
|
|
+namespace KairoEngine.Statistics
|
|
|
+{
|
|
|
+ [HideMonoScript]
|
|
|
+ public class Statistics : SerializedMonoBehaviour
|
|
|
+ {
|
|
|
+ #region Singleton
|
|
|
+ private static Statistics statistics;
|
|
|
+ public static Statistics instance
|
|
|
+ {
|
|
|
+ get {
|
|
|
+ if(!statistics)
|
|
|
+ {
|
|
|
+ statistics = FindObjectOfType (typeof(Statistics)) as Statistics;
|
|
|
+ if(!statistics)
|
|
|
+ {
|
|
|
+ Debug.LogError("There need to one active Statistics script on the scene.");
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return statistics;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ #endregion
|
|
|
+
|
|
|
+ public bool listenForStatisticsEvents = true;
|
|
|
+ public string setStatisticEvent = "SetStatisticData";
|
|
|
+ [InlineEditor(InlineEditorObjectFieldModes.Boxed)] public StatisticsLibrary db;
|
|
|
+
|
|
|
+ void Awake()
|
|
|
+ {
|
|
|
+ if(instance != null && instance != this)
|
|
|
+ {
|
|
|
+ Destroy(this.gameObject);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ void Start()
|
|
|
+ {
|
|
|
+ if(db == null)
|
|
|
+ {
|
|
|
+ Debug.LogError("Statistics component is missing the StatisticsList.\nPlease configure the statistics module in the Game config file.");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ LoadStatistics();
|
|
|
+ if(!listenForStatisticsEvents) return;
|
|
|
+ GenericEvents.StartListening(setStatisticEvent, SetStatisticInt);
|
|
|
+ GenericEvents.StartListeningForStringFloat(setStatisticEvent, SetStatisticFloat);
|
|
|
+ GenericEvents.StartListening(setStatisticEvent, SetStatisticString);
|
|
|
+ }
|
|
|
+
|
|
|
+ void OnDestroy()
|
|
|
+ {
|
|
|
+ SaveStatistics();
|
|
|
+ if(!listenForStatisticsEvents) return;
|
|
|
+ GenericEvents.StopListening(setStatisticEvent, SetStatisticInt);
|
|
|
+ GenericEvents.StopListeningForStringFloat(setStatisticEvent, SetStatisticFloat);
|
|
|
+ GenericEvents.StopListening(setStatisticEvent, SetStatisticString);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static StatisticData GetData(string title)
|
|
|
+ {
|
|
|
+ if(instance == null) return null;
|
|
|
+ if(instance.db == null) return null;
|
|
|
+ for (int i = 0; i < instance.db.data.Count; i++)
|
|
|
+ {
|
|
|
+ if(instance.db.data[i].title == title) return instance.db.data[i];
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void LoadStatistics()
|
|
|
+ {
|
|
|
+ if(instance == null) return;
|
|
|
+ if(instance.db == null) return;
|
|
|
+ for (int i = 0; i < instance.db.data.Count; i++)
|
|
|
+ {
|
|
|
+ StatisticData stat = instance.db.data[i];
|
|
|
+ if(stat.persistent == false) continue;
|
|
|
+ switch (stat.category)
|
|
|
+ {
|
|
|
+ case StatisticType.time:
|
|
|
+ float time = PlayerPrefs.GetFloat(stat.title, 0f);
|
|
|
+ stat.SetTime(time);
|
|
|
+ break;
|
|
|
+ case StatisticType.integer:
|
|
|
+ int integer = PlayerPrefs.GetInt(stat.title, 0);
|
|
|
+ stat.SetInteger(integer);
|
|
|
+ break;
|
|
|
+ case StatisticType.text:
|
|
|
+ string text = PlayerPrefs.GetString(stat.title, "");
|
|
|
+ stat.SetText(text);
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void SaveStatistics()
|
|
|
+ {
|
|
|
+ if(instance == null) return;
|
|
|
+ if(instance.db == null) return;
|
|
|
+ for (int i = 0; i < instance.db.data.Count; i++)
|
|
|
+ {
|
|
|
+ StatisticData stat = instance.db.data[i];
|
|
|
+ if(stat.persistent == false) continue;
|
|
|
+ switch (stat.category)
|
|
|
+ {
|
|
|
+ case StatisticType.time:
|
|
|
+ PlayerPrefs.SetFloat(stat.title, stat.GetTime());
|
|
|
+ break;
|
|
|
+ case StatisticType.integer:
|
|
|
+ PlayerPrefs.SetInt(stat.title, stat.GetInteger());
|
|
|
+ break;
|
|
|
+ case StatisticType.text:
|
|
|
+ PlayerPrefs.SetString(stat.title, stat.GetText());
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void SetStatisticInt(string statName, int newValue)
|
|
|
+ {
|
|
|
+ StatisticData statisticData = GetData(statName);
|
|
|
+ if(statisticData == null) return;
|
|
|
+ if(statisticData.category != StatisticType.integer) return;
|
|
|
+ statisticData.SetInteger(newValue);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void SetStatisticFloat(string statName, float newValue)
|
|
|
+ {
|
|
|
+ StatisticData statisticData = GetData(statName);
|
|
|
+ if(statisticData == null) return;
|
|
|
+ if(statisticData.category != StatisticType.time) return;
|
|
|
+ statisticData.SetTime(newValue);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void SetStatisticString(string statName, string newValue)
|
|
|
+ {
|
|
|
+ StatisticData statisticData = GetData(statName);
|
|
|
+ if(statisticData == null) return;
|
|
|
+ if(statisticData.category != StatisticType.text) return;
|
|
|
+ statisticData.SetText(newValue);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|