Browse Source

Added Achievement Encyclopedia Article interface

James Peret 2 years ago
parent
commit
5812fbbf7b

+ 30 - 1
Runtime/AchievementBase.cs

@@ -5,11 +5,12 @@ using UnityEngine;
 using Sirenix.OdinInspector;
 using KairoEngine.Statistics;
 using StatisticsSystem = KairoEngine.Statistics.Statistics;
+using KairoEngine.UI.Encyclopedia;
 
 namespace KairoEngine.Achievements
 {
     [CreateAssetMenu(fileName = "Achievement", menuName = "KairoEngine/Achievements/Achievement", order = 10), HideMonoScript]
-    public class AchievementBase : ScriptableObject
+    public class AchievementBase : ScriptableObject, IEncyclopediaArticle
     {
         
         [BoxGroup("Achievement")] public string title;
@@ -78,6 +79,34 @@ namespace KairoEngine.Achievements
                     else return false;
             }
         }
+
+        public EncyclopediaArticle GetArticle()
+        {
+            bool unlocked = HasAchieved();
+            var stat = StatisticsSystem.GetData(statisticTitle);
+            var article = new EncyclopediaArticle
+            {
+                title = title,
+                id = identifier,
+                description = description,
+                unlocked = unlocked,
+                icon = unlocked ? unlockedIcon : lockedIcon
+            };
+            article.content.Add("title", title);
+            article.content.Add("identifier", identifier);
+            article.content.Add("description", description);
+            article.content.Add("statistic", statisticTitle);
+            article.booleans.Add("unlocked", unlocked);
+            article.booleans.Add("use-statistic", useStatistic);
+            article.images.Add("unlocked-icon", unlockedIcon);
+            article.images.Add("locked-icon", lockedIcon);
+            if(statisticType == StatisticType.integer && stat != null)
+            {
+                article.integers.Add("current-value", stat.GetInteger());
+                article.integers.Add("max-value", intValue);
+            }
+            return article;
+        }
     }
 
     public enum ComparisionOperator

+ 50 - 0
Runtime/AchievementEncyclopediaArticleUi.cs

@@ -0,0 +1,50 @@
+using System.Collections;
+using System.Collections.Generic;
+using UnityEngine;
+using UnityEngine.UI;
+using TMPro;
+using KairoEngine.UI.Encyclopedia;
+using Sirenix.OdinInspector;
+
+namespace KairoEngine.Achievements
+{
+    [HideMonoScript]
+    public class AchievementEncyclopediaArticleUi : MonoBehaviour, IEncyclopediaArticleDataView
+    {
+        public Image iconContainer;
+        public TextMeshProUGUI titleText;
+        public TextMeshProUGUI descriptionText;
+        public Slider progressBar;
+        public Color unlockedTextColor = Color.white;
+        public Color lockedTextColor = Color.gray;
+
+        public void Setup(EncyclopediaArticle article)
+        {
+            article.booleans.TryGetValue("unlocked", out bool unlocked);
+            if(unlocked) 
+            {
+                titleText.color = unlockedTextColor;
+                descriptionText.color = unlockedTextColor;
+            }
+            else
+            {
+                titleText.color = lockedTextColor;
+                descriptionText.color = lockedTextColor;
+            }
+            article.booleans.TryGetValue("use-statistic", out bool useStatistic);
+            iconContainer.sprite = article.icon;
+            titleText.text = article.title;
+            descriptionText.text = article.description;
+            if(progressBar != null && useStatistic)
+            {
+                progressBar.gameObject.SetActive(true);
+                article.integers.TryGetValue("current-value", out int value);
+                article.integers.TryGetValue("max-value", out int max);
+                if(value > max) value = max;
+                progressBar.maxValue = max;
+                progressBar.value = value;
+            }
+            else progressBar.gameObject.SetActive(false);
+        }
+    }
+}

+ 11 - 0
Runtime/AchievementEncyclopediaArticleUi.cs.meta

@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: c16937765c56f274087991436875ea33
+MonoImporter:
+  externalObjects: {}
+  serializedVersion: 2
+  defaultReferences: []
+  executionOrder: 0
+  icon: {instanceID: 0}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 40 - 0
Runtime/AchievementsEncyclopediaCategory.cs

@@ -0,0 +1,40 @@
+using System.Collections;
+using System.Collections.Generic;
+using UnityEngine;
+using KairoEngine.UI.Encyclopedia;
+using Sirenix.OdinInspector;
+
+namespace KairoEngine.Achievements
+{
+    public class AchievementsEncyclopediaCategory : EncyclopediaCategory
+    {
+        public AchievementsController achievementsController;
+
+        private List<EncyclopediaArticle> data = new List<EncyclopediaArticle>();
+
+        private void Start()
+        {
+            if(achievementsController == null) achievementsController = FindObjectOfType<AchievementsController>();
+            if(achievementsController == null) Debug.LogError("Missing Achievements Controller", this.gameObject);
+        }
+
+        public override List<EncyclopediaArticle> GetArticles()
+        {
+            UpdateArticles();
+            return data;
+        }
+
+        public override void UpdateArticles()
+        {
+            data = new List<EncyclopediaArticle>();
+            if(achievementsController == null) return;
+            foreach (AchievementStatus item in achievementsController.achievementsStatus) 
+            {
+                var article = item.achievement.GetArticle();
+                article.id = id + "_" + article.id;
+                article.unlocked = item.unlocked;
+                data.Add(article);
+            }
+        }
+    }
+}

+ 11 - 0
Runtime/AchievementsEncyclopediaCategory.cs.meta

@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: 83f73a21432ed6941801d2cf5b9db9a8
+MonoImporter:
+  externalObjects: {}
+  serializedVersion: 2
+  defaultReferences: []
+  executionOrder: 0
+  icon: {instanceID: 0}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 3 - 1
Runtime/KairoEngine.Achievements.asmdef

@@ -4,9 +4,11 @@
     "references": [
         "GUID:7e5ae6a38d1532248b4c890eca668b06",
         "GUID:165d83fc3bb2a4144925c85421871d8e",
+        "GUID:142285d3db5e7e849b02ea3a75bc2de7",
         "GUID:560b04d1a97f54a4e82edc0cbbb69285",
         "GUID:ca01279424a9a9144a1fea1b6910a36a",
-        "GUID:e048eeec9bdb9d30448017b829deb3f6"
+        "GUID:e048eeec9bdb9d30448017b829deb3f6",
+        "GUID:6055be8ebefd69e48b49212b09b47b2f"
     ],
     "includePlatforms": [],
     "excludePlatforms": [],