NumericComponent.cs 2.6 KB

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