NodeContext.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. using System;
  2. using XGame.Framework.Asset;
  3. using XGame.Framework.Map;
  4. using XGame.Framework.Quality;
  5. using XGame.Framework.Time;
  6. using XGame.Framework.UI;
  7. namespace XGame.Framework.Nodes
  8. {
  9. /// <summary>
  10. /// 每个NodeGroup一个Context
  11. /// </summary>
  12. public sealed class NodeContext : IContext, IDisposable, INodeTreeSetter
  13. {
  14. public IQualityModule Quality { get; private set; }
  15. private ITimeModule _timeModule;
  16. private ITimeProxy _timeProxy;
  17. public ITimeProxy Time => _timeProxy ?? (_timeProxy = new TimeProxy(_timeModule));
  18. public IUIModule UI { get; private set; }
  19. public IMapModule Map { get; private set; }
  20. /// <summary>
  21. /// 资源加载模块,需要时才创建
  22. /// </summary>
  23. private IAssetModule _asset;
  24. /// <summary>
  25. /// 资源加载模块,需要时才创建
  26. /// </summary>
  27. public IAssetModule Asset => _asset ?? (_asset = new AssetModule());
  28. /// <summary>
  29. /// NodeTree
  30. /// </summary>
  31. public INodeTree Tree { get; private set; }
  32. INodeTree INodeTreeSetter.NodeTree { set => Tree = value; }
  33. public NodeContext(IQualityModule qualityModule, ITimeModule timeModule, IUIModule uiModule, IMapModule mapModule)
  34. {
  35. Quality = qualityModule;
  36. _timeModule = timeModule;
  37. UI = uiModule;
  38. Map = mapModule;
  39. }
  40. public NodeContext Clone()
  41. {
  42. var context = new NodeContext(Quality, _timeModule, UI, Map);
  43. (context as INodeTreeSetter).NodeTree = Tree;
  44. return context;
  45. }
  46. /// <summary>
  47. /// 取消定时器、清理资源加载监听等
  48. /// </summary>
  49. public void Clear()
  50. {
  51. _timeProxy?.CancelAll();
  52. _asset?.StopAllAsyncs();
  53. }
  54. void IDisposable.Dispose()
  55. {
  56. Quality = null;
  57. UI = null;
  58. _timeModule = null;
  59. _timeProxy?.CancelAll();
  60. _timeProxy = null;
  61. (_asset as IDisposable)?.Dispose();
  62. _asset = null;
  63. }
  64. }
  65. }