MapView.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using System;
  2. using XGame.Framework.Components;
  3. using XGame.Framework.Interfaces;
  4. using XGame.Framework.ObjectCollection;
  5. namespace XGame.Framework.Map
  6. {
  7. /// <summary>
  8. /// Map实例
  9. /// </summary>
  10. public abstract class MapView<TViewModel> : IMapView, IMapContextSetter, IDisposable where TViewModel : class, IMapViewModel
  11. {
  12. private MapContext _context;
  13. public MapContext Context => _context;
  14. private MapBehaviour _behaviour;
  15. private TViewModel _viewModel;
  16. public TViewModel VM => _viewModel;
  17. private ComponentGroup _componentGroup;
  18. #region 接口实现
  19. MapContext IMapContextSetter.Context { set => _context = value; }
  20. MapBehaviour IMapView.Behaviour
  21. {
  22. get => _behaviour;
  23. set
  24. {
  25. _behaviour = value;
  26. _viewModel = CtreatViewModel(value.Collector);
  27. _componentGroup = new ComponentGroup(this);
  28. AddComponent(_componentGroup);
  29. }
  30. }
  31. public bool Active { get; private set; }
  32. void IMapView.Enable(object intent)
  33. {
  34. if (Active) return;
  35. Active = true;
  36. _behaviour.gameObject.SetActive(true);
  37. _componentGroup.Enable(intent);
  38. }
  39. void IMapView.Disable()
  40. {
  41. if (!Active) return;
  42. Active = false;
  43. _behaviour.gameObject.SetActive(false);
  44. _componentGroup.Disable();
  45. }
  46. void IUpdate.Update(int millisecond)
  47. {
  48. if (!Active) return;
  49. _componentGroup.Update(millisecond);
  50. }
  51. void ILateUpdate.LateUpdate(int millisecond)
  52. {
  53. if (!Active) return;
  54. _componentGroup.LateUpdate(millisecond);
  55. }
  56. void IDisposable.Dispose()
  57. {
  58. OnDispose();
  59. _componentGroup.Dispose();
  60. _behaviour = null;
  61. (_context as IDisposable)?.Dispose();
  62. _context = null;
  63. _viewModel = null;
  64. }
  65. #endregion
  66. protected abstract TViewModel CtreatViewModel(IObjectCollector collector);
  67. protected abstract void AddComponent(IComponentGroup group);
  68. protected abstract void OnDispose();
  69. }
  70. }