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