NodeComponentGroup.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using System;
  2. using System.Collections.Generic;
  3. using XGame.Framework.Asyncs;
  4. using XGame.Framework.Interfaces;
  5. using XGame.Framework.Loadable;
  6. namespace XGame.Framework.Nodes
  7. {
  8. /// <summary>
  9. /// 业务集合
  10. /// 主要用于实现业务的Enable、Update、LateUpdate、Load、Unload操作
  11. /// </summary>
  12. public class NodeComponentGroup : INodeComponentGroup, ILoadAsyncHandle
  13. {
  14. private List<INodeComponent> _components;
  15. private List<IUpdate> _updates;
  16. private List<ILateUpdate> _lateUpdates;
  17. protected NodeContext _context;
  18. public NodeComponentGroup(NodeContext context)
  19. {
  20. _context = context;
  21. _components = new List<INodeComponent>();
  22. _updates = new List<IUpdate>();
  23. _lateUpdates = new List<ILateUpdate>();
  24. }
  25. #region 接口实现
  26. public void Add(INodeComponent component)
  27. {
  28. _components.Add(component);
  29. component.Bind(_context);
  30. if (component is IUpdate update)
  31. {
  32. _updates.Add(update);
  33. }
  34. if (component is ILateUpdate lateUpdate)
  35. {
  36. _lateUpdates.Add(lateUpdate);
  37. }
  38. }
  39. #endregion
  40. #region public
  41. public void Enable(object intent)
  42. {
  43. foreach(var component in _components)
  44. {
  45. component?.OnEnable(intent);
  46. }
  47. }
  48. public void Disable()
  49. {
  50. foreach(var component in _components)
  51. {
  52. component?.OnDisable();
  53. }
  54. }
  55. public void Update(int millisecond)
  56. {
  57. foreach(var update in _updates)
  58. {
  59. update?.Update(millisecond);
  60. }
  61. }
  62. public void LateUpdate(int millisecond)
  63. {
  64. foreach(var lateUpdate in _lateUpdates)
  65. {
  66. lateUpdate?.LateUpdate(millisecond);
  67. }
  68. }
  69. public void Dispose()
  70. {
  71. foreach (var component in _components)
  72. {
  73. (component as IDisposable)?.Dispose();
  74. }
  75. }
  76. #endregion
  77. public void OnLoadAsync(IAsyncGroup group)
  78. {
  79. foreach(var component in _components)
  80. {
  81. (component as ILoadAsyncHandle)?.OnLoadAsync(group);
  82. }
  83. }
  84. public void OnUnloadAsync(IAsyncGroup group)
  85. {
  86. foreach(var component in _components)
  87. {
  88. (component as ILoadAsyncHandle)?.OnUnloadAsync(group);
  89. }
  90. }
  91. }
  92. }