MapView.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. _context.Clear();
  46. }
  47. void IUpdate.Update(int millisecond)
  48. {
  49. if (!Active) return;
  50. _componentGroup.Update(millisecond);
  51. }
  52. void ILateUpdate.LateUpdate(int millisecond)
  53. {
  54. if (!Active) return;
  55. _componentGroup.LateUpdate(millisecond);
  56. }
  57. void IDisposable.Dispose()
  58. {
  59. OnDispose();
  60. _componentGroup.Dispose();
  61. _behaviour = null;
  62. (_context as IDisposable)?.Dispose();
  63. _context = null;
  64. _viewModel = null;
  65. }
  66. #endregion
  67. protected TComponent GetComponent<TComponent>() where TComponent : class, IComponent
  68. {
  69. return _componentGroup.Get<TComponent>();
  70. }
  71. protected abstract TViewModel CtreatViewModel(IObjectCollector collector);
  72. protected abstract void AddComponent(IComponentGroup group);
  73. protected abstract void OnDispose();
  74. }
  75. }