BusinessGroup.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using System.Collections.Generic;
  2. using XGame.Framework.Asyncs;
  3. using XGame.Framework.Interfaces;
  4. using XGame.Framework.Loadable;
  5. namespace XGame.Framework.Nodes
  6. {
  7. public class BusinessGroup : IBusinessGroup, ILoadAsyncHandle
  8. {
  9. private List<IBusiness> _businesses;
  10. private List<IUpdate> _updates;
  11. private List<ILateUpdate> _lateUpdates;
  12. protected NodeContext _context;
  13. public BusinessGroup(NodeContext context)
  14. {
  15. _context = context;
  16. _businesses = new List<IBusiness>();
  17. _updates = new List<IUpdate>();
  18. _lateUpdates = new List<ILateUpdate>();
  19. }
  20. #region 接口实现
  21. public void AddBusiness(IBusiness business)
  22. {
  23. _businesses.Add(business);
  24. if (business is IContextSetter setter){
  25. setter.Context = _context;
  26. }
  27. if (business is IUpdate update)
  28. {
  29. _updates.Add(update);
  30. }
  31. if (business is ILateUpdate lateUpdate)
  32. {
  33. _lateUpdates.Add(lateUpdate);
  34. }
  35. }
  36. #endregion
  37. #region public
  38. public void Enable(object intent)
  39. {
  40. foreach(var business in _businesses)
  41. {
  42. business?.OnEnable(intent);
  43. }
  44. }
  45. public void Disable()
  46. {
  47. foreach(var business in _businesses)
  48. {
  49. business?.OnDisable();
  50. }
  51. }
  52. public void Update(int millisecond)
  53. {
  54. foreach(var update in _updates)
  55. {
  56. update?.Update(millisecond);
  57. }
  58. }
  59. public void LateUpdate(int millisecond)
  60. {
  61. foreach(var lateUpdate in _lateUpdates)
  62. {
  63. lateUpdate?.LateUpdate(millisecond);
  64. }
  65. }
  66. public void Dispose()
  67. {
  68. }
  69. #endregion
  70. public void OnLoadAsync(IAsyncGroup group)
  71. {
  72. foreach(var business in _businesses)
  73. {
  74. (business as ILoadAsyncHandle)?.OnLoadAsync(group);
  75. }
  76. }
  77. public void OnUnloadAsync(IAsyncGroup group)
  78. {
  79. foreach(var business in _businesses)
  80. {
  81. (business as ILoadAsyncHandle)?.OnUnloadAsync(group);
  82. }
  83. }
  84. }
  85. }