NumericComponent.cs 2.9 KB

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