AchievementBase.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using System.Collections;
  2. using System;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using Sirenix.OdinInspector;
  6. using KairoEngine.Statistics;
  7. using StatisticsSystem = KairoEngine.Statistics.Statistics;
  8. using KairoEngine.UI.Encyclopedia;
  9. namespace KairoEngine.Achievements
  10. {
  11. [CreateAssetMenu(fileName = "Achievement", menuName = "KairoEngine/Achievements/Achievement", order = 10), HideMonoScript]
  12. public class AchievementBase : ScriptableObject, IEncyclopediaArticle
  13. {
  14. [BoxGroup("Achievement")] public string title;
  15. [BoxGroup("Achievement")] public string identifier;
  16. [BoxGroup("Achievement")] public Sprite unlockedIcon;
  17. [BoxGroup("Achievement")] public Sprite lockedIcon;
  18. [BoxGroup("Achievement")] public bool useStatistic = true;
  19. [BoxGroup("Achievement"), HideLabel, TextArea(minLines:2 ,maxLines:2)] public string description;
  20. [BoxGroup("Statistic"), ShowIf("@useStatistic")] public string statisticTitle;
  21. [BoxGroup("Statistic"), ShowIf("@useStatistic")] public ComparisionOperator comparasion;
  22. [BoxGroup("Statistic"), ShowIf("@useStatistic")] public StatisticType statisticType = StatisticType.integer;
  23. [BoxGroup("Statistic"), ShowIf("@statisticType == StatisticType.integer && useStatistic")] public int intValue;
  24. [BoxGroup("Statistic"), ShowIf("@statisticType == StatisticType.time && useStatistic")] public float floatValue;
  25. [BoxGroup("Statistic"), ShowIf("@statisticType == StatisticType.text && useStatistic")] public string stringValue;
  26. public bool HasAchieved()
  27. {
  28. var stat = StatisticsSystem.GetData(statisticTitle);
  29. if(stat == null) return false;
  30. switch (statisticType)
  31. {
  32. case StatisticType.integer:
  33. int currentInt = stat.GetInteger();
  34. return Compare<int>(currentInt, intValue);
  35. case StatisticType.time:
  36. float currentTime = stat.GetTime();
  37. return Compare<float>(currentTime, floatValue);
  38. case StatisticType.text:
  39. string currentText = stat.GetText();
  40. return Compare<string>(currentText, stringValue);
  41. default:
  42. return false;
  43. }
  44. }
  45. public bool Compare<T>(T value1, T value2)
  46. {
  47. switch (comparasion)
  48. {
  49. default:
  50. case ComparisionOperator.EqualTo:
  51. return EqualityComparer<T>.Default.Equals(value1 , value2);
  52. case ComparisionOperator.NotEqualTo:
  53. return !EqualityComparer<T>.Default.Equals(value1 , value2);
  54. case ComparisionOperator.GreaterThan:
  55. if(typeof(T) == typeof(int)) return Convert.ToInt32(value1) > Convert.ToInt32(value2);
  56. else if(typeof(T) == typeof(float)) return Convert.ToDecimal(value1) > Convert.ToDecimal(value2);
  57. else if(typeof(T) == typeof(string)) return Convert.ToString(value1).Length > Convert.ToString(value2).Length;
  58. else return false;
  59. case ComparisionOperator.GreaterThanOrEqualTo:
  60. if(typeof(T) == typeof(int)) return Convert.ToInt32(value1) >= Convert.ToInt32(value2);
  61. else if(typeof(T) == typeof(float)) return Convert.ToDecimal(value1) >= Convert.ToDecimal(value2);
  62. else if(typeof(T) == typeof(string)) return Convert.ToString(value1).Length >= Convert.ToString(value2).Length;
  63. else return false;
  64. case ComparisionOperator.LessThan:
  65. if(typeof(T) == typeof(int)) return Convert.ToInt32(value1) < Convert.ToInt32(value2);
  66. else if(typeof(T) == typeof(float)) return Convert.ToDecimal(value1) < Convert.ToDecimal(value2);
  67. else if(typeof(T) == typeof(string)) return Convert.ToString(value1).Length < Convert.ToString(value2).Length;
  68. else return false;
  69. case ComparisionOperator.LessOrEqualTo:
  70. if(typeof(T) == typeof(int)) return Convert.ToInt32(value1) <= Convert.ToInt32(value2);
  71. else if(typeof(T) == typeof(float)) return Convert.ToDecimal(value1) <= Convert.ToDecimal(value2);
  72. else if(typeof(T) == typeof(string)) return Convert.ToString(value1).Length <= Convert.ToString(value2).Length;
  73. else return false;
  74. }
  75. }
  76. public EncyclopediaArticle GetArticle()
  77. {
  78. bool unlocked = HasAchieved();
  79. var stat = StatisticsSystem.GetData(statisticTitle);
  80. var article = new EncyclopediaArticle
  81. {
  82. title = title,
  83. id = identifier,
  84. description = description,
  85. unlocked = unlocked,
  86. icon = unlocked ? unlockedIcon : lockedIcon
  87. };
  88. article.content.Add("title", title);
  89. article.content.Add("identifier", identifier);
  90. article.content.Add("description", description);
  91. article.content.Add("statistic", statisticTitle);
  92. article.booleans.Add("unlocked", unlocked);
  93. article.booleans.Add("use-statistic", useStatistic);
  94. article.images.Add("unlocked-icon", unlockedIcon);
  95. article.images.Add("locked-icon", lockedIcon);
  96. if(statisticType == StatisticType.integer && stat != null)
  97. {
  98. article.integers.Add("current-value", stat.GetInteger());
  99. article.integers.Add("max-value", intValue);
  100. }
  101. return article;
  102. }
  103. }
  104. public enum ComparisionOperator
  105. {
  106. EqualTo, NotEqualTo, GreaterThan, GreaterThanOrEqualTo, LessThan, LessOrEqualTo
  107. }
  108. }