NumericComponent.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 long Old;
  11. public long 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, long> NumericDic = new Dictionary<int, long>();
  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 (int)GetByKey((int)numericType);
  40. }
  41. public long GetAsLong(NumericType numericType)
  42. {
  43. return GetByKey((int)numericType);
  44. }
  45. public int GetAsInt(int numericType)
  46. {
  47. return (int)GetByKey(numericType);
  48. }
  49. public long GetAsLong(int numericType)
  50. {
  51. return GetByKey(numericType);
  52. }
  53. public void Set(NumericType nt, float value)
  54. {
  55. this[nt] = (int) (value * 10000);
  56. }
  57. public void Set(NumericType nt, int value)
  58. {
  59. this[nt] = value;
  60. }
  61. public void Set(NumericType nt, long value)
  62. {
  63. this[nt] = value;
  64. }
  65. public long this[NumericType numericType]
  66. {
  67. get
  68. {
  69. return this.GetByKey((int) numericType);
  70. }
  71. set
  72. {
  73. long v = this.GetByKey((int) numericType);
  74. if (v == value)
  75. {
  76. return;
  77. }
  78. NumericDic[(int)numericType] = value;
  79. Update(numericType);
  80. }
  81. }
  82. private long GetByKey(int key)
  83. {
  84. long value = 0;
  85. this.NumericDic.TryGetValue(key, out value);
  86. return value;
  87. }
  88. public void Update(NumericType numericType)
  89. {
  90. if (numericType < NumericType.Max)
  91. {
  92. return;
  93. }
  94. int final = (int) numericType / 10;
  95. int bas = final * 10 + 1;
  96. int add = final * 10 + 2;
  97. int pct = final * 10 + 3;
  98. int finalAdd = final * 10 + 4;
  99. int finalPct = final * 10 + 5;
  100. // 一个数值可能会多种情况影响,比如速度,加个buff可能增加速度绝对值100,也有些buff增加10%速度,所以一个值可以由5个值进行控制其最终结果
  101. // final = (((base + add) * (100 + pct) / 100) + finalAdd) * (100 + finalPct) / 100;
  102. long old = this.NumericDic[final];
  103. long result = (long)(((this.GetByKey(bas) + this.GetByKey(add)) * (100 + this.GetAsFloat(pct)) / 100f + this.GetByKey(finalAdd)) * (100 + this.GetAsFloat(finalPct)) / 100f * 10000);
  104. this.NumericDic[final] = result;
  105. Game.EventSystem.Publish(new EventType.NumbericChange()
  106. {
  107. Parent = this.Parent,
  108. NumericType = (NumericType) final,
  109. Old = old,
  110. New = result
  111. }).Coroutine();
  112. }
  113. }
  114. }