NumericComponent.cs 3.0 KB

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