AttackActor.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. using DG.Tweening;
  2. using FL.Battle.Actions;
  3. using FL.Battle.Components;
  4. using System;
  5. using UnityEngine;
  6. using XGame.Database;
  7. using XGame.Framework;
  8. using XGame.Framework.Interfaces;
  9. namespace FL.Battle.Skills
  10. {
  11. /// <summary>
  12. /// 普通攻击
  13. /// </summary>
  14. internal class AttackActor : ISkillActor
  15. {
  16. public ISkillContext Context { get; set; }
  17. void ISkillActor.Play(int skillId, ITarget target, Action<int, int> onCompleted)
  18. {
  19. var owner = Context.Entity;
  20. if (Context.Entity.EntityType != EEntityType.Partner)
  21. { // 玩家或者怪物
  22. PlaySkillAtk(target, onCompleted);
  23. //Context.Spine.Play(ActionName.attack, () =>
  24. //{
  25. // OnHitFrame();
  26. // OnAttackEnd();
  27. // Context.Spine.Play(ActionName.idle);
  28. // onCompleted?.Invoke();
  29. //});
  30. return;
  31. }
  32. // 随从
  33. PartnerType partnerType = (PartnerType)((Context.Entity.TableId / 100) % 10);
  34. //单手剑、双手剑、法杖使用法术类型攻击
  35. var isSkill = partnerType == PartnerType.Sword || partnerType == PartnerType.Wand;
  36. if (isSkill)
  37. {
  38. PlaySkillAtk(target, onCompleted);
  39. }
  40. else
  41. {
  42. PartnerNormalAtk(target, onCompleted);
  43. }
  44. }
  45. private void PlaySkillAtk(ITarget target, Action<int, int> onCompleted)
  46. {
  47. var actArgs = new AnimationPlayArgs()
  48. {
  49. aniName = EAnimationName.attack,
  50. onCompleted = () =>
  51. {
  52. //Context.Animator.Play(EAnimationName.idle/* + UnityEngine.Random.Range(0, 100) % 2*/);
  53. if (target == null || target.IsDead)
  54. {
  55. //OnAttackEnd();
  56. onCompleted.SafeInvoke(0, Context.Entity.Attr.AttackCD);
  57. return;
  58. }
  59. var to = target.Position;
  60. var actMove = ObjectPool.Acquire<MoveBezierAction>();
  61. actMove.args.from = Context.Position;
  62. actMove.args.to = to;
  63. actMove.args.duration = 500;
  64. var bulletArgs = ObjectPool.Acquire<VfxArgs>();
  65. bulletArgs.vfxName = Context.Entity.EntityType == EEntityType.Partner ? "effect_atk_bullet_20181" : "atk_007";
  66. bulletArgs.duration = 500;
  67. bulletArgs.followType = EVfxFollowType.World;
  68. bulletArgs.action = actMove;
  69. bulletArgs.onFinish = () =>
  70. {
  71. OnHitFrame(target);
  72. };
  73. var actPosition = ObjectPool.Acquire<PositionAction>();
  74. actPosition.position = to;
  75. var hitArgs = ObjectPool.Acquire<VfxArgs>();
  76. hitArgs.vfxName = Context.Entity.EntityType == EEntityType.Partner ? "effect_atk_hit_20181" : "hit_007";
  77. hitArgs.duration = 2000;
  78. hitArgs.followType = EVfxFollowType.World;
  79. hitArgs.action = actPosition;
  80. bulletArgs.next = hitArgs;
  81. Context.Vfx.Play(bulletArgs);
  82. //var dto = new SkillEffectDto()
  83. //{
  84. // skillEffect = "",
  85. // skillTime = 667,
  86. // from = Context.Position,
  87. // to = _targetView.Position,
  88. // hpAdd = new HpAddDto()
  89. // {
  90. // position = _targetView.Position,
  91. // hpValue = hpValue,
  92. // }
  93. //};
  94. //if (Context.Entity.EntityType == EEntityType.Partner)
  95. //{
  96. // dto.hitEffect = "effect_atk_hit_20181";
  97. // dto.bulletEffect = "effect_atk_bullet_20181";
  98. //}
  99. //else
  100. //{
  101. // dto.hitEffect = "hit_007";
  102. // dto.bulletEffect = "atk_007";
  103. //}
  104. //EventSingle.Instance.Notify(EventDefine.GameMainMapSkillEffect, dto);
  105. //OnAttackEnd();
  106. onCompleted.SafeInvoke(0, Context.Entity.Attr.AttackCD);
  107. }
  108. };
  109. Context.Animator.Play(actArgs);
  110. }
  111. private void PartnerNormalAtk(ITarget target, Action<int, int> onCompleted)
  112. {
  113. var from = Context.Position;
  114. var to = target.Position;
  115. var distance = Vector3.Distance(from, to);
  116. var atkRange = 0;// _target.Attr.Radius;
  117. if (distance > atkRange)
  118. { // 大于攻击距离
  119. //Log.Info($"PlayAttack:Owner:{Handle.Owner.EntityId} Target:{_target.EntityId}");
  120. var dir = (to - from).normalized;
  121. var moveDis = distance - atkRange;
  122. var atkPosition = Context.ActionRoot.localPosition + dir * moveDis;
  123. //var moveTime = 0.42f;// Mathf.Abs(moveDis) / 5;
  124. Context.ActionRoot.localPosition = atkPosition;
  125. var actArgs = new AnimationPlayArgs()
  126. {
  127. aniName = EAnimationName.attack,
  128. onCompleted = () =>
  129. {
  130. //Context.Animator.Play(EAnimationName.idle/* + UnityEngine.Random.Range(0, 100) % 2*/);
  131. Context.ActionRoot.localPosition = Vector3.zero;
  132. var endArgs = ObjectPool.Acquire<VfxArgs>();
  133. endArgs.vfxName = "effect_melee_appear";
  134. endArgs.duration = 500;
  135. Context.Vfx.Play(endArgs);
  136. OnHitFrame(target);
  137. //OnAttackEnd();
  138. onCompleted.SafeInvoke(0, Context.Entity.Attr.AttackCD);
  139. }
  140. };
  141. Context.Animator.Play(actArgs);
  142. var startArgs = ObjectPool.Acquire<VfxArgs>();
  143. startArgs.vfxName = "effect_melee_blink";
  144. startArgs.duration = 500;
  145. Context.Vfx.Play(startArgs);
  146. //MoveTo(atkPosition, moveTime, () =>
  147. //{
  148. // OnHitFrame();
  149. // //moveback
  150. // MoveTo(Vector3.zero, moveTime, () =>
  151. // {
  152. // OnAttackEnd();
  153. // onCompleted?.Invoke();
  154. // });
  155. //});
  156. }
  157. else
  158. {
  159. OnHitFrame(target);
  160. //OnAttackEnd();
  161. onCompleted.SafeInvoke(0, Context.Entity.Attr.AttackCD);
  162. }
  163. }
  164. public void Stop()
  165. {
  166. Context.ActionRoot.DOKill();
  167. Context.ActionRoot.localPosition = Vector3.zero;
  168. }
  169. private void MoveTo(Vector3 to, float duration, Action callback)
  170. {
  171. var tween = Context.ActionRoot.DOLocalMove(to, duration);
  172. tween.onComplete = () =>
  173. {
  174. callback.SafeInvoke();
  175. };
  176. }
  177. private void OnHitFrame(ITarget target)
  178. { // 攻击帧回调
  179. //TODO 目标受击
  180. if (target == null || target.IsDead)
  181. return;
  182. target.Calculation.Damage(-Context.Entity.Attr.Attack, EElementType.None);
  183. }
  184. void IReference.Clear()
  185. {
  186. Stop();
  187. Context = null;
  188. }
  189. //private void OnAttackEnd()
  190. //{
  191. // _lastAtkTime = Context.Time.GetNowTime();
  192. // //冷却计时
  193. // var time = _lastAtkTime + Context.Entity.Attr.AttackCD;
  194. // _cdAlarm = Context.Time.AddAlarm(() =>
  195. // {
  196. // Context.EnqueueSkill(0);
  197. // }, time);
  198. //}
  199. }
  200. }