using System.Collections; using System; using System.Collections.Generic; using UnityEngine; using Sirenix.OdinInspector; using KairoEngine.Utilities.Statistics; namespace KairoEngine.Achievements { [CreateAssetMenu(fileName = "Achievement", menuName = "KairoEngine/Achievements/Achievement", order = 10), HideMonoScript] public class AchievementBase : ScriptableObject { [BoxGroup("Achievement")] public string title; [BoxGroup("Achievement")] public string identifier; [BoxGroup("Achievement")] public Sprite unlockedIcon; [BoxGroup("Achievement")] public Sprite lockedIcon; [BoxGroup("Achievement"), HideLabel, TextArea(minLines:2 ,maxLines:2)] public string description; [BoxGroup("Statistic")] public string statisticTitle; [BoxGroup("Statistic")] 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; public bool HasAchieved() { var stat = Statistics.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 enum ComparisionOperator { EqualTo, NotEqualTo, GreaterThan, GreaterThanOrEqualTo, LessThan, LessOrEqualTo } }