NumericWatcherComponent.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. using System;
  2. using System.Collections.Generic;
  3. namespace ETModel
  4. {
  5. [ObjectSystem]
  6. public class NumericWatcherComponentAwakeSystem : AwakeSystem<NumericWatcherComponent>
  7. {
  8. public override void Awake(NumericWatcherComponent self)
  9. {
  10. self.Awake();
  11. }
  12. }
  13. [ObjectSystem]
  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 : Component
  25. {
  26. private Dictionary<NumericType, List<INumericWatcher>> allWatchers;
  27. public void Awake()
  28. {
  29. this.Load();
  30. }
  31. public void Load()
  32. {
  33. this.allWatchers = new Dictionary<NumericType, List<INumericWatcher>>();
  34. List<Type> types = Game.EventSystem.GetTypes(typeof(NumericWatcherAttribute));
  35. foreach (Type type in types)
  36. {
  37. object[] attrs = type.GetCustomAttributes(typeof(NumericWatcherAttribute), false);
  38. foreach (object attr in attrs)
  39. {
  40. NumericWatcherAttribute numericWatcherAttribute = (NumericWatcherAttribute)attr;
  41. INumericWatcher obj = (INumericWatcher)Activator.CreateInstance(type);
  42. if (!this.allWatchers.ContainsKey(numericWatcherAttribute.NumericType))
  43. {
  44. this.allWatchers.Add(numericWatcherAttribute.NumericType, new List<INumericWatcher>());
  45. }
  46. this.allWatchers[numericWatcherAttribute.NumericType].Add(obj);
  47. }
  48. }
  49. }
  50. public void Run(NumericType numericType, long id, int value)
  51. {
  52. List<INumericWatcher> list;
  53. if (!this.allWatchers.TryGetValue(numericType, out list))
  54. {
  55. return;
  56. }
  57. foreach (INumericWatcher numericWatcher in list)
  58. {
  59. numericWatcher.Run(id, value);
  60. }
  61. }
  62. }
  63. }