NumericComponent.cs 3.8 KB

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