GameDataManager.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Text;
  4. /// <summary>
  5. /// This is the data collected during a game. It will not be persisted.
  6. /// The data is reset on every game played.
  7. /// </summary>
  8. public class GameDataManager : MonoBehaviour
  9. {
  10. #region Unity Singleton
  11. private static GameDataManager _instance;
  12. public static GameDataManager Instance
  13. {
  14. get
  15. {
  16. if (_instance == null)
  17. {
  18. _instance = FindObjectOfType(typeof(GameDataManager)) as GameDataManager;
  19. if (_instance == null)
  20. {
  21. 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));
  22. }
  23. }
  24. return _instance;
  25. }
  26. }
  27. void Awake()
  28. {
  29. if (_instance != null && _instance != this)
  30. {
  31. AVDebug.LogWarning(string.Format("{0} Instance already exists on another gameObject. Destroying this gameObject {1}", this.GetType().Name, gameObject.name));
  32. Destroy(gameObject);
  33. return;
  34. }
  35. _instance = this;
  36. DontDestroyOnLoad(gameObject);
  37. RegisterListeners();
  38. }
  39. void OnDestroy()
  40. {
  41. UnregisterListeners();
  42. }
  43. #endregion
  44. int _score;
  45. public int Score { get { return _score; } }
  46. int[][] _enemiesCaughtWithWeapon;
  47. int[] _weaponUpgradesPickedUp;
  48. int[] _enemiesGoneThrough;
  49. public bool HasStartedSpawningEnemies;
  50. const string bestScoreKeyPrefix = "bestScore_";
  51. public int SpecialSpermsGoneThrough
  52. {
  53. get
  54. {
  55. return
  56. GetEnemyTypeGoneThrough(EnemyType.bronzeSperm) +
  57. GetEnemyTypeGoneThrough(EnemyType.silverSperm) +
  58. GetEnemyTypeGoneThrough(EnemyType.goldSperm);
  59. }
  60. }
  61. public int SpermsGoneThrough
  62. {
  63. get
  64. {
  65. return SpecialSpermsGoneThrough +
  66. GetEnemyTypeGoneThrough(EnemyType.standardSperm);
  67. }
  68. }
  69. float _timePassed;
  70. public float TimePassed { get { return _timePassed; } }
  71. int _biggestCountOfSpermsHitBySpermicidalWave;
  72. public int BiggestCountOfSpermsHitBySpermicidalWave { get { return _biggestCountOfSpermsHitBySpermicidalWave; } }
  73. int _spermsHitInCurrentSpermicidalWave;
  74. int _biggestCountOfEnemiesHitByRocket;
  75. public int BiggestCountOfEnemiesHitByRocket { get { return _biggestCountOfEnemiesHitByRocket; } }
  76. int _biggestCountOfEnemiesCaughtByGiantCondom;
  77. public int BiggestCountOfEnemiesCaughtByGiantCondom { get { return _biggestCountOfEnemiesCaughtByGiantCondom; } }
  78. #region Unity Callbacks
  79. void RegisterListeners()
  80. {
  81. NotificationCenter.AddListener(OnGameStart, NotificationType.GameStart);
  82. NotificationCenter.AddListener(OnGameOver, NotificationType.GameOver);
  83. NotificationCenter.AddListener(OnAddToScore, NotificationType.AddToScore);
  84. NotificationCenter.AddListener(OnHitSomething, NotificationType.HitSomething);
  85. NotificationCenter.AddListener(OnPickedUpWeaponUpgrade, NotificationType.PickedUpWeaponUpgrade);
  86. NotificationCenter.AddListener(OnEnemyGoneThrough, NotificationType.EnemyGoneThrough);
  87. NotificationCenter.AddListener(OnFiredSpermidicalWave, NotificationType.FireSpermicidalWave);
  88. NotificationCenter.AddListener(OnSpermHitBySpermicidalWave, NotificationType.SpermHitBySpermicidalWave);
  89. NotificationCenter.AddListener(OnRocketHitEnemies, NotificationType.RocketHitEnemies);
  90. NotificationCenter.AddListener(OnGiantCondomCaughtEnemies, NotificationType.GiantCondomCaughtEnemies);
  91. DebugViewManager.OnDebugView += OnDebugView;
  92. CoreNotificationCenter.AddListener(OnResetData, CoreNotificationType.ResetData);
  93. NotificationCenter.AddListener(OnStartSpawningEnemies, NotificationType.StartSpawningEnemies);
  94. }
  95. void UnregisterListeners()
  96. {
  97. NotificationCenter.RemoveListener(OnGameStart, NotificationType.GameStart);
  98. NotificationCenter.RemoveListener(OnGameOver, NotificationType.GameOver);
  99. NotificationCenter.RemoveListener(OnAddToScore, NotificationType.AddToScore);
  100. NotificationCenter.RemoveListener(OnHitSomething, NotificationType.HitSomething);
  101. NotificationCenter.RemoveListener(OnPickedUpWeaponUpgrade, NotificationType.PickedUpWeaponUpgrade);
  102. NotificationCenter.RemoveListener(OnEnemyGoneThrough, NotificationType.EnemyGoneThrough);
  103. NotificationCenter.RemoveListener(OnFiredSpermidicalWave, NotificationType.FireSpermicidalWave);
  104. NotificationCenter.RemoveListener(OnSpermHitBySpermicidalWave, NotificationType.SpermHitBySpermicidalWave);
  105. NotificationCenter.RemoveListener(OnRocketHitEnemies, NotificationType.RocketHitEnemies);
  106. NotificationCenter.RemoveListener(OnGiantCondomCaughtEnemies, NotificationType.GiantCondomCaughtEnemies);
  107. DebugViewManager.OnDebugView -= OnDebugView;
  108. CoreNotificationCenter.RemoveListener(OnResetData, CoreNotificationType.ResetData);
  109. NotificationCenter.RemoveListener(OnStartSpawningEnemies, NotificationType.StartSpawningEnemies);
  110. }
  111. void Update()
  112. {
  113. if (HasStartedSpawningEnemies && GameUpdateManager.Instance._isPlaying)
  114. {
  115. _timePassed += Time.deltaTime;
  116. }
  117. else
  118. {
  119. //Debug.Log(_timePassed);
  120. }
  121. }
  122. #endregion
  123. void OnDebugView()
  124. {
  125. if (GUILayout.Button("Fake Game Data for Simulating Achievements"))
  126. {
  127. EnemyType[] enemyTypes = (EnemyType[])System.Enum.GetValues(typeof(EnemyType));
  128. foreach (EnemyType e in enemyTypes)
  129. {
  130. _enemiesCaughtWithWeapon[(int)e][(int)PickupType.DefaultWeapon] = 100;
  131. _enemiesGoneThrough[(int)e] = 0;
  132. }
  133. PickupType[] weaponTypes = (PickupType[])System.Enum.GetValues(typeof(PickupType));
  134. foreach (PickupType w in weaponTypes)
  135. {
  136. _weaponUpgradesPickedUp[(int)w] = 100;
  137. }
  138. _timePassed = 100 * 60;
  139. _biggestCountOfEnemiesCaughtByGiantCondom = 100;
  140. _biggestCountOfEnemiesHitByRocket = 100;
  141. _biggestCountOfSpermsHitBySpermicidalWave = 100;
  142. }
  143. }
  144. #region Notifications
  145. void OnGameStart(Notification note)
  146. {
  147. ResetData();
  148. NotificationCenter.Post(NotificationType.UpdateScoreLabel);
  149. }
  150. void OnStartSpawningEnemies(Notification note)
  151. {
  152. HasStartedSpawningEnemies = true;
  153. }
  154. void OnGameOver(Notification note)
  155. {
  156. //Output the stats
  157. int levelIndex = LevelsManager.Instance.CurrentLevelIndex;
  158. StringBuilder sb = new StringBuilder();
  159. sb.AppendLine("Game Stats");
  160. sb.AppendLine("==========");
  161. sb.AppendLine("Level:"+levelIndex);
  162. sb.AppendLine("Score: "+_score);
  163. sb.AppendLine("Time: "+_timePassed);
  164. sb.AppendLine("Biggest Spermicidal Wave: " + _biggestCountOfSpermsHitBySpermicidalWave);
  165. sb.AppendLine("Biggest Rocket hit: " + _biggestCountOfEnemiesHitByRocket);
  166. sb.AppendLine("Biggest Giant Condom catch: " + _biggestCountOfEnemiesCaughtByGiantCondom);
  167. sb.AppendLine("Enemies Caught/PassedThrough:");
  168. EnemyType[] enemyTypes = (EnemyType[])System.Enum.GetValues(typeof(EnemyType));
  169. foreach (EnemyType e in enemyTypes)
  170. {
  171. sb.AppendLine(string.Format("-{0}: {1}/{2}", e, GetEnemyTypeCaught(e), GetEnemyTypeGoneThrough(e)));
  172. }
  173. sb.AppendLine("Weapon Upgrades picked up");
  174. PickupType[] weaponTypes = (PickupType[])System.Enum.GetValues(typeof(PickupType));;
  175. foreach (PickupType w in weaponTypes)
  176. {
  177. sb.AppendLine(string.Format("-{0}: {1}", w, GetWeaponUpgradeTypePickedUp(w)));
  178. }
  179. Logger.LogFlurryEvent("Game Stats", "summary", sb.ToString());
  180. if (LevelsManager.Instance.CurrentLevel.AreAllObjectivesMet())
  181. {
  182. Logger.LogFlurryEvent("Level completed", "level", LevelsManager.Instance.CurrentLevelIndex.ToString());
  183. }
  184. AVDebug.Log(sb.ToString());
  185. if (_score > GetBestScoreForCurrentLevel())
  186. {
  187. PlayerPrefs.SetInt(bestScoreKeyPrefix+levelIndex, _score);
  188. }
  189. }
  190. void OnResetData(CoreNotification note)
  191. {
  192. AVDebug.Log("Resetting Game Data");
  193. ResetData();
  194. }
  195. void OnAddToScore(Notification note)
  196. {
  197. ScoreChangeData data = (ScoreChangeData)note.data;
  198. _score += data.scoreDelta;
  199. NotificationCenter.Post(NotificationType.UpdateScoreLabel);
  200. }
  201. void OnHitSomething(Notification note)
  202. {
  203. HitEvent hitEvent = (HitEvent)note.data;
  204. Enemy enemy = hitEvent.hitObject.GetComponent<Enemy>();
  205. int enemyType = (int)enemy.type;
  206. ++_enemiesCaughtWithWeapon[enemyType][(int)hitEvent.projectile.type];
  207. ++_enemiesCaughtWithWeapon[enemyType][(int)PickupType.AnyWeapon];
  208. ++_enemiesCaughtWithWeapon[(int)EnemyType.anyEnemy][(int)PickupType.AnyWeapon];
  209. ++_enemiesCaughtWithWeapon[(int)EnemyType.anyEnemy][(int)hitEvent.projectile.type];
  210. //if it's a sperm
  211. if (enemyType >= (int)EnemyType.standardSperm &&
  212. enemyType <= (int)EnemyType.goldSperm)
  213. {
  214. ++_enemiesCaughtWithWeapon[(int)EnemyType.anySperm][(int)PickupType.AnyWeapon];
  215. ++_enemiesCaughtWithWeapon[(int)EnemyType.anySperm][(int)hitEvent.projectile.type];
  216. }
  217. //if it's a virus
  218. if (enemyType < (int)EnemyType.standardSperm)
  219. {
  220. ++_enemiesCaughtWithWeapon[(int)EnemyType.anyVirus][(int)PickupType.AnyWeapon];
  221. ++_enemiesCaughtWithWeapon[(int)EnemyType.anyVirus][(int)hitEvent.projectile.type];
  222. }
  223. }
  224. void OnPickedUpWeaponUpgrade(Notification note)
  225. {
  226. PickupType pickupType = (PickupType)note.data;
  227. ++_weaponUpgradesPickedUp[(int)pickupType];
  228. }
  229. void OnEnemyGoneThrough(Notification note)
  230. {
  231. EnemyType enemyType = (EnemyType)note.data;
  232. //AVDebug.Log("enemyType passed through: "+enemyType);
  233. ++_enemiesGoneThrough[(int)enemyType];
  234. Logger.LogFlurryEvent("Enemy Gone Through", "Virus", enemyType.ToString(), "Level", LevelsManager.Instance.CurrentLevelIndex.ToString());
  235. }
  236. void OnFiredSpermidicalWave(Notification note)
  237. {
  238. _spermsHitInCurrentSpermicidalWave = 0;
  239. }
  240. void OnSpermHitBySpermicidalWave(Notification note)
  241. {
  242. Enemy enemy = (Enemy)note.data;
  243. ++_enemiesCaughtWithWeapon[(int)enemy.type][(int)PickupType.SpermicidalWave];
  244. ++_spermsHitInCurrentSpermicidalWave;
  245. if (_spermsHitInCurrentSpermicidalWave > _biggestCountOfSpermsHitBySpermicidalWave)
  246. {
  247. _biggestCountOfSpermsHitBySpermicidalWave = _spermsHitInCurrentSpermicidalWave;
  248. }
  249. }
  250. void OnRocketHitEnemies(Notification note)
  251. {
  252. int enemiesHit = (int)note.data;
  253. if (enemiesHit > _biggestCountOfEnemiesHitByRocket)
  254. {
  255. _biggestCountOfEnemiesHitByRocket = enemiesHit;
  256. }
  257. }
  258. void OnGiantCondomCaughtEnemies(Notification note)
  259. {
  260. int enemiesCaught = (int)note.data;
  261. if (enemiesCaught > _biggestCountOfEnemiesCaughtByGiantCondom)
  262. {
  263. _biggestCountOfEnemiesCaughtByGiantCondom = enemiesCaught;
  264. }
  265. }
  266. #endregion
  267. void ResetData()
  268. {
  269. _score = 0;
  270. _timePassed = 0;
  271. HasStartedSpawningEnemies = false;
  272. _biggestCountOfSpermsHitBySpermicidalWave = 0;
  273. _spermsHitInCurrentSpermicidalWave = 0;
  274. _biggestCountOfEnemiesHitByRocket = 0;
  275. _biggestCountOfEnemiesCaughtByGiantCondom = 0;
  276. if (_enemiesCaughtWithWeapon == null)
  277. {
  278. int numOfEnemyTypes = System.Enum.GetNames(typeof(EnemyType)).Length;
  279. _enemiesGoneThrough = new int[numOfEnemyTypes];
  280. int numOfWeaponUpgrades = System.Enum.GetNames(typeof(PickupType)).Length;
  281. _weaponUpgradesPickedUp = new int[numOfWeaponUpgrades];
  282. _enemiesCaughtWithWeapon = new int[numOfEnemyTypes][];
  283. for (int i = 0; i < numOfEnemyTypes; i++)
  284. {
  285. _enemiesCaughtWithWeapon[i] = new int[numOfWeaponUpgrades];
  286. }
  287. } else
  288. {
  289. for (int i = 0; i < _enemiesCaughtWithWeapon.Length; i++)
  290. {
  291. for (int j = 0; j < _enemiesCaughtWithWeapon[i].Length; j++)
  292. {
  293. _enemiesCaughtWithWeapon[i][j] = 0;
  294. }
  295. _enemiesGoneThrough[i] = 0;
  296. }
  297. for (int i = 0; i < _weaponUpgradesPickedUp.Length; i++)
  298. {
  299. _weaponUpgradesPickedUp[i] = 0;
  300. }
  301. }
  302. }
  303. public int GetEnemyTypeCaught(EnemyType enemyType)
  304. {
  305. return _enemiesCaughtWithWeapon[(int)enemyType][(int)PickupType.AnyWeapon];
  306. }
  307. public int GetEnemyTypeCaughtWithWeapon(EnemyType enemyType, PickupType weaponType)
  308. {
  309. return _enemiesCaughtWithWeapon[(int)enemyType][(int)weaponType];
  310. }
  311. public int GetEnemyTypeGoneThrough(EnemyType enemyType)
  312. {
  313. return _enemiesGoneThrough[(int)enemyType];
  314. }
  315. public int GetWeaponUpgradeTypePickedUp(PickupType pickupType)
  316. {
  317. return _weaponUpgradesPickedUp[(int)pickupType];
  318. }
  319. public int GetBestScoreForCurrentLevel()
  320. {
  321. return PlayerPrefs.GetInt(bestScoreKeyPrefix+LevelsManager.Instance.CurrentLevelIndex, 0);
  322. }
  323. }