NumericComponent.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using System.Collections.Generic;
  2. namespace ET
  3. {
  4. namespace EventType
  5. {
  6. public struct NumbericChange
  7. {
  8. public Entity Parent;
  9. public NumericType NumericType;
  10. public int Old;
  11. public int New;
  12. }
  13. }
  14. [ObjectSystem]
  15. public class NumericComponentAwakeSystem : AwakeSystem<NumericComponent>
  16. {
  17. public override void Awake(NumericComponent self)
  18. {
  19. self.Awake();
  20. }
  21. }
  22. public class NumericComponent: Entity
  23. {
  24. public Dictionary<int, int> NumericDic = new Dictionary<int, int>();
  25. public void Awake()
  26. {
  27. // 这里初始化base值
  28. }
  29. public float GetAsFloat(NumericType numericType)
  30. {
  31. return (float)GetByKey((int)numericType) / 10000;
  32. }
  33. public float GetAsFloat(int numericType)
  34. {
  35. return (float)GetByKey(numericType) / 10000;
  36. }
  37. public int GetAsInt(NumericType numericType)
  38. {
  39. return GetByKey((int)numericType);
  40. }
  41. public int GetAsInt(int numericType)
  42. {
  43. return GetByKey(numericType);
  44. }
  45. public void Set(NumericType nt, float value)
  46. {
  47. this[nt] = (int) (value * 10000);
  48. }
  49. public void Set(NumericType nt, int value)
  50. {
  51. this[nt] = value;
  52. }
  53. public int this[NumericType numericType]
  54. {
  55. get
  56. {
  57. return this.GetByKey((int) numericType);
  58. }
  59. set
  60. {
  61. int v = this.GetByKey((int) numericType);
  62. if (v == value)
  63. {
  64. return;
  65. }
  66. NumericDic[(int)numericType] = value;
  67. Update(numericType);
  68. }
  69. }
  70. private int GetByKey(int key)
  71. {
  72. int value = 0;
  73. this.NumericDic.TryGetValue(key, out value);
  74. return value;
  75. }
  76. public void Update(NumericType numericType)
  77. {
  78. if (numericType < NumericType.Max)
  79. {
  80. return;
  81. }
  82. int final = (int) numericType / 10;
  83. int bas = final * 10 + 1;
  84. int add = final * 10 + 2;
  85. int pct = final * 10 + 3;
  86. int finalAdd = final * 10 + 4;
  87. int finalPct = final * 10 + 5;
  88. // 一个数值可能会多种情况影响,比如速度,加个buff可能增加速度绝对值100,也有些buff增加10%速度,所以一个值可以由5个值进行控制其最终结果
  89. // final = (((base + add) * (100 + pct) / 100) + finalAdd) * (100 + finalPct) / 100;
  90. int old = this.NumericDic[final];
  91. int result = (int)(((this.GetByKey(bas) + this.GetByKey(add)) * (100 + this.GetAsFloat(pct)) / 100f + this.GetByKey(finalAdd)) * (100 + this.GetAsFloat(finalPct)) / 100f * 10000);
  92. this.NumericDic[final] = result;
  93. Game.EventSystem.Publish(new EventType.NumbericChange()
  94. {
  95. Parent = this.Parent,
  96. NumericType = (NumericType) final,
  97. Old = old,
  98. New = result
  99. });
  100. }
  101. }
  102. }