using UnityEngine; using System.Collections; using System.Text; public class PersistedGameDataManager : MonoBehaviour { #region Unity Singleton private static PersistedGameDataManager _instance; public static PersistedGameDataManager Instance { get { if (_instance == null) { _instance = FindObjectOfType(typeof(PersistedGameDataManager)) as PersistedGameDataManager; if (_instance == null) { AVDebug.LogError(string.Format("No gameObject with {0} component exists. Make sure to create a gameObject with {0} component", new System.Diagnostics.StackFrame().GetMethod().DeclaringType)); } } return _instance; } } void Awake() { if (_instance != null && _instance != this) { AVDebug.LogWarning(string.Format("{0} Instance already exists on another gameObject. Destroying this gameObject {1}", this.GetType().Name, gameObject.name)); Destroy(gameObject); return; } _instance = this; DontDestroyOnLoad(gameObject); Load(); RegisterListeners(); } void OnDestroy() { UnregisterListeners(); } #endregion int _totalGamesPlayed; int[] _totalCaught; int _spermicidalWavesFired; public int SpermicidalWavesFired { get { return _spermicidalWavesFired; } } void RegisterListeners() { NotificationCenter.AddListener(OnGameEnded, NotificationType.GameOver); NotificationCenter.AddListener(OnFiredSpermicidalWave, NotificationType.FireSpermicidalWave); DebugViewManager.OnDebugView += OnDebugView; CoreNotificationCenter.AddListener(OnResetData, CoreNotificationType.ResetData); } void UnregisterListeners() { NotificationCenter.RemoveListener(OnGameEnded, NotificationType.GameOver); NotificationCenter.RemoveListener(OnFiredSpermicidalWave, NotificationType.FireSpermicidalWave); DebugViewManager.OnDebugView -= OnDebugView; CoreNotificationCenter.RemoveListener(OnResetData, CoreNotificationType.ResetData); } void OnDebugView() { if (GUILayout.Button("Fake Persisted Game Data for Simulating Achievements")) { EnemyType[] enemyTypes = GetEnemyTypes(); foreach (EnemyType e in enemyTypes) { _totalCaught[(int)e] = 10000; } _spermicidalWavesFired = 10000; } if (GUILayout.Button("Reset Persisted Game Data")) { EnemyType[] enemyTypes = GetEnemyTypes(); foreach (EnemyType e in enemyTypes) { _totalCaught[(int)e] = 0; } _spermicidalWavesFired = 0; Save(); } } void OnGameEnded(Notification note) { ++_totalGamesPlayed; EnemyType[] enemyTypes = GetEnemyTypes(); foreach (EnemyType e in enemyTypes) { _totalCaught[(int)e] += GameDataManager.Instance.GetEnemyTypeCaught(e); } Save(); StringBuilder sb = new StringBuilder(); sb.AppendLine("Persisted Game Stats"); sb.AppendLine("===================="); sb.AppendLine("Total Games Played: "+_totalGamesPlayed); sb.AppendLine("Spermicidal Waves Fired: "+_spermicidalWavesFired); sb.AppendLine("Total Caught:"); foreach (EnemyType e in enemyTypes) { sb.AppendLine(string.Format("-{0}: {1}", e, GetTotalCaught(e))); } AVDebug.Log(sb.ToString()); } void OnFiredSpermicidalWave(Notification note) { ++_spermicidalWavesFired; } void OnResetData(CoreNotification note) { AVDebug.Log("Resetting Persisted Game Data"); PlayerPrefs.SetInt("totalGamesPlayed", 0); PlayerPrefs.SetInt("spermicidalWavesFired", 0); //Enemies caught EnemyType[] enemyTypes = GetEnemyTypes(); foreach (EnemyType e in enemyTypes) { PlayerPrefs.SetInt("total"+e.ToString()+"Caught", 0); } PlayerPrefs.Save(); Load(); } void Load() { _totalGamesPlayed = PlayerPrefs.GetInt("totalGamesPlayed", 0); _spermicidalWavesFired = PlayerPrefs.GetInt("spermicidalWavesFired", 0); //Enemies caught EnemyType[] enemyTypes = GetEnemyTypes(); _totalCaught = new int[enemyTypes.Length]; foreach (EnemyType e in enemyTypes) { _totalCaught[(int)e] = PlayerPrefs.GetInt("total"+e.ToString()+"Caught", 0); } } void Save() { PlayerPrefs.SetInt("totalGamesPlayed", _totalGamesPlayed); PlayerPrefs.SetInt("spermicidalWavesFired", _spermicidalWavesFired); //Enemies caught EnemyType[] enemyTypes = GetEnemyTypes(); foreach (EnemyType e in enemyTypes) { PlayerPrefs.SetInt("total"+e.ToString()+"Caught", _totalCaught[(int)e]); } } EnemyType[] GetEnemyTypes() { return (EnemyType[])System.Enum.GetValues(typeof(EnemyType)); } public int GetTotalCaught(EnemyType enemyType) { return _totalCaught[(int)enemyType]; } }