AchievementBase.cs 4.5 KB

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