Browse Source

Added manual achievement unlock

James Peret 2 years ago
parent
commit
8b2d4dbed9
2 changed files with 44 additions and 9 deletions
  1. 7 6
      Runtime/AchievementBase.cs
  2. 37 3
      Runtime/AchievementsController.cs

+ 7 - 6
Runtime/AchievementBase.cs

@@ -15,15 +15,16 @@ namespace KairoEngine.Achievements
         [BoxGroup("Achievement")] public string identifier;
         [BoxGroup("Achievement")] public Sprite unlockedIcon;
         [BoxGroup("Achievement")] public Sprite lockedIcon;
+        [BoxGroup("Achievement")] public bool useStatistic = true;
         [BoxGroup("Achievement"), HideLabel, TextArea(minLines:2 ,maxLines:2)] public string description;
 
-        [BoxGroup("Statistic")] public string statisticTitle;
-        [BoxGroup("Statistic")] public ComparisionOperator comparasion;
+        [BoxGroup("Statistic"), ShowIf("@useStatistic")] public string statisticTitle;
+        [BoxGroup("Statistic"), ShowIf("@useStatistic")] public ComparisionOperator comparasion;
 
-        [BoxGroup("Statistic")] public StatisticType statisticType = StatisticType.integer;
-        [BoxGroup("Statistic"), ShowIf("@statisticType == StatisticType.integer")] public int intValue;
-        [BoxGroup("Statistic") , ShowIf("@statisticType == StatisticType.time")] public float floatValue;
-        [BoxGroup("Statistic"), ShowIf("@statisticType == StatisticType.text")] public string stringValue;
+        [BoxGroup("Statistic"), ShowIf("@useStatistic")] public StatisticType statisticType = StatisticType.integer;
+        [BoxGroup("Statistic"), ShowIf("@statisticType == StatisticType.integer && useStatistic")] public int intValue;
+        [BoxGroup("Statistic"), ShowIf("@statisticType == StatisticType.time && useStatistic")] public float floatValue;
+        [BoxGroup("Statistic"), ShowIf("@statisticType == StatisticType.text && useStatistic")] public string stringValue;
 
         public bool HasAchieved()
         {

+ 37 - 3
Runtime/AchievementsController.cs

@@ -11,7 +11,8 @@ namespace KairoEngine.Achievements
     [HideMonoScript]
     public class AchievementsController : SerializedMonoBehaviour
     {
-        public string achievementCompletedEvent = "AchievementCompletedEvent";
+        public string achievementUnlockedEvent = "AchievementUnlockedEvent";
+        public string achievementLockedEvent = "AchievementLockedEvent";
        
         public bool CheckOnStart = true;
         public bool checkOnInterval = true;
@@ -47,6 +48,8 @@ namespace KairoEngine.Achievements
                 if(CheckOnStart) CheckAchievements();
                 if(checkOnInterval) StartCheckTimer();
             });
+            GenericEvents.StartListening("UnlockAchievement", UnlockAchievement);
+            GenericEvents.StartListening("LockAchievement", LockAchievement);
             
         }
 
@@ -65,8 +68,8 @@ namespace KairoEngine.Achievements
                 if(achievementsStatus[i].achievement.HasAchieved())
                 {
                     Debug.Log($"Achievement Unlocked: {achievementsStatus[i].achievement.title}");
-                    GenericEvents.Trigger(achievementCompletedEvent, achievementsStatus[i].achievement.identifier);
                     achievementsStatus[i].unlocked = true;
+                    GenericEvents.Trigger(achievementUnlockedEvent, achievementsStatus[i].achievement.identifier);
                     PlayerPrefs.SetInt($"ACHIEVEMENT_{achievementsStatus[i].achievement.identifier}", 1);
                 }
             }
@@ -81,7 +84,38 @@ namespace KairoEngine.Achievements
                     StartCheckTimer();
                 }
             });
-            
+        }
+
+        public void UnlockAchievement(string identifier)
+        {
+            if(debug) Debug.Log($"Unlocking achievement {identifier}");
+            if(library == null) return;
+            for (int i = 0; i < achievementsStatus.Count; i++)
+            {
+                if(achievementsStatus[i].achievement.identifier == identifier)
+                {
+                    if(achievementsStatus[i].unlocked == true) return;
+                    achievementsStatus[i].unlocked = true;
+                    GenericEvents.Trigger(achievementUnlockedEvent, achievementsStatus[i].achievement.identifier);
+                    PlayerPrefs.SetInt($"ACHIEVEMENT_{achievementsStatus[i].achievement.identifier}", 1);
+                }
+            }
+        }
+
+        public void LockAchievement(string identifier)
+        {
+            if(debug) Debug.Log($"Locking achievement {identifier}");
+            if(library == null) return;
+            for (int i = 0; i < achievementsStatus.Count; i++)
+            {
+                if(achievementsStatus[i].achievement.identifier == identifier)
+                {
+                    if(achievementsStatus[i].unlocked == false) return;
+                    achievementsStatus[i].unlocked = false;
+                    GenericEvents.Trigger(achievementLockedEvent, achievementsStatus[i].achievement.identifier);
+                    PlayerPrefs.SetInt($"ACHIEVEMENT_{achievementsStatus[i].achievement.identifier}", 0);
+                }
+            }
         }
     }
 }