MapContext.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using System;
  2. using XGame.Framework.Quality;
  3. using XGame.Framework.Time;
  4. using XGame.Framework.UI;
  5. namespace XGame.Framework.Map
  6. {
  7. public sealed class MapContext : IDisposable
  8. {
  9. public IQualityModule Quality { get; private set; }
  10. private ITimeModule _timeModule;
  11. private ITimeProxy _timeProxy;
  12. public ITimeProxy Time => _timeProxy ?? (_timeProxy = new TimeProxy(_timeModule));
  13. public IMapModule Map { get; private set; }
  14. public IUIModule UI { get; private set; }
  15. /// <summary>
  16. /// 资源加载模块,需要时才创建
  17. /// </summary>
  18. private IMapAssetModule _assetModule;
  19. public IMapAssetModule Asset => _assetModule ?? (_assetModule = new MapAssetModule(this));
  20. public MapContext(IQualityModule quality, ITimeModule time, IMapModule map, IUIModule ui)
  21. {
  22. Quality = quality;
  23. _timeModule = time;
  24. Map = map;
  25. UI = ui;
  26. }
  27. public MapContext Clone()
  28. {
  29. return new MapContext(Quality, _timeModule, Map, UI);
  30. }
  31. /// <summary>
  32. /// 取消定时器、清理资源加载监听等
  33. /// </summary>
  34. public void Clear()
  35. {
  36. _timeProxy?.CancelAll();
  37. _assetModule?.StopAllAsyncs();
  38. }
  39. void IDisposable.Dispose()
  40. {
  41. Quality = null;
  42. _timeModule = null;
  43. Map = null;
  44. _timeProxy?.CancelAll();
  45. _timeProxy = null;
  46. (_assetModule as IDisposable)?.Dispose();
  47. _assetModule = null;
  48. }
  49. }
  50. public interface IMapContextSetter
  51. {
  52. MapContext Context { set; }
  53. }
  54. }