ComponentGroup.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using System;
  2. using System.Collections.Generic;
  3. using XGame.Framework.Interfaces;
  4. namespace XGame.Framework.Components
  5. {
  6. public class ComponentGroup : IComponentGroup
  7. {
  8. private Dictionary<Type, IComponent> _components;
  9. private List<IUpdate> _updates;
  10. private List<ILateUpdate> _lateUpdates;
  11. private object _context;
  12. public ComponentGroup(object context)
  13. {
  14. _context = context;
  15. _components = new Dictionary<Type, IComponent>();
  16. _updates = new List<IUpdate>();
  17. _lateUpdates = new List<ILateUpdate>();
  18. }
  19. public void Add(IComponent component)
  20. {
  21. if (component == null) return;
  22. _components.Add(component.GetType(), component);
  23. component.Bind(_context);
  24. if (component is IUpdate update)
  25. {
  26. _updates.Add(update);
  27. }
  28. if (component is ILateUpdate lateUpdate)
  29. {
  30. _lateUpdates.Add(lateUpdate);
  31. }
  32. }
  33. public void Remove(IComponent component)
  34. {
  35. if (component == null) return;
  36. _components.Remove(component.GetType());
  37. if (component is IUpdate update)
  38. {
  39. _updates.Remove(update);
  40. }
  41. if (component is ILateUpdate lateUpdate)
  42. {
  43. _lateUpdates.Remove(lateUpdate);
  44. }
  45. }
  46. public TComponent Get<TComponent>() where TComponent : IComponent
  47. {
  48. if (_components.TryGetValue(typeof(TComponent), out var component))
  49. {
  50. return (TComponent)component;
  51. }
  52. return default;
  53. }
  54. public void Enable(object intent)
  55. {
  56. foreach (var component in _components.Values)
  57. {
  58. if (component is IActiveable impl)
  59. {
  60. impl.Enable(intent);
  61. }
  62. }
  63. }
  64. public void Disable()
  65. {
  66. foreach (var component in _components.Values)
  67. {
  68. if (component is IActiveable impl)
  69. {
  70. impl.Disable();
  71. }
  72. }
  73. }
  74. public void Update(int millisecond)
  75. {
  76. foreach (var update in _updates)
  77. {
  78. update?.Update(millisecond);
  79. }
  80. }
  81. public void LateUpdate(int millisecond)
  82. {
  83. foreach (var lateUpdate in _lateUpdates)
  84. {
  85. lateUpdate?.LateUpdate(millisecond);
  86. }
  87. }
  88. public void Reset()
  89. {
  90. foreach (var component in _components.Values)
  91. {
  92. (component as IReset)?.Reset();
  93. }
  94. }
  95. public void Clear()
  96. {
  97. foreach (var component in _components.Values)
  98. {
  99. (component as IReference)?.Clear();
  100. }
  101. }
  102. public void Dispose()
  103. {
  104. foreach (var component in _components.Values)
  105. {
  106. (component as IDisposable)?.Dispose();
  107. }
  108. _components.Clear();
  109. }
  110. }
  111. }