NumericComponent.cs 3.0 KB

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