MapContext.cs 1.6 KB

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