EntityView.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using System;
  2. using UnityEngine;
  3. using XGame.Framework.Components;
  4. using XGame.Framework.Interfaces;
  5. namespace XGame.Framework.Map
  6. {
  7. /// <summary>
  8. /// Entity实例
  9. /// </summary>
  10. /// <typeparam name="TViewModel"></typeparam>
  11. public abstract class EntityView<TViewModel> : IEntityView, IMapContextSetter, IEntityProxy, IDisposable where TViewModel : class, IEntityViewModel
  12. {
  13. private MapContext _context;
  14. protected MapContext Context => _context;
  15. private EntityBehaviour _behaviour;
  16. private TViewModel _viewModel;
  17. private ComponentGroup _componentGroup;
  18. protected ComponentGroup Group => _componentGroup;
  19. protected TViewModel VM => _viewModel;
  20. public Vector3 LocalPosition { get => _behaviour.transform.localPosition; set => _behaviour.transform.localPosition = value; }
  21. public Vector3 Position { get => _behaviour.transform.position; set => _behaviour.transform.position = value; }
  22. public Vector3 Rotation { get => _behaviour.transform.rotation.eulerAngles; set => _behaviour.transform.rotation = Quaternion.Euler(value); }
  23. protected bool IsInited { get; private set; }
  24. private void Init()
  25. {
  26. IsInited = true;
  27. _viewModel = Activator.CreateInstance<TViewModel>();
  28. (_viewModel as EntityViewModel)?.Init(_behaviour);
  29. if (_componentGroup == null)
  30. {
  31. _componentGroup = new ComponentGroup(this);
  32. }
  33. OnInited();
  34. }
  35. #region 接口实现
  36. public TComponent GetComponent<TComponent>(bool autoCreate = true) where TComponent : class, IComponent
  37. {
  38. var component = _componentGroup.Get<TComponent>();
  39. if (autoCreate && component == null)
  40. {
  41. component = Activator.CreateInstance<TComponent>();
  42. //component.Bind(this);
  43. _componentGroup.Add(component);
  44. if (Active && component is IActiveable activeable)
  45. { // EntityView处于激活状态,激活Component
  46. activeable.Enable();
  47. }
  48. }
  49. return component;
  50. }
  51. MapContext IMapContextSetter.Context { set => _context = value; }
  52. EntityBehaviour IEntityProxy.Behaviour
  53. {
  54. get => _behaviour;
  55. set
  56. {
  57. _behaviour = value;
  58. Init();
  59. }
  60. }
  61. public bool Active { get; private set; }
  62. public void Enable(object intent)
  63. {
  64. if (Active) return;
  65. Active = true;
  66. _behaviour.gameObject.SetActive(true);
  67. _componentGroup.Enable(intent);
  68. OnEnable(intent);
  69. }
  70. public void Disable()
  71. {
  72. if (!Active) return;
  73. Active = false;
  74. _behaviour.gameObject.SetActive(false);
  75. _componentGroup.Disable();
  76. OnDisable();
  77. }
  78. void IDisposable.Dispose()
  79. {
  80. IsInited = false;
  81. OnDispose();
  82. _componentGroup.Dispose();
  83. (_viewModel as IDisposable)?.Dispose();
  84. _viewModel = null;
  85. _behaviour = null;
  86. _context = null;
  87. }
  88. public void Clear()
  89. {
  90. if (!IsInited) return;
  91. IsInited = false;
  92. OnDispose();
  93. _componentGroup.Clear();
  94. (_viewModel as IDisposable)?.Dispose();
  95. _viewModel = null;
  96. _behaviour = null;
  97. _context = null;
  98. }
  99. #endregion
  100. //protected abstract IEntityViewModel CtreatViewModel(IObjectCollector collector);
  101. protected abstract void OnEnable(object intent);
  102. protected abstract void OnDisable();
  103. protected virtual void OnInited()
  104. {
  105. }
  106. protected virtual void OnDispose()
  107. {
  108. }
  109. }
  110. }