StatModifiersController.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Sirenix.OdinInspector;
  5. namespace KairoEngine.Stats
  6. {
  7. [System.Serializable]
  8. public class StatModifiersController
  9. {
  10. [ListDrawerSettings(HideAddButton = false, HideRemoveButton = false, DraggableItems = false, Expanded = true, ShowPaging = false, ShowItemCount = false)]
  11. [PropertyOrder(1), LabelText("Modifiers"), OnCollectionChanged("Calculate")] public List<StatModifier> list = new List<StatModifier>();
  12. [HideInInspector] public int value = 0;
  13. [HideInInspector] public int minValue = 0;
  14. [HideInInspector] public int maxValue = 0;
  15. [System.NonSerialized] public Stat stat;
  16. public string pretyValue()
  17. {
  18. if(value > 0) return $"+{value}";
  19. else if (value == 0) return " 0 ";
  20. else return $"{value}";
  21. }
  22. public string pretyCount()
  23. {
  24. if(list.Count > 99) return $" {list.Count} ";
  25. else if (list.Count > 9) return $" {list.Count} ";
  26. else return $" {list.Count} ";
  27. }
  28. public StatModifiersController(Stat stat)
  29. {
  30. this.stat = stat;
  31. }
  32. public void Add(StatModifier statModifier)
  33. {
  34. list.Add(statModifier);
  35. Calculate();
  36. }
  37. public void Remove(StatModifier statModifier)
  38. {
  39. for (int i = 0; i < list.Count; i++)
  40. {
  41. if(statModifier.Equals(list[i]))
  42. {
  43. list.Remove(list[i]);
  44. Calculate();
  45. return;
  46. }
  47. }
  48. }
  49. public bool HasModifier(StatModifier statModifier)
  50. {
  51. for (int i = 0; i < list.Count; i++)
  52. {
  53. if(statModifier.Equals(list[i])) return true;
  54. }
  55. return false;
  56. }
  57. public void Calculate()
  58. {
  59. if(stat == null) return;
  60. value = CalculateModifier("", stat.GetValueRaw());
  61. minValue = CalculateModifier("min", stat.GetMinValueRaw());
  62. maxValue = CalculateModifier("max", stat.GetMaxValueRaw());
  63. //Debug.Log($"{stat.title} Modifiers: value={value} minValue={minValue} maxValue={maxValue}");
  64. }
  65. private int CalculateModifier(string valueType, int statValue)
  66. {
  67. int values = 0;
  68. int percentage = 0;
  69. for (int i = 0; i < list.Count; i++)
  70. {
  71. if(list[i].endOp == "%" && list[i].valueType == valueType)
  72. {
  73. if(list[i].op == "+") percentage += list[i].value;
  74. else percentage -= list[i].value;
  75. }
  76. else if(list[i].endOp == "" && list[i].valueType == valueType)
  77. {
  78. if(list[i].op == "+") values += list[i].value;
  79. else values -= list[i].value;
  80. }
  81. }
  82. if(percentage != 0) percentage = ((statValue + values) * percentage) /100;
  83. return percentage + values;
  84. }
  85. }
  86. }