AchievementBase.cs 4.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. namespace KairoEngine.Achievements
  9. {
  10. [CreateAssetMenu(fileName = "Achievement", menuName = "KairoEngine/Achievements/Achievement", order = 10), HideMonoScript]
  11. public class AchievementBase : ScriptableObject
  12. {
  13. [BoxGroup("Achievement")] public string title;
  14. [BoxGroup("Achievement")] public string identifier;
  15. [BoxGroup("Achievement")] public Sprite unlockedIcon;
  16. [BoxGroup("Achievement")] public Sprite lockedIcon;
  17. [BoxGroup("Achievement")] public bool useStatistic = true;
  18. [BoxGroup("Achievement"), HideLabel, TextArea(minLines:2 ,maxLines:2)] public string description;
  19. [BoxGroup("Statistic"), ShowIf("@useStatistic")] public string statisticTitle;
  20. [BoxGroup("Statistic"), ShowIf("@useStatistic")] public ComparisionOperator comparasion;
  21. [BoxGroup("Statistic"), ShowIf("@useStatistic")] public StatisticType statisticType = StatisticType.integer;
  22. [BoxGroup("Statistic"), ShowIf("@statisticType == StatisticType.integer && useStatistic")] public int intValue;
  23. [BoxGroup("Statistic"), ShowIf("@statisticType == StatisticType.time && useStatistic")] public float floatValue;
  24. [BoxGroup("Statistic"), ShowIf("@statisticType == StatisticType.text && useStatistic")] public string stringValue;
  25. public bool HasAchieved()
  26. {
  27. var stat = StatisticsSystem.GetData(statisticTitle);
  28. if(stat == null) return false;
  29. switch (statisticType)
  30. {
  31. case StatisticType.integer:
  32. int currentInt = stat.GetInteger();
  33. return Compare<int>(currentInt, intValue);
  34. case StatisticType.time:
  35. float currentTime = stat.GetTime();
  36. return Compare<float>(currentTime, floatValue);
  37. case StatisticType.text:
  38. string currentText = stat.GetText();
  39. return Compare<string>(currentText, stringValue);
  40. default:
  41. return false;
  42. }
  43. }
  44. public bool Compare<T>(T value1, T value2)
  45. {
  46. switch (comparasion)
  47. {
  48. default:
  49. case ComparisionOperator.EqualTo:
  50. return EqualityComparer<T>.Default.Equals(value1 , value2);
  51. case ComparisionOperator.NotEqualTo:
  52. return !EqualityComparer<T>.Default.Equals(value1 , value2);
  53. case ComparisionOperator.GreaterThan:
  54. if(typeof(T) == typeof(int)) return Convert.ToInt32(value1) > Convert.ToInt32(value2);
  55. else if(typeof(T) == typeof(float)) return Convert.ToDecimal(value1) > Convert.ToDecimal(value2);
  56. else if(typeof(T) == typeof(string)) return Convert.ToString(value1).Length > Convert.ToString(value2).Length;
  57. else return false;
  58. case ComparisionOperator.GreaterThanOrEqualTo:
  59. if(typeof(T) == typeof(int)) return Convert.ToInt32(value1) >= Convert.ToInt32(value2);
  60. else if(typeof(T) == typeof(float)) return Convert.ToDecimal(value1) >= Convert.ToDecimal(value2);
  61. else if(typeof(T) == typeof(string)) return Convert.ToString(value1).Length >= Convert.ToString(value2).Length;
  62. else return false;
  63. case ComparisionOperator.LessThan:
  64. if(typeof(T) == typeof(int)) return Convert.ToInt32(value1) < Convert.ToInt32(value2);
  65. else if(typeof(T) == typeof(float)) return Convert.ToDecimal(value1) < Convert.ToDecimal(value2);
  66. else if(typeof(T) == typeof(string)) return Convert.ToString(value1).Length < Convert.ToString(value2).Length;
  67. else return false;
  68. case ComparisionOperator.LessOrEqualTo:
  69. if(typeof(T) == typeof(int)) return Convert.ToInt32(value1) <= Convert.ToInt32(value2);
  70. else if(typeof(T) == typeof(float)) return Convert.ToDecimal(value1) <= Convert.ToDecimal(value2);
  71. else if(typeof(T) == typeof(string)) return Convert.ToString(value1).Length <= Convert.ToString(value2).Length;
  72. else return false;
  73. }
  74. }
  75. }
  76. public enum ComparisionOperator
  77. {
  78. EqualTo, NotEqualTo, GreaterThan, GreaterThanOrEqualTo, LessThan, LessOrEqualTo
  79. }
  80. }