PersistedGameDataManager.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Text;
  4. public class PersistedGameDataManager : MonoBehaviour
  5. {
  6. #region Unity Singleton
  7. private static PersistedGameDataManager _instance;
  8. public static PersistedGameDataManager Instance
  9. {
  10. get
  11. {
  12. if (_instance == null)
  13. {
  14. _instance = FindObjectOfType(typeof(PersistedGameDataManager)) as PersistedGameDataManager;
  15. if (_instance == null)
  16. {
  17. 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));
  18. }
  19. }
  20. return _instance;
  21. }
  22. }
  23. void Awake()
  24. {
  25. if (_instance != null && _instance != this)
  26. {
  27. AVDebug.LogWarning(string.Format("{0} Instance already exists on another gameObject. Destroying this gameObject {1}", this.GetType().Name, gameObject.name));
  28. Destroy(gameObject);
  29. return;
  30. }
  31. _instance = this;
  32. DontDestroyOnLoad(gameObject);
  33. Load();
  34. RegisterListeners();
  35. }
  36. void OnDestroy()
  37. {
  38. UnregisterListeners();
  39. }
  40. #endregion
  41. int _totalGamesPlayed;
  42. int[] _totalCaught;
  43. int _spermicidalWavesFired;
  44. public int SpermicidalWavesFired { get { return _spermicidalWavesFired; } }
  45. void RegisterListeners()
  46. {
  47. NotificationCenter.AddListener(OnGameEnded, NotificationType.GameOver);
  48. NotificationCenter.AddListener(OnFiredSpermicidalWave, NotificationType.FireSpermicidalWave);
  49. DebugViewManager.OnDebugView += OnDebugView;
  50. CoreNotificationCenter.AddListener(OnResetData, CoreNotificationType.ResetData);
  51. }
  52. void UnregisterListeners()
  53. {
  54. NotificationCenter.RemoveListener(OnGameEnded, NotificationType.GameOver);
  55. NotificationCenter.RemoveListener(OnFiredSpermicidalWave, NotificationType.FireSpermicidalWave);
  56. DebugViewManager.OnDebugView -= OnDebugView;
  57. CoreNotificationCenter.RemoveListener(OnResetData, CoreNotificationType.ResetData);
  58. }
  59. void OnDebugView()
  60. {
  61. if (GUILayout.Button("Fake Persisted Game Data for Simulating Achievements"))
  62. {
  63. EnemyType[] enemyTypes = GetEnemyTypes();
  64. foreach (EnemyType e in enemyTypes)
  65. {
  66. _totalCaught[(int)e] = 10000;
  67. }
  68. _spermicidalWavesFired = 10000;
  69. }
  70. if (GUILayout.Button("Reset Persisted Game Data"))
  71. {
  72. EnemyType[] enemyTypes = GetEnemyTypes();
  73. foreach (EnemyType e in enemyTypes)
  74. {
  75. _totalCaught[(int)e] = 0;
  76. }
  77. _spermicidalWavesFired = 0;
  78. Save();
  79. }
  80. }
  81. void OnGameEnded(Notification note)
  82. {
  83. ++_totalGamesPlayed;
  84. EnemyType[] enemyTypes = GetEnemyTypes();
  85. foreach (EnemyType e in enemyTypes)
  86. {
  87. _totalCaught[(int)e] += GameDataManager.Instance.GetEnemyTypeCaught(e);
  88. }
  89. Save();
  90. StringBuilder sb = new StringBuilder();
  91. sb.AppendLine("Persisted Game Stats");
  92. sb.AppendLine("====================");
  93. sb.AppendLine("Total Games Played: "+_totalGamesPlayed);
  94. sb.AppendLine("Spermicidal Waves Fired: "+_spermicidalWavesFired);
  95. sb.AppendLine("Total Caught:");
  96. foreach (EnemyType e in enemyTypes)
  97. {
  98. sb.AppendLine(string.Format("-{0}: {1}", e, GetTotalCaught(e)));
  99. }
  100. AVDebug.Log(sb.ToString());
  101. }
  102. void OnFiredSpermicidalWave(Notification note)
  103. {
  104. ++_spermicidalWavesFired;
  105. }
  106. void OnResetData(CoreNotification note)
  107. {
  108. AVDebug.Log("Resetting Persisted Game Data");
  109. PlayerPrefs.SetInt("totalGamesPlayed", 0);
  110. PlayerPrefs.SetInt("spermicidalWavesFired", 0);
  111. //Enemies caught
  112. EnemyType[] enemyTypes = GetEnemyTypes();
  113. foreach (EnemyType e in enemyTypes)
  114. {
  115. PlayerPrefs.SetInt("total"+e.ToString()+"Caught", 0);
  116. }
  117. PlayerPrefs.Save();
  118. Load();
  119. }
  120. void Load()
  121. {
  122. _totalGamesPlayed = PlayerPrefs.GetInt("totalGamesPlayed", 0);
  123. _spermicidalWavesFired = PlayerPrefs.GetInt("spermicidalWavesFired", 0);
  124. //Enemies caught
  125. EnemyType[] enemyTypes = GetEnemyTypes();
  126. _totalCaught = new int[enemyTypes.Length];
  127. foreach (EnemyType e in enemyTypes)
  128. {
  129. _totalCaught[(int)e] = PlayerPrefs.GetInt("total"+e.ToString()+"Caught", 0);
  130. }
  131. }
  132. void Save()
  133. {
  134. PlayerPrefs.SetInt("totalGamesPlayed", _totalGamesPlayed);
  135. PlayerPrefs.SetInt("spermicidalWavesFired", _spermicidalWavesFired);
  136. //Enemies caught
  137. EnemyType[] enemyTypes = GetEnemyTypes();
  138. foreach (EnemyType e in enemyTypes)
  139. {
  140. PlayerPrefs.SetInt("total"+e.ToString()+"Caught", _totalCaught[(int)e]);
  141. }
  142. }
  143. EnemyType[] GetEnemyTypes()
  144. {
  145. return (EnemyType[])System.Enum.GetValues(typeof(EnemyType));
  146. }
  147. public int GetTotalCaught(EnemyType enemyType)
  148. {
  149. return _totalCaught[(int)enemyType];
  150. }
  151. }