NumericComponent.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. using System.Collections.Generic;
  2. namespace ETModel
  3. {
  4. [ObjectSystem]
  5. public class NumericComponentAwakeSystem : AwakeSystem<NumericComponent>
  6. {
  7. public override void Awake(NumericComponent self)
  8. {
  9. self.Awake();
  10. }
  11. }
  12. public class NumericComponent: Entity
  13. {
  14. public Dictionary<int, int> NumericDic = new Dictionary<int, int>();
  15. public void Awake()
  16. {
  17. // 这里初始化base值
  18. }
  19. public float GetAsFloat(NumericType numericType)
  20. {
  21. return (float)GetByKey((int)numericType) / 10000;
  22. }
  23. public float GetAsFloat(int numericType)
  24. {
  25. return (float)GetByKey(numericType) / 10000;
  26. }
  27. public int GetAsInt(NumericType numericType)
  28. {
  29. return GetByKey((int)numericType);
  30. }
  31. public int GetAsInt(int numericType)
  32. {
  33. return GetByKey(numericType);
  34. }
  35. public void Set(NumericType nt, float value)
  36. {
  37. this[nt] = (int) (value * 10000);
  38. }
  39. public void Set(NumericType nt, int value)
  40. {
  41. this[nt] = value;
  42. }
  43. public int this[NumericType numericType]
  44. {
  45. get
  46. {
  47. return this.GetByKey((int) numericType);
  48. }
  49. set
  50. {
  51. int v = this.GetByKey((int) numericType);
  52. if (v == value)
  53. {
  54. return;
  55. }
  56. NumericDic[(int)numericType] = value;
  57. Update(numericType);
  58. }
  59. }
  60. private int GetByKey(int key)
  61. {
  62. int value = 0;
  63. this.NumericDic.TryGetValue(key, out value);
  64. return value;
  65. }
  66. public void Update(NumericType numericType)
  67. {
  68. if (numericType < NumericType.Max)
  69. {
  70. return;
  71. }
  72. int final = (int) numericType / 10;
  73. int bas = final * 10 + 1;
  74. int add = final * 10 + 2;
  75. int pct = final * 10 + 3;
  76. int finalAdd = final * 10 + 4;
  77. int finalPct = final * 10 + 5;
  78. // 一个数值可能会多种情况影响,比如速度,加个buff可能增加速度绝对值100,也有些buff增加10%速度,所以一个值可以由5个值进行控制其最终结果
  79. // final = (((base + add) * (100 + pct) / 100) + finalAdd) * (100 + finalPct) / 100;
  80. int result = (int)(((this.GetByKey(bas) + this.GetByKey(add)) * (100 + this.GetAsFloat(pct)) / 100f + this.GetByKey(finalAdd)) * (100 + this.GetAsFloat(finalPct)) / 100f * 10000);
  81. this.NumericDic[final] = result;
  82. Game.EventSystem.Run(EventIdType.NumbericChange, this.Parent.Id, (NumericType) final, result);
  83. }
  84. }
  85. }