Node.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. using System;
  2. using XGame.Framework.Asyncs;
  3. using XGame.Framework.Data;
  4. using XGame.Framework.Interfaces;
  5. using XGame.Framework.Loadable;
  6. namespace XGame.Framework.Nodes
  7. {
  8. /// <summary>
  9. /// Node½Úµã
  10. /// ¼¤»î: LoadAsync->Enable
  11. /// Òþ²Ø: Disable->UnloadAsync
  12. /// </summary>
  13. public abstract class Node : AsyncLoadable, INode, IUpdate, ILateUpdate, IDisposable
  14. {
  15. #region ½Ó¿ÚʵÏÖ
  16. public bool Active { get; protected set; }
  17. public NodeContext Context { get; private set; }
  18. private DataGroup _dataGroup;
  19. private NodeComponentGroup _componentGroup;
  20. public void Init(NodeContext context)
  21. {
  22. Context = context;
  23. _dataGroup = new DataGroup();
  24. _componentGroup = new NodeComponentGroup(Context);
  25. AddData(_dataGroup);
  26. AddComponent(_componentGroup);
  27. OnInited();
  28. }
  29. public void Enable(object intent = null)
  30. {
  31. if (Active) return;
  32. if (State != LoadState.Loaded) return;
  33. Active = true;
  34. _componentGroup.Enable(intent);
  35. }
  36. public void Disable()
  37. {
  38. if (!Active) return;
  39. if (State != LoadState.Loaded) return;
  40. Active = false;
  41. _componentGroup.Disable();
  42. }
  43. public void Update(int millisecond)
  44. {
  45. if (!Active) return;
  46. _componentGroup.Update(millisecond);
  47. }
  48. public void LateUpdate(int millisecond)
  49. {
  50. if (!Active) return;
  51. _componentGroup.LateUpdate(millisecond);
  52. }
  53. void IDisposable.Dispose()
  54. {
  55. OnDispose();
  56. ClearAsyncs();
  57. _componentGroup.Dispose();
  58. //(Context as IDisposable).Dispose();
  59. }
  60. #endregion
  61. protected override void OnLoadAsync(IAsyncGroup group)
  62. {
  63. _dataGroup.OnLoadAsync(group);
  64. _componentGroup.OnLoadAsync(group);
  65. }
  66. protected override void OnUnloadAsync(IAsyncGroup group)
  67. {
  68. _dataGroup.OnUnloadAsync(group);
  69. _componentGroup.OnUnloadAsync(group);
  70. }
  71. protected abstract void AddData(IDataGroup group);
  72. protected abstract void AddComponent(INodeComponentGroup group);
  73. protected abstract void OnInited();
  74. protected abstract void OnDispose();
  75. }
  76. }