NumericComponent.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. using System.Collections.Generic;
  2. namespace Model
  3. {
  4. [ObjectEvent]
  5. public class NumericComponentSystem : ObjectSystem<NumericComponent>, IAwake
  6. {
  7. public void Awake()
  8. {
  9. this.Get().Awake();
  10. }
  11. }
  12. public class NumericComponent: Component
  13. {
  14. public readonly 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 int GetAsInt(NumericType numericType)
  24. {
  25. return GetByKey((int)numericType);
  26. }
  27. public void Set(NumericType nt, float value)
  28. {
  29. this[nt] = (int) (value * 10000);
  30. }
  31. public void Set(NumericType nt, int value)
  32. {
  33. this[nt] = value;
  34. }
  35. public int this[NumericType numericType]
  36. {
  37. get
  38. {
  39. return this.GetByKey((int) numericType);
  40. }
  41. set
  42. {
  43. int v = this.GetByKey((int) numericType);
  44. if (v == value)
  45. {
  46. return;
  47. }
  48. NumericDic[(int)numericType] = value;
  49. Update(numericType);
  50. }
  51. }
  52. private int GetByKey(int key)
  53. {
  54. int value = 0;
  55. this.NumericDic.TryGetValue(key, out value);
  56. return value;
  57. }
  58. public void Update(NumericType numericType)
  59. {
  60. if (numericType < NumericType.Max)
  61. {
  62. return;
  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. this.NumericDic[final] = ((this.GetByKey(bas) + this.GetByKey(add)) * (100 + this.GetByKey(pct)) / 100 + this.GetByKey(finalAdd)) * (100 + this.GetByKey(finalPct)) / 100;
  73. EventSystem.Instance.Run(EventIdType.NumbericChange, this.Parent.Id, numericType, final);
  74. }
  75. }
  76. }