|
@@ -5,69 +5,88 @@ namespace XGame.Framework.Map
|
|
|
{
|
|
|
/// <summary>
|
|
|
/// Entity实例
|
|
|
- /// 只有一个Controller
|
|
|
+ /// 没有Controller
|
|
|
/// </summary>
|
|
|
- /// <typeparam name="TCtrl"></typeparam>
|
|
|
- public abstract class EntityView<TCtrl> : IEntityView, IDisposable where TCtrl : class, IEntityController
|
|
|
+ /// <typeparam name="TViewModel"></typeparam>
|
|
|
+ public abstract class EntityView<TViewModel> : IEntityView, IMapContextSetter, IEntityProxy, IDisposable where TViewModel : class, IEntityViewModel
|
|
|
{
|
|
|
- public MapContext Context { get; private set; }
|
|
|
+ private MapContext _context;
|
|
|
+ protected MapContext Context => _context;
|
|
|
|
|
|
private EntityBehaviour _behaviour;
|
|
|
private IEntityViewModel _viewModel;
|
|
|
- /// <summary>
|
|
|
- /// Entity的controller只有一个
|
|
|
- /// </summary>
|
|
|
- private IEntityController _controller;
|
|
|
- public TCtrl Controller => _controller as TCtrl;
|
|
|
+ protected TViewModel VM => _viewModel as TViewModel;
|
|
|
|
|
|
- public void Init(MapContext context, EntityBehaviour behaviour)
|
|
|
+ private void Init()
|
|
|
{
|
|
|
- Context = context;
|
|
|
- _behaviour = behaviour;
|
|
|
- _viewModel = CtreatViewModel(behaviour.Collector);
|
|
|
- _controller = CreateController();
|
|
|
- if (_controller is IEntityViewModelSetter vmSetter)
|
|
|
- {
|
|
|
- vmSetter.ViewModel = _viewModel;
|
|
|
- }
|
|
|
+ _viewModel = Activator.CreateInstance<TViewModel>();
|
|
|
+ (_viewModel as EntityViewModel)?.Init(_behaviour);
|
|
|
+ //_controller = Activator.CreateInstance<TCtrl>();
|
|
|
+ //if (_controller is IMapContextSetter contextSetter)
|
|
|
+ //{
|
|
|
+ // contextSetter.Context = context;
|
|
|
+ //}
|
|
|
+ //if (_controller is IEntityViewModelSetter vmSetter)
|
|
|
+ //{
|
|
|
+ // vmSetter.ViewModel = _viewModel;
|
|
|
+ //}
|
|
|
OnInited();
|
|
|
}
|
|
|
|
|
|
#region 接口实现
|
|
|
+
|
|
|
+ MapContext IMapContextSetter.Context { set => _context = value; }
|
|
|
+ EntityBehaviour IEntityProxy.Behaviour
|
|
|
+ {
|
|
|
+ get => _behaviour;
|
|
|
+ set
|
|
|
+ {
|
|
|
+ _behaviour = value;
|
|
|
+ Init();
|
|
|
+ }
|
|
|
+ }
|
|
|
public bool Active { get; private set; }
|
|
|
+
|
|
|
|
|
|
public void Enable(object intent)
|
|
|
{
|
|
|
if (Active) return;
|
|
|
Active = true;
|
|
|
_behaviour.gameObject.SetActive(true);
|
|
|
- _controller.OnEnable(intent);
|
|
|
+ OnEnable(intent);
|
|
|
}
|
|
|
|
|
|
+
|
|
|
public void Disable()
|
|
|
{
|
|
|
if (!Active) return;
|
|
|
Active = false;
|
|
|
_behaviour.gameObject.SetActive(false);
|
|
|
- _controller.OnDisable();
|
|
|
+ OnDisable();
|
|
|
}
|
|
|
|
|
|
+
|
|
|
void IDisposable.Dispose()
|
|
|
{
|
|
|
OnDispose();
|
|
|
- _behaviour = null;
|
|
|
- (_controller as IDisposable)?.Dispose();
|
|
|
- _controller = null;
|
|
|
- Context = null;
|
|
|
+ (_viewModel as IDisposable)?.Dispose();
|
|
|
_viewModel = null;
|
|
|
+ _behaviour = null;
|
|
|
+ _context = null;
|
|
|
}
|
|
|
#endregion
|
|
|
|
|
|
protected abstract IEntityViewModel CtreatViewModel(IObjectCollector collector);
|
|
|
- protected abstract IEntityController CreateController();
|
|
|
|
|
|
- protected abstract void OnInited();
|
|
|
- protected abstract void OnDispose();
|
|
|
+ protected abstract void OnEnable(object intent);
|
|
|
+
|
|
|
+ protected abstract void OnDisable();
|
|
|
+ protected virtual void OnInited()
|
|
|
+ {
|
|
|
|
|
|
+ }
|
|
|
+ protected virtual void OnDispose()
|
|
|
+ {
|
|
|
+ }
|
|
|
}
|
|
|
}
|