using System.Collections; using System; using System.Collections.Generic; 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, IEncyclopediaArticle { [BoxGroup("Achievement")] public string title; [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"), ShowIf("@useStatistic")] public string statisticTitle; [BoxGroup("Statistic"), ShowIf("@useStatistic")] public ComparisionOperator comparasion; [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() { var stat = StatisticsSystem.GetData(statisticTitle); if(stat == null) return false; switch (statisticType) { case StatisticType.integer: int currentInt = stat.GetInteger(); return Compare(currentInt, intValue); case StatisticType.time: float currentTime = stat.GetTime(); return Compare(currentTime, floatValue); case StatisticType.text: string currentText = stat.GetText(); return Compare(currentText, stringValue); default: return false; } } public bool Compare(T value1, T value2) { switch (comparasion) { default: case ComparisionOperator.EqualTo: return EqualityComparer.Default.Equals(value1 , value2); case ComparisionOperator.NotEqualTo: return !EqualityComparer.Default.Equals(value1 , value2); case ComparisionOperator.GreaterThan: if(typeof(T) == typeof(int)) return Convert.ToInt32(value1) > Convert.ToInt32(value2); else if(typeof(T) == typeof(float)) return Convert.ToDecimal(value1) > Convert.ToDecimal(value2); else if(typeof(T) == typeof(string)) return Convert.ToString(value1).Length > Convert.ToString(value2).Length; else return false; case ComparisionOperator.GreaterThanOrEqualTo: if(typeof(T) == typeof(int)) return Convert.ToInt32(value1) >= Convert.ToInt32(value2); else if(typeof(T) == typeof(float)) return Convert.ToDecimal(value1) >= Convert.ToDecimal(value2); else if(typeof(T) == typeof(string)) return Convert.ToString(value1).Length >= Convert.ToString(value2).Length; else return false; case ComparisionOperator.LessThan: if(typeof(T) == typeof(int)) return Convert.ToInt32(value1) < Convert.ToInt32(value2); else if(typeof(T) == typeof(float)) return Convert.ToDecimal(value1) < Convert.ToDecimal(value2); else if(typeof(T) == typeof(string)) return Convert.ToString(value1).Length < Convert.ToString(value2).Length; else return false; case ComparisionOperator.LessOrEqualTo: if(typeof(T) == typeof(int)) return Convert.ToInt32(value1) <= Convert.ToInt32(value2); else if(typeof(T) == typeof(float)) return Convert.ToDecimal(value1) <= Convert.ToDecimal(value2); else if(typeof(T) == typeof(string)) return Convert.ToString(value1).Length <= Convert.ToString(value2).Length; 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 { EqualTo, NotEqualTo, GreaterThan, GreaterThanOrEqualTo, LessThan, LessOrEqualTo } }