AchievementsController.cs 5.6 KB

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