using Prime31; using UnityEngine; using System.Collections.Generic; public class AchievementsManager : MonoBehaviour { public UnlockedAchievementPopup unlockedAchievementPopupPrefab; public Transform popupAnchor; public int gotXAutomaticWeaponUpgradesThreshold = 3; public int gotXHomingWeaponUpgradesThreshold = 3; public int gotXRocketWeaponUpgradesThreshold = 3; public int gotXGiantWeaponUpgradesThreshold = 3; public int capturedATotalOfXGonorrheaThreshold = 50; public int capturedATotalOfXSyphilisThreshold = 50; public int capturedATotalOfXHPVThreshold = 50; public int capturedATotalOfXHIVThreshold = 50; public int capturedATotalOfXChlamydiaThreshold = 50; public int capturedATotalOfXHerpesThreshold = 50; public int usedSpermicidalWaveForCoupleOfTimesThreshold = 10; public int usedSpermicidalWaveForSomeTimesThreshold = 30; public int usedSpermicidalWaveForSeveralTimes = 50; public int destroyedCoupleOfSpermsWithSpermicidalWaveThreshold = 5; public int destroyedSomeSpermsWithSpermicidalWaveThreshold = 10; public int destroyedSeveralSpermsWithSpermicidalWaveThreshold = 15; public int destroyedXEnemiesWithRocketProjectileThreshold = 3; public int caughtXEnemiesWithGiantProjectileThreshold = 3; Dictionary _registeredAchievements = new Dictionary(); List _unlockedAchievements = new List(); List _unlockedAchievementsInJustPlayedGame = new List(); UnlockedAchievementPopup _unlockedAchievementPopup; int _achievementUnlockedInLastPlayIndex; int _totalAchievementsUnlockedInLastPlay; void Awake() { //FacebookManager.sessionOpenedEvent += OnSessionOpened; NotificationCenter.AddListener(OnTransitToGameOver, NotificationType.TransitToGameOver); NotificationCenter.AddListener(OnContinueAfterGameOver, NotificationType.MenuTransition); NotificationCenter.AddListener(OnShowNextUnlockedAchievement, NotificationType.ShowNextUnlockedAchievement); CoreNotificationCenter.AddListener(OnResetData, CoreNotificationType.ResetData); DebugViewManager.OnDebugView += OnDebugView; //AVDebug.Log("Querying for registered achievements"); //SocialManager.Instance.LoadRegisteredAchievements((isSuccessful, achievements) => //{ // if (isSuccessful) // { // AVDebug.Log("Got registered achievements: "+achievements.Length); // foreach(FaceBookAchievement achievement in achievements) // { // AchievementType achievementID = (AchievementType)System.Enum.Parse(typeof(AchievementType), achievement.AchievementID); //Enum.TryParse exists only in .Net 4 // _registeredAchievements[achievementID] = achievement; // } // } //}); //Populate unlockedAchievements with local data (if any) //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, //This is to cover the edge case of when offline, we cannot get the current unlocked achievements from the server AchievementType[] achievementTypes = (AchievementType[])System.Enum.GetValues(typeof(AchievementType)); foreach (AchievementType achievementType in achievementTypes) { if (PlayerPrefs.GetInt("Unlocked"+achievementType, 0) == 1) { _unlockedAchievements.Add(achievementType); } } } void OnDestroy() { //FacebookManager.sessionOpenedEvent -= OnSessionOpened; NotificationCenter.RemoveListener(OnTransitToGameOver, NotificationType.TransitToGameOver); NotificationCenter.RemoveListener(OnContinueAfterGameOver, NotificationType.MenuTransition); NotificationCenter.RemoveListener(OnShowNextUnlockedAchievement, NotificationType.ShowNextUnlockedAchievement); CoreNotificationCenter.RemoveListener(OnResetData, CoreNotificationType.ResetData); DebugViewManager.OnDebugView -= OnDebugView; } void OnResetData(CoreNotification note) { AVDebug.Log("Resetting unlocked achievements locally"); AchievementType[] achievementTypes = (AchievementType[])System.Enum.GetValues(typeof(AchievementType)); foreach (AchievementType achievementType in achievementTypes) { PlayerPrefs.SetInt("Unlocked"+achievementType, 0); } _unlockedAchievements.Clear(); } void OnDebugView() { if (GUILayout.Button("Lock Back Achievements")) { SocialManager.Instance.LockAchievements( (str,obj) => { _unlockedAchievements.Clear(); AVDebug.Log("Lock Achievements completed"); AVDebug.Log(str); }); } } void OnSessionOpened() { if (SocialManager.Instance.isSessionValid) { AVDebug.Log("Querying for unlocked achievements"); SocialManager.Instance.LoadAchievements((isSuccessful, achievements) => { if (isSuccessful) { AVDebug.Log("Got unlocked achievements: "+achievements.Length); var sb = new System.Text.StringBuilder(); foreach(FaceBookUnlockedAchievement achievement in achievements) { AVDebug.Log(achievement); sb.AppendLine("Unlocked achievement: "+achievement.AchievementID); AchievementType achievementType = (AchievementType)System.Enum.Parse(typeof(AchievementType), achievement.AchievementID); //Enum.TryParse exists only in .Net 4 _unlockedAchievements.Add(achievementType); //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 //achievements from previous install, quits, goes offline, relaunches, and plays it offline PlayerPrefs.SetInt("Unlocked"+achievementType,1); } PlayerPrefs.Save(); AVDebug.Log(sb.ToString()); } }); } } void OnTransitToGameOver(Notification note) { GameOverData data = (GameOverData)note.data; //CheckForAchievements(data.reason); } void OnContinueAfterGameOver(Notification note) { MenuManager.MenuTransition t = (MenuManager.MenuTransition)note.data; if ((MenuManager.CurrentState == MenuManager.MenuState.ViewingTimeUp || MenuManager.CurrentState == MenuManager.MenuState.ViewingVirusInfo) && t == MenuManager.MenuTransition.Continue) { if (_unlockedAchievementsInJustPlayedGame.Count > 0) { _unlockedAchievementPopup = (UnlockedAchievementPopup)Instantiate(unlockedAchievementPopupPrefab); Transform popupTransform = _unlockedAchievementPopup.transform; Vector3 originalScale = popupTransform.localScale; popupTransform.parent = popupAnchor; popupTransform.localScale = originalScale; popupTransform.localPosition = unlockedAchievementPopupPrefab.transform.localPosition; _unlockedAchievementPopup.Show(); _achievementUnlockedInLastPlayIndex = 0; _totalAchievementsUnlockedInLastPlay = _unlockedAchievementsInJustPlayedGame.Count; OnShowNextUnlockedAchievement(null); } else { NotificationCenter.Post(NotificationType.AchievementsPopupsFinished); } } } void OnShowNextUnlockedAchievement(Notification note) { if (_unlockedAchievementsInJustPlayedGame.Count > 0) { ++_achievementUnlockedInLastPlayIndex; AchievementType unlockedAchievement = _unlockedAchievementsInJustPlayedGame[0]; _unlockedAchievementsInJustPlayedGame.RemoveAt(0); if (_registeredAchievements.ContainsKey(unlockedAchievement)) { _unlockedAchievementPopup.SetData(_registeredAchievements[unlockedAchievement], _achievementUnlockedInLastPlayIndex, _totalAchievementsUnlockedInLastPlay); } else { AVDebug.LogError("Registered achievements does not have "+unlockedAchievement+". Registered achievements must have failed to load. Current registeredAchievements size is "+_registeredAchievements.Count); OnShowNextUnlockedAchievement(null); //Basically skip this and go to the next } } else { _unlockedAchievementPopup.Hide(); _unlockedAchievementPopup = null; NotificationCenter.Post(NotificationType.AchievementsPopupsFinished); } } void UnlockAchievement(AchievementType achievementType) { if (_unlockedAchievements.Contains(achievementType)) { AVDebug.Log("Ignore achievement "+achievementType+". Already unlocked"); return; } AVDebug.Log("Unlocking achievement "+achievementType); SocialManager.Instance.UnlockAchievement(achievementType.ToString(), (isSuccessful, msg) => { //Only save locally if we managed to do record it online PlayerPrefs.SetInt("Unlocked"+achievementType,1); PlayerPrefs.Save(); AVDebug.Log("Unlock successful : " + isSuccessful); AVDebug.Log("Unlock msg : " + msg); }); _unlockedAchievements.Add(achievementType); _unlockedAchievementsInJustPlayedGame.Add(achievementType); } void CheckForAchievements(GameOverData.Reason gameOverReason) { //Do not check for achievements if we are not logged in to facebook if (!FacebookHelper.Instance.IsLoggedIn()) { return; } GameDataManager gameDataManager = GameDataManager.Instance; //GotAllWeaponUpgrades PickupType[] pickupTypes = (PickupType[])System.Enum.GetValues(typeof(PickupType)); bool gotAllWeaponUpgrades = true; foreach (PickupType pickupType in pickupTypes) { if (pickupType != PickupType.DefaultWeapon && //ignore default gameDataManager.GetWeaponUpgradeTypePickedUp(pickupType) == 0) { gotAllWeaponUpgrades = false; break; } } if (gotAllWeaponUpgrades) { UnlockAchievement(AchievementType.GotAllWeaponUpgrades); } //GotXAutomaticWeaponUpgrades if (gameDataManager.GetWeaponUpgradeTypePickedUp(PickupType.Automatic) >= gotXAutomaticWeaponUpgradesThreshold) { UnlockAchievement(AchievementType.GotXAutomaticWeaponUpgrades); } //GotXHomingWeaponUpgrades if (gameDataManager.GetWeaponUpgradeTypePickedUp(PickupType.Homing) >= gotXHomingWeaponUpgradesThreshold) { UnlockAchievement(AchievementType.GotXHomingWeaponUpgrades); } //GotXRocketWeaponUpgrades if (gameDataManager.GetWeaponUpgradeTypePickedUp(PickupType.Rocket) >= gotXRocketWeaponUpgradesThreshold) { UnlockAchievement(AchievementType.GotXRocketWeaponUpgrades); } //GotXGiantWeaponUpgrades if (gameDataManager.GetWeaponUpgradeTypePickedUp(PickupType.GiantCondom) >= gotXGiantWeaponUpgradesThreshold) { UnlockAchievement(AchievementType.GotXGiantWeaponUpgrades); } //FinishedTimedModeWithoutCapturingASingleSperm if (gameOverReason == GameOverData.Reason.TimeUp && gameDataManager.GetEnemyTypeCaught(EnemyType.standardSperm) == 0 && gameDataManager.GetEnemyTypeCaught(EnemyType.bronzeSperm) == 0 && gameDataManager.GetEnemyTypeCaught(EnemyType.silverSperm) == 0 && gameDataManager.GetEnemyTypeCaught(EnemyType.goldSperm) == 0) { UnlockAchievement(AchievementType.FinishedTimedModeWithoutCapturingASingleSperm); } //CapturedAllSpecialSpermsInASinglePlay if (gameOverReason == GameOverData.Reason.TimeUp && gameDataManager.SpecialSpermsGoneThrough == 0) { UnlockAchievement(AchievementType.CapturedAllSpecialSpermsInASinglePlay); } //CapturedAllSpermsAndEnemiesInASinglePlay //If the time is up, it is obvious that no virus passed through and so got captured if (gameOverReason == GameOverData.Reason.TimeUp && gameDataManager.SpermsGoneThrough == 0) { UnlockAchievement(AchievementType.CapturedAllSpermsAndEnemiesInASinglePlay); } PersistedGameDataManager persistedGameDataManager = PersistedGameDataManager.Instance; //CapturedATotalOfXGonorrhea if (persistedGameDataManager.GetTotalCaught(EnemyType.gonorrhea) >= capturedATotalOfXGonorrheaThreshold) { UnlockAchievement(AchievementType.CapturedATotalOfXGonorrhea); } //CapturedATotalOfXSyphilis if (persistedGameDataManager.GetTotalCaught(EnemyType.syphilis) >= capturedATotalOfXSyphilisThreshold) { UnlockAchievement(AchievementType.CapturedATotalOfXSyphilis); } //CapturedATotalOfXHPV if (persistedGameDataManager.GetTotalCaught(EnemyType.hpv) >= capturedATotalOfXHPVThreshold) { UnlockAchievement(AchievementType.CapturedATotalOfXHPV); } //CapturedATotalOfXHIV if (persistedGameDataManager.GetTotalCaught(EnemyType.hiv) >= capturedATotalOfXHIVThreshold) { UnlockAchievement(AchievementType.CapturedATotalOfXHIV); } //CapturedATotalOfXChlamydia if (persistedGameDataManager.GetTotalCaught(EnemyType.chlamydia) >= capturedATotalOfXChlamydiaThreshold) { UnlockAchievement(AchievementType.CapturedATotalOfXChlamydia); } //CapturedATotalOfXHerpes if (persistedGameDataManager.GetTotalCaught(EnemyType.herpes) >= capturedATotalOfXHerpesThreshold) { UnlockAchievement(AchievementType.CapturedATotalOfXHerpes); } //UsedSpermicidalWaveForCoupleOfTimes if (persistedGameDataManager.SpermicidalWavesFired >= usedSpermicidalWaveForCoupleOfTimesThreshold) { UnlockAchievement(AchievementType.UsedSpermicidalWaveForCoupleOfTimes); } //UsedSpermicidalWaveForSomeTimes if (persistedGameDataManager.SpermicidalWavesFired >= usedSpermicidalWaveForSomeTimesThreshold) { UnlockAchievement(AchievementType.UsedSpermicidalWaveForSomeTimes); } //UsedSpermicidalWaveForSeveralTimes if (persistedGameDataManager.SpermicidalWavesFired >= usedSpermicidalWaveForSeveralTimes) { UnlockAchievement(AchievementType.UsedSpermicidalWaveForSeveralTimes); } //DestroyedCoupleOfSpermsWithSpermicidalWave if (gameDataManager.BiggestCountOfSpermsHitBySpermicidalWave >= destroyedCoupleOfSpermsWithSpermicidalWaveThreshold) { UnlockAchievement(AchievementType.DestroyedCoupleOfSpermsWithSpermicidalWave); } //DestroyedSomeSpermsWithSpermicidalWave if (gameDataManager.BiggestCountOfSpermsHitBySpermicidalWave >= destroyedSomeSpermsWithSpermicidalWaveThreshold) { UnlockAchievement(AchievementType.DestroyedSomeSpermsWithSpermicidalWave); } //DestroyedSeveralSpermsWithSpermicidalWave if (gameDataManager.BiggestCountOfSpermsHitBySpermicidalWave >= destroyedSeveralSpermsWithSpermicidalWaveThreshold) { UnlockAchievement(AchievementType.DestroyedSeveralSpermsWithSpermicidalWave); } //DestroyedXEnemiesWithRocketProjectile if (gameDataManager.BiggestCountOfEnemiesHitByRocket >= destroyedXEnemiesWithRocketProjectileThreshold) { UnlockAchievement(AchievementType.DestroyedXEnemiesWithRocketProjectile); } //CaughtXEnemiesWithGiantProjectile if (gameDataManager.BiggestCountOfEnemiesCaughtByGiantCondom >= caughtXEnemiesWithGiantProjectileThreshold) { UnlockAchievement(AchievementType.CaughtXEnemiesWithGiantProjectile); } } }