AchievementsController.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Sirenix.OdinInspector;
  5. using KairoEngine.Statistics;
  6. using KairoEngine.Core;
  7. using UniRx;
  8. using StatisticsSystem = KairoEngine.Statistics.Statistics;
  9. namespace KairoEngine.Achievements
  10. {
  11. [HideMonoScript]
  12. public class AchievementsController : SerializedMonoBehaviour
  13. {
  14. public string achievementUnlockedEvent = "AchievementUnlockedEvent";
  15. public string achievementLockedEvent = "AchievementLockedEvent";
  16. public bool CheckOnStart = true;
  17. public bool checkOnInterval = true;
  18. [ShowIf("@checkOnInterval"), SuffixLabel("ms")] public int checkInterval = 1000;
  19. [SuffixLabel("ms")] public int startDelay = 0;
  20. public bool debug = false;
  21. [ShowIf("@!initialized")] public AchievementsLibrary library;
  22. [ShowIf("@initialized"), System.NonSerialized, ShowInInspector, HideReferenceObjectPicker]
  23. [ListDrawerSettings(IsReadOnly = true, DraggableItems = false, Expanded = true, ShowIndexLabels = false, ShowPaging = false, ShowItemCount = true, HideRemoveButton = true)]
  24. public List<AchievementStatus> achievementsStatus;
  25. private bool initialized = false;
  26. private CompositeDisposable timerComposite;
  27. private void Start()
  28. {
  29. if(debug) Debug.Log("Initializing AchievementsController");
  30. if(StatisticsSystem.instance == null) Debug.LogError("Missing Statistics component");
  31. if(library == null)
  32. {
  33. Debug.LogError("Missing Achievements Library on AchievementsController.\nPlease configure the Achievements module in the GameConfig file.");
  34. return;
  35. }
  36. achievementsStatus = new List<AchievementStatus>();
  37. for (int i = 0; i < library.achievements.Count; i++)
  38. {
  39. int value = PlayerPrefs.GetInt($"ACHIEVEMENT_{library.achievements[i].identifier}", 0);
  40. achievementsStatus.Add(new AchievementStatus(library.achievements[i], value == 0 ? false : true));
  41. PlayerPrefs.SetInt($"ACHIEVEMENT_{library.achievements[i].identifier}", value);
  42. }
  43. if(initialized == false) initialized = true;
  44. Timer.ExecuteRealTime(startDelay, () => {
  45. if(CheckOnStart) CheckAchievements();
  46. if(checkOnInterval) StartCheckTimer();
  47. });
  48. GenericEvents.StartListening("UnlockAchievement", UnlockAchievement);
  49. GenericEvents.StartListening("LockAchievement", LockAchievement);
  50. }
  51. private void OnDestroy()
  52. {
  53. if (timerComposite != null) timerComposite.Dispose();
  54. }
  55. public void CheckAchievements()
  56. {
  57. //if(debug) Debug.Log($"Checking achievements ({achievementsStatus.Count})");
  58. if(library == null) return;
  59. for (int i = 0; i < achievementsStatus.Count; i++)
  60. {
  61. if(achievementsStatus[i].unlocked == true) continue;
  62. if(achievementsStatus[i].achievement.HasAchieved())
  63. {
  64. Debug.Log($"Achievement Unlocked: {achievementsStatus[i].achievement.title}");
  65. achievementsStatus[i].unlocked = true;
  66. GenericEvents.Trigger(achievementUnlockedEvent, achievementsStatus[i].achievement.identifier);
  67. PlayerPrefs.SetInt($"ACHIEVEMENT_{achievementsStatus[i].achievement.identifier}", 1);
  68. }
  69. }
  70. }
  71. public void StartCheckTimer()
  72. {
  73. timerComposite = Timer.ExecuteRealTime(checkInterval, () => {
  74. if(checkOnInterval)
  75. {
  76. CheckAchievements();
  77. StartCheckTimer();
  78. }
  79. });
  80. }
  81. public void UnlockAchievement(string identifier)
  82. {
  83. if(debug) Debug.Log($"Unlocking achievement {identifier}");
  84. if(library == null) return;
  85. for (int i = 0; i < achievementsStatus.Count; i++)
  86. {
  87. if(achievementsStatus[i].achievement.identifier == identifier)
  88. {
  89. if(achievementsStatus[i].unlocked == true) return;
  90. achievementsStatus[i].unlocked = true;
  91. GenericEvents.Trigger(achievementUnlockedEvent, achievementsStatus[i].achievement.identifier);
  92. PlayerPrefs.SetInt($"ACHIEVEMENT_{achievementsStatus[i].achievement.identifier}", 1);
  93. }
  94. }
  95. }
  96. public void LockAchievement(string identifier)
  97. {
  98. if(debug) Debug.Log($"Locking achievement {identifier}");
  99. if(library == null) return;
  100. for (int i = 0; i < achievementsStatus.Count; i++)
  101. {
  102. if(achievementsStatus[i].achievement.identifier == identifier)
  103. {
  104. if(achievementsStatus[i].unlocked == false) return;
  105. achievementsStatus[i].unlocked = false;
  106. GenericEvents.Trigger(achievementLockedEvent, achievementsStatus[i].achievement.identifier);
  107. PlayerPrefs.SetInt($"ACHIEVEMENT_{achievementsStatus[i].achievement.identifier}", 0);
  108. }
  109. }
  110. }
  111. }
  112. }