BattleEntityView.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using FL.Battle;
  2. using FL.Battle.Components;
  3. using FL.Battle.Components.AI;
  4. using System;
  5. using UnityEngine;
  6. using XGame.Framework.Map;
  7. using XGame.Framework.Time;
  8. namespace FL.Map
  9. {
  10. /// <summary>
  11. /// 玩家、随从、怪物等有IEntity的战斗对象基类
  12. /// </summary>
  13. /// <typeparam name="TViewModel"></typeparam>
  14. public abstract class BattleEntityView<TViewModel> : EntityView<TViewModel>, IMoveContext, IBuffsContext, ICombatContext, IBattleEntityAIContext, ITarget where TViewModel : EntityViewModel
  15. {
  16. public IMapAssetModule Asset => Context.Asset;
  17. public ITimeModule Time => Context.Time;
  18. private IEntity _entity;
  19. public IEntity Entity
  20. {
  21. get => _entity;
  22. set
  23. {
  24. _entity = value;
  25. LocalPosition = value?.BirthPosition ?? Vector3.zero;
  26. States.Reset();
  27. }
  28. }
  29. public bool IsDead => _entity.IsDead;
  30. #region 状态
  31. //private ITimer _deadTimer;
  32. //public EEntityState State
  33. //{
  34. // get => _entity.State;
  35. // set
  36. // {
  37. // if (value != EEntityState.Idle && value == _entity.State)
  38. // {
  39. // return;
  40. // }
  41. // if (_entity.State == EEntityState.Dead)
  42. // { // 已经死亡,不能再修改状态
  43. // return;
  44. // }
  45. // _entity.State = value;
  46. // //VM.StateText.text = value.ToString();
  47. // //Animation
  48. // if (value == EEntityState.Dead)
  49. // {
  50. // //TODO 死亡动作 停止所有组件
  51. // _deadTimer = Context.Time.AddDelayTimer(500, () =>
  52. // { // 死亡事件通知
  53. // _deadTimer = null;
  54. // EventSingle.Instance.Notify(EventDefine.GameMainMapRemoveEntity, _entity.EntityId);
  55. // });
  56. // }
  57. // OnStateChanged();
  58. // }
  59. //}
  60. //protected abstract void OnStateChanged();
  61. #endregion
  62. #region IMoveContext
  63. public bool IsMoving => Move.IsMoving;
  64. public Transform OwnerTr => VM.Tr;
  65. #endregion
  66. #region 目标选择
  67. public abstract Transform UIPoint { get; }
  68. /// <summary>
  69. /// 默认的目标类型
  70. /// </summary>
  71. protected abstract EEntityType DefaultTargetType { get; }
  72. public ITargetSelector Selector { get; set; }
  73. public void FindTargetAsync(Action<ITarget> onSelected)
  74. {
  75. Selector.FindAsync(this, DefaultTargetType, ETargetFindType.Nearest, onSelected);
  76. }
  77. #endregion
  78. #region 组件
  79. public VfxComponent Vfx => GetComponent<VfxComponent>();
  80. public MoveComponent Move => GetComponent<MoveComponent>();
  81. public SkillComponent Skill => GetComponent<SkillComponent>();
  82. public BuffsComponent Buffs => GetComponent<BuffsComponent>();
  83. public BattleEntityAI CommonAI => GetComponent<BattleEntityAI>();
  84. public StatesComponent States => GetComponent<StatesComponent>();
  85. public ICombatCalculation Calculation => GetComponent<CombatComponent>();
  86. public abstract EpigraphicsComponent Epigraphics { get; }
  87. #endregion
  88. protected override void OnEnable(object intent)
  89. {
  90. }
  91. protected override void OnDisable()
  92. {
  93. Selector = null;
  94. }
  95. /// <summary>
  96. /// 重置状态
  97. /// </summary>
  98. /// <param name="isOnlyComponent">是否只重置Component</param>
  99. public void Reset(bool isOnlyComponent = false)
  100. {
  101. Group.Reset();
  102. //States.State = EEntityState.Idle;
  103. if (isOnlyComponent)
  104. return;
  105. LocalPosition = _entity.BirthPosition;
  106. }
  107. }
  108. }