NumericComponent.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 Entity Parent;
  12. public int NumericType;
  13. public long Old;
  14. public long New;
  15. }
  16. }
  17. [FriendClass(typeof(NumericComponent))]
  18. public static class NumericComponentSystem
  19. {
  20. public static float GetAsFloat(this NumericComponent self, int numericType)
  21. {
  22. return (float)self.GetByKey(numericType) / 10000;
  23. }
  24. public static int GetAsInt(this NumericComponent self, int numericType)
  25. {
  26. return (int)self.GetByKey(numericType);
  27. }
  28. public static long GetAsLong(this NumericComponent self, int numericType)
  29. {
  30. return self.GetByKey(numericType);
  31. }
  32. public static void Set(this NumericComponent self, int nt, float value)
  33. {
  34. self[nt] = (int) (value * 10000);
  35. }
  36. public static void Set(this NumericComponent self, int nt, int value)
  37. {
  38. self[nt] = value;
  39. }
  40. public static void Set(this NumericComponent self, int nt, long value)
  41. {
  42. self[nt] = value;
  43. }
  44. public static void SetNoEvent(this NumericComponent self, int numericType, long value)
  45. {
  46. self.Insert(numericType,value,false);
  47. }
  48. public static void Insert(this NumericComponent self, int numericType, long value,bool isPublicEvent = true)
  49. {
  50. long oldValue = self.GetByKey(numericType);
  51. if (oldValue == value)
  52. {
  53. return;
  54. }
  55. self.NumericDic[numericType] = value;
  56. if (numericType >= NumericType.Max)
  57. {
  58. self.Update(numericType,isPublicEvent);
  59. return;
  60. }
  61. if (isPublicEvent)
  62. {
  63. Game.EventSystem.Publish(new NumbericChange() {New = value, Old = oldValue, Parent = self.Parent, NumericType = numericType});
  64. }
  65. }
  66. public static long GetByKey(this NumericComponent self, int key)
  67. {
  68. long value = 0;
  69. self.NumericDic.TryGetValue(key, out value);
  70. return value;
  71. }
  72. public static void Update(this NumericComponent self, int numericType,bool isPublicEvent)
  73. {
  74. int final = (int) numericType / 10;
  75. int bas = final * 10 + 1;
  76. int add = final * 10 + 2;
  77. int pct = final * 10 + 3;
  78. int finalAdd = final * 10 + 4;
  79. int finalPct = final * 10 + 5;
  80. // 一个数值可能会多种情况影响,比如速度,加个buff可能增加速度绝对值100,也有些buff增加10%速度,所以一个值可以由5个值进行控制其最终结果
  81. // final = (((base + add) * (100 + pct) / 100) + finalAdd) * (100 + finalPct) / 100;
  82. long result = (long)(((self.GetByKey(bas) + self.GetByKey(add)) * (100 + self.GetAsFloat(pct)) / 100f + self.GetByKey(finalAdd)) * (100 + self.GetAsFloat(finalPct)) / 100f);
  83. self.Insert(final,result,isPublicEvent);
  84. }
  85. }
  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. }