AchievementBase.cs 4.3 KB

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