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