AchievementsManager.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. using Prime31;
  2. using UnityEngine;
  3. using System.Collections.Generic;
  4. public class AchievementsManager : MonoBehaviour
  5. {
  6. public UnlockedAchievementPopup unlockedAchievementPopupPrefab;
  7. public Transform popupAnchor;
  8. public int gotXAutomaticWeaponUpgradesThreshold = 3;
  9. public int gotXHomingWeaponUpgradesThreshold = 3;
  10. public int gotXRocketWeaponUpgradesThreshold = 3;
  11. public int gotXGiantWeaponUpgradesThreshold = 3;
  12. public int capturedATotalOfXGonorrheaThreshold = 50;
  13. public int capturedATotalOfXSyphilisThreshold = 50;
  14. public int capturedATotalOfXHPVThreshold = 50;
  15. public int capturedATotalOfXHIVThreshold = 50;
  16. public int capturedATotalOfXChlamydiaThreshold = 50;
  17. public int capturedATotalOfXHerpesThreshold = 50;
  18. public int usedSpermicidalWaveForCoupleOfTimesThreshold = 10;
  19. public int usedSpermicidalWaveForSomeTimesThreshold = 30;
  20. public int usedSpermicidalWaveForSeveralTimes = 50;
  21. public int destroyedCoupleOfSpermsWithSpermicidalWaveThreshold = 5;
  22. public int destroyedSomeSpermsWithSpermicidalWaveThreshold = 10;
  23. public int destroyedSeveralSpermsWithSpermicidalWaveThreshold = 15;
  24. public int destroyedXEnemiesWithRocketProjectileThreshold = 3;
  25. public int caughtXEnemiesWithGiantProjectileThreshold = 3;
  26. Dictionary<AchievementType, FaceBookAchievement> _registeredAchievements = new Dictionary<AchievementType, FaceBookAchievement>();
  27. List<AchievementType> _unlockedAchievements = new List<AchievementType>();
  28. List<AchievementType> _unlockedAchievementsInJustPlayedGame = new List<AchievementType>();
  29. UnlockedAchievementPopup _unlockedAchievementPopup;
  30. int _achievementUnlockedInLastPlayIndex;
  31. int _totalAchievementsUnlockedInLastPlay;
  32. void Awake()
  33. {
  34. //FacebookManager.sessionOpenedEvent += OnSessionOpened;
  35. NotificationCenter.AddListener(OnTransitToGameOver, NotificationType.TransitToGameOver);
  36. NotificationCenter.AddListener(OnContinueAfterGameOver, NotificationType.MenuTransition);
  37. NotificationCenter.AddListener(OnShowNextUnlockedAchievement, NotificationType.ShowNextUnlockedAchievement);
  38. CoreNotificationCenter.AddListener(OnResetData, CoreNotificationType.ResetData);
  39. DebugViewManager.OnDebugView += OnDebugView;
  40. //AVDebug.Log("Querying for registered achievements");
  41. //SocialManager.Instance.LoadRegisteredAchievements((isSuccessful, achievements) =>
  42. //{
  43. // if (isSuccessful)
  44. // {
  45. // AVDebug.Log("Got registered achievements: "+achievements.Length);
  46. // foreach(FaceBookAchievement achievement in achievements)
  47. // {
  48. // AchievementType achievementID = (AchievementType)System.Enum.Parse(typeof(AchievementType), achievement.AchievementID); //Enum.TryParse exists only in .Net 4
  49. // _registeredAchievements[achievementID] = achievement;
  50. // }
  51. // }
  52. //});
  53. //Populate unlockedAchievements with local data (if any)
  54. //This will help to not unnecessarily report to the player unlocking of achievements if the player is offline and he already got them when he was online,
  55. //This is to cover the edge case of when offline, we cannot get the current unlocked achievements from the server
  56. AchievementType[] achievementTypes = (AchievementType[])System.Enum.GetValues(typeof(AchievementType));
  57. foreach (AchievementType achievementType in achievementTypes)
  58. {
  59. if (PlayerPrefs.GetInt("Unlocked"+achievementType, 0) == 1)
  60. {
  61. _unlockedAchievements.Add(achievementType);
  62. }
  63. }
  64. }
  65. void OnDestroy()
  66. {
  67. //FacebookManager.sessionOpenedEvent -= OnSessionOpened;
  68. NotificationCenter.RemoveListener(OnTransitToGameOver, NotificationType.TransitToGameOver);
  69. NotificationCenter.RemoveListener(OnContinueAfterGameOver, NotificationType.MenuTransition);
  70. NotificationCenter.RemoveListener(OnShowNextUnlockedAchievement, NotificationType.ShowNextUnlockedAchievement);
  71. CoreNotificationCenter.RemoveListener(OnResetData, CoreNotificationType.ResetData);
  72. DebugViewManager.OnDebugView -= OnDebugView;
  73. }
  74. void OnResetData(CoreNotification note)
  75. {
  76. AVDebug.Log("Resetting unlocked achievements locally");
  77. AchievementType[] achievementTypes = (AchievementType[])System.Enum.GetValues(typeof(AchievementType));
  78. foreach (AchievementType achievementType in achievementTypes)
  79. {
  80. PlayerPrefs.SetInt("Unlocked"+achievementType, 0);
  81. }
  82. _unlockedAchievements.Clear();
  83. }
  84. void OnDebugView()
  85. {
  86. if (GUILayout.Button("Lock Back Achievements"))
  87. {
  88. SocialManager.Instance.LockAchievements( (str,obj) =>
  89. {
  90. _unlockedAchievements.Clear();
  91. AVDebug.Log("Lock Achievements completed");
  92. AVDebug.Log(str);
  93. });
  94. }
  95. }
  96. void OnSessionOpened()
  97. {
  98. if (SocialManager.Instance.isSessionValid)
  99. {
  100. AVDebug.Log("Querying for unlocked achievements");
  101. SocialManager.Instance.LoadAchievements((isSuccessful, achievements) =>
  102. {
  103. if (isSuccessful)
  104. {
  105. AVDebug.Log("Got unlocked achievements: "+achievements.Length);
  106. var sb = new System.Text.StringBuilder();
  107. foreach(FaceBookUnlockedAchievement achievement in achievements)
  108. {
  109. AVDebug.Log(achievement);
  110. sb.AppendLine("Unlocked achievement: "+achievement.AchievementID);
  111. AchievementType achievementType = (AchievementType)System.Enum.Parse(typeof(AchievementType), achievement.AchievementID); //Enum.TryParse exists only in .Net 4
  112. _unlockedAchievements.Add(achievementType);
  113. //Also save locally for future reference. This covers the edge case when the player deletes the game, and re-installs it, launches it, gets unlocked
  114. //achievements from previous install, quits, goes offline, relaunches, and plays it offline
  115. PlayerPrefs.SetInt("Unlocked"+achievementType,1);
  116. }
  117. PlayerPrefs.Save();
  118. AVDebug.Log(sb.ToString());
  119. }
  120. });
  121. }
  122. }
  123. void OnTransitToGameOver(Notification note)
  124. {
  125. GameOverData data = (GameOverData)note.data;
  126. //CheckForAchievements(data.reason);
  127. }
  128. void OnContinueAfterGameOver(Notification note)
  129. {
  130. MenuManager.MenuTransition t = (MenuManager.MenuTransition)note.data;
  131. if ((MenuManager.CurrentState == MenuManager.MenuState.ViewingTimeUp ||
  132. MenuManager.CurrentState == MenuManager.MenuState.ViewingVirusInfo) &&
  133. t == MenuManager.MenuTransition.Continue)
  134. {
  135. if (_unlockedAchievementsInJustPlayedGame.Count > 0)
  136. {
  137. _unlockedAchievementPopup = (UnlockedAchievementPopup)Instantiate(unlockedAchievementPopupPrefab);
  138. Transform popupTransform = _unlockedAchievementPopup.transform;
  139. Vector3 originalScale = popupTransform.localScale;
  140. popupTransform.parent = popupAnchor;
  141. popupTransform.localScale = originalScale;
  142. popupTransform.localPosition = unlockedAchievementPopupPrefab.transform.localPosition;
  143. _unlockedAchievementPopup.Show();
  144. _achievementUnlockedInLastPlayIndex = 0;
  145. _totalAchievementsUnlockedInLastPlay = _unlockedAchievementsInJustPlayedGame.Count;
  146. OnShowNextUnlockedAchievement(null);
  147. } else
  148. {
  149. NotificationCenter.Post(NotificationType.AchievementsPopupsFinished);
  150. }
  151. }
  152. }
  153. void OnShowNextUnlockedAchievement(Notification note)
  154. {
  155. if (_unlockedAchievementsInJustPlayedGame.Count > 0)
  156. {
  157. ++_achievementUnlockedInLastPlayIndex;
  158. AchievementType unlockedAchievement = _unlockedAchievementsInJustPlayedGame[0];
  159. _unlockedAchievementsInJustPlayedGame.RemoveAt(0);
  160. if (_registeredAchievements.ContainsKey(unlockedAchievement))
  161. {
  162. _unlockedAchievementPopup.SetData(_registeredAchievements[unlockedAchievement], _achievementUnlockedInLastPlayIndex, _totalAchievementsUnlockedInLastPlay);
  163. } else
  164. {
  165. AVDebug.LogError("Registered achievements does not have "+unlockedAchievement+". Registered achievements must have failed to load. Current registeredAchievements size is "+_registeredAchievements.Count);
  166. OnShowNextUnlockedAchievement(null); //Basically skip this and go to the next
  167. }
  168. } else
  169. {
  170. _unlockedAchievementPopup.Hide();
  171. _unlockedAchievementPopup = null;
  172. NotificationCenter.Post(NotificationType.AchievementsPopupsFinished);
  173. }
  174. }
  175. void UnlockAchievement(AchievementType achievementType)
  176. {
  177. if (_unlockedAchievements.Contains(achievementType))
  178. {
  179. AVDebug.Log("Ignore achievement "+achievementType+". Already unlocked");
  180. return;
  181. }
  182. AVDebug.Log("Unlocking achievement "+achievementType);
  183. SocialManager.Instance.UnlockAchievement(achievementType.ToString(), (isSuccessful, msg) =>
  184. {
  185. //Only save locally if we managed to do record it online
  186. PlayerPrefs.SetInt("Unlocked"+achievementType,1);
  187. PlayerPrefs.Save();
  188. AVDebug.Log("Unlock successful : " + isSuccessful);
  189. AVDebug.Log("Unlock msg : " + msg);
  190. });
  191. _unlockedAchievements.Add(achievementType);
  192. _unlockedAchievementsInJustPlayedGame.Add(achievementType);
  193. }
  194. void CheckForAchievements(GameOverData.Reason gameOverReason)
  195. {
  196. //Do not check for achievements if we are not logged in to facebook
  197. if (!FacebookHelper.Instance.IsLoggedIn())
  198. {
  199. return;
  200. }
  201. GameDataManager gameDataManager = GameDataManager.Instance;
  202. //GotAllWeaponUpgrades
  203. PickupType[] pickupTypes = (PickupType[])System.Enum.GetValues(typeof(PickupType));
  204. bool gotAllWeaponUpgrades = true;
  205. foreach (PickupType pickupType in pickupTypes)
  206. {
  207. if (pickupType != PickupType.DefaultWeapon && //ignore default
  208. gameDataManager.GetWeaponUpgradeTypePickedUp(pickupType) == 0)
  209. {
  210. gotAllWeaponUpgrades = false;
  211. break;
  212. }
  213. }
  214. if (gotAllWeaponUpgrades)
  215. {
  216. UnlockAchievement(AchievementType.GotAllWeaponUpgrades);
  217. }
  218. //GotXAutomaticWeaponUpgrades
  219. if (gameDataManager.GetWeaponUpgradeTypePickedUp(PickupType.Automatic) >= gotXAutomaticWeaponUpgradesThreshold)
  220. {
  221. UnlockAchievement(AchievementType.GotXAutomaticWeaponUpgrades);
  222. }
  223. //GotXHomingWeaponUpgrades
  224. if (gameDataManager.GetWeaponUpgradeTypePickedUp(PickupType.Homing) >= gotXHomingWeaponUpgradesThreshold)
  225. {
  226. UnlockAchievement(AchievementType.GotXHomingWeaponUpgrades);
  227. }
  228. //GotXRocketWeaponUpgrades
  229. if (gameDataManager.GetWeaponUpgradeTypePickedUp(PickupType.Rocket) >= gotXRocketWeaponUpgradesThreshold)
  230. {
  231. UnlockAchievement(AchievementType.GotXRocketWeaponUpgrades);
  232. }
  233. //GotXGiantWeaponUpgrades
  234. if (gameDataManager.GetWeaponUpgradeTypePickedUp(PickupType.GiantCondom) >= gotXGiantWeaponUpgradesThreshold)
  235. {
  236. UnlockAchievement(AchievementType.GotXGiantWeaponUpgrades);
  237. }
  238. //FinishedTimedModeWithoutCapturingASingleSperm
  239. if (gameOverReason == GameOverData.Reason.TimeUp &&
  240. gameDataManager.GetEnemyTypeCaught(EnemyType.standardSperm) == 0 &&
  241. gameDataManager.GetEnemyTypeCaught(EnemyType.bronzeSperm) == 0 &&
  242. gameDataManager.GetEnemyTypeCaught(EnemyType.silverSperm) == 0 &&
  243. gameDataManager.GetEnemyTypeCaught(EnemyType.goldSperm) == 0)
  244. {
  245. UnlockAchievement(AchievementType.FinishedTimedModeWithoutCapturingASingleSperm);
  246. }
  247. //CapturedAllSpecialSpermsInASinglePlay
  248. if (gameOverReason == GameOverData.Reason.TimeUp &&
  249. gameDataManager.SpecialSpermsGoneThrough == 0)
  250. {
  251. UnlockAchievement(AchievementType.CapturedAllSpecialSpermsInASinglePlay);
  252. }
  253. //CapturedAllSpermsAndEnemiesInASinglePlay
  254. //If the time is up, it is obvious that no virus passed through and so got captured
  255. if (gameOverReason == GameOverData.Reason.TimeUp &&
  256. gameDataManager.SpermsGoneThrough == 0)
  257. {
  258. UnlockAchievement(AchievementType.CapturedAllSpermsAndEnemiesInASinglePlay);
  259. }
  260. PersistedGameDataManager persistedGameDataManager = PersistedGameDataManager.Instance;
  261. //CapturedATotalOfXGonorrhea
  262. if (persistedGameDataManager.GetTotalCaught(EnemyType.gonorrhea) >= capturedATotalOfXGonorrheaThreshold)
  263. {
  264. UnlockAchievement(AchievementType.CapturedATotalOfXGonorrhea);
  265. }
  266. //CapturedATotalOfXSyphilis
  267. if (persistedGameDataManager.GetTotalCaught(EnemyType.syphilis) >= capturedATotalOfXSyphilisThreshold)
  268. {
  269. UnlockAchievement(AchievementType.CapturedATotalOfXSyphilis);
  270. }
  271. //CapturedATotalOfXHPV
  272. if (persistedGameDataManager.GetTotalCaught(EnemyType.hpv) >= capturedATotalOfXHPVThreshold)
  273. {
  274. UnlockAchievement(AchievementType.CapturedATotalOfXHPV);
  275. }
  276. //CapturedATotalOfXHIV
  277. if (persistedGameDataManager.GetTotalCaught(EnemyType.hiv) >= capturedATotalOfXHIVThreshold)
  278. {
  279. UnlockAchievement(AchievementType.CapturedATotalOfXHIV);
  280. }
  281. //CapturedATotalOfXChlamydia
  282. if (persistedGameDataManager.GetTotalCaught(EnemyType.chlamydia) >= capturedATotalOfXChlamydiaThreshold)
  283. {
  284. UnlockAchievement(AchievementType.CapturedATotalOfXChlamydia);
  285. }
  286. //CapturedATotalOfXHerpes
  287. if (persistedGameDataManager.GetTotalCaught(EnemyType.herpes) >= capturedATotalOfXHerpesThreshold)
  288. {
  289. UnlockAchievement(AchievementType.CapturedATotalOfXHerpes);
  290. }
  291. //UsedSpermicidalWaveForCoupleOfTimes
  292. if (persistedGameDataManager.SpermicidalWavesFired >= usedSpermicidalWaveForCoupleOfTimesThreshold)
  293. {
  294. UnlockAchievement(AchievementType.UsedSpermicidalWaveForCoupleOfTimes);
  295. }
  296. //UsedSpermicidalWaveForSomeTimes
  297. if (persistedGameDataManager.SpermicidalWavesFired >= usedSpermicidalWaveForSomeTimesThreshold)
  298. {
  299. UnlockAchievement(AchievementType.UsedSpermicidalWaveForSomeTimes);
  300. }
  301. //UsedSpermicidalWaveForSeveralTimes
  302. if (persistedGameDataManager.SpermicidalWavesFired >= usedSpermicidalWaveForSeveralTimes)
  303. {
  304. UnlockAchievement(AchievementType.UsedSpermicidalWaveForSeveralTimes);
  305. }
  306. //DestroyedCoupleOfSpermsWithSpermicidalWave
  307. if (gameDataManager.BiggestCountOfSpermsHitBySpermicidalWave >= destroyedCoupleOfSpermsWithSpermicidalWaveThreshold)
  308. {
  309. UnlockAchievement(AchievementType.DestroyedCoupleOfSpermsWithSpermicidalWave);
  310. }
  311. //DestroyedSomeSpermsWithSpermicidalWave
  312. if (gameDataManager.BiggestCountOfSpermsHitBySpermicidalWave >= destroyedSomeSpermsWithSpermicidalWaveThreshold)
  313. {
  314. UnlockAchievement(AchievementType.DestroyedSomeSpermsWithSpermicidalWave);
  315. }
  316. //DestroyedSeveralSpermsWithSpermicidalWave
  317. if (gameDataManager.BiggestCountOfSpermsHitBySpermicidalWave >= destroyedSeveralSpermsWithSpermicidalWaveThreshold)
  318. {
  319. UnlockAchievement(AchievementType.DestroyedSeveralSpermsWithSpermicidalWave);
  320. }
  321. //DestroyedXEnemiesWithRocketProjectile
  322. if (gameDataManager.BiggestCountOfEnemiesHitByRocket >= destroyedXEnemiesWithRocketProjectileThreshold)
  323. {
  324. UnlockAchievement(AchievementType.DestroyedXEnemiesWithRocketProjectile);
  325. }
  326. //CaughtXEnemiesWithGiantProjectile
  327. if (gameDataManager.BiggestCountOfEnemiesCaughtByGiantCondom >= caughtXEnemiesWithGiantProjectileThreshold)
  328. {
  329. UnlockAchievement(AchievementType.CaughtXEnemiesWithGiantProjectile);
  330. }
  331. }
  332. }