NumericWatcherComponent.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using System;
  2. using System.Collections.Generic;
  3. namespace ET
  4. {
  5. [ObjectSystem]
  6. public class NumericWatcherComponentAwakeSystem : AwakeSystem<NumericWatcherComponent>
  7. {
  8. public override void Awake(NumericWatcherComponent self)
  9. {
  10. NumericWatcherComponent.Instance = self;
  11. self.Awake();
  12. }
  13. }
  14. public class NumericWatcherComponentLoadSystem : LoadSystem<NumericWatcherComponent>
  15. {
  16. public override void Load(NumericWatcherComponent self)
  17. {
  18. self.Load();
  19. }
  20. }
  21. /// <summary>
  22. /// 监视数值变化组件,分发监听
  23. /// </summary>
  24. public class NumericWatcherComponent : Entity
  25. {
  26. public static NumericWatcherComponent Instance { get; set; }
  27. private Dictionary<NumericType, List<INumericWatcher>> allWatchers;
  28. public void Awake()
  29. {
  30. this.Load();
  31. }
  32. public void Load()
  33. {
  34. this.allWatchers = new Dictionary<NumericType, List<INumericWatcher>>();
  35. HashSet<Type> types = Game.EventSystem.GetTypes(typeof(NumericWatcherAttribute));
  36. foreach (Type type in types)
  37. {
  38. object[] attrs = type.GetCustomAttributes(typeof(NumericWatcherAttribute), false);
  39. foreach (object attr in attrs)
  40. {
  41. NumericWatcherAttribute numericWatcherAttribute = (NumericWatcherAttribute)attr;
  42. INumericWatcher obj = (INumericWatcher)Activator.CreateInstance(type);
  43. if (!this.allWatchers.ContainsKey(numericWatcherAttribute.NumericType))
  44. {
  45. this.allWatchers.Add(numericWatcherAttribute.NumericType, new List<INumericWatcher>());
  46. }
  47. this.allWatchers[numericWatcherAttribute.NumericType].Add(obj);
  48. }
  49. }
  50. }
  51. public void Run(NumericType numericType, long id, long value)
  52. {
  53. List<INumericWatcher> list;
  54. if (!this.allWatchers.TryGetValue(numericType, out list))
  55. {
  56. return;
  57. }
  58. foreach (INumericWatcher numericWatcher in list)
  59. {
  60. numericWatcher.Run(id, value);
  61. }
  62. }
  63. }
  64. }