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