TrapAI.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. using FL.Battle.Actions;
  2. using System;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using XGame.Database;
  6. using XGame.Framework;
  7. using XGame.Framework.Components;
  8. using XGame.Framework.Time;
  9. namespace FL.Battle.Components.AI
  10. {
  11. public interface ITrapAIContext
  12. {
  13. long UID { get; }
  14. long MasterId { get; }
  15. int TableId { get; }
  16. ITargetSelector Selector { get; }
  17. ITimeModule Time { get; }
  18. Vector3 Position { get; }
  19. VfxComponent Vfx { get; }
  20. MoveComponent Move { get; }
  21. GameObject ParticleRoot { get; }
  22. }
  23. /// <summary>
  24. /// AI组件和具体的EntityView绑定,其他Component最好不要尝试获取AI组件
  25. /// </summary>
  26. public class TrapAI : Component<ITrapAIContext>
  27. {
  28. /// <summary>
  29. /// 陷阱特效半径
  30. /// </summary>
  31. private const float TRAP_VFX_RAIDUS = 2.56f;
  32. private ITimer _alertTimer;
  33. private int _loopTimes;
  34. private EEntityType _targetType;
  35. private SkillTable _skill;
  36. private SkillVfxsTable _skillVfxs;
  37. protected override void OnDispose()
  38. {
  39. _skill = null;
  40. _skillVfxs = null;
  41. _alertTimer?.Cancel();
  42. _alertTimer = null;
  43. }
  44. public void Start(Vector3 targetPosition, EEntityType targetType)
  45. {
  46. _skill = SkillTableRepo.Get(Context.TableId);
  47. _skillVfxs = SkillVfxsTableRepo.Get(Context.TableId);
  48. _targetType = targetType;
  49. Context.ParticleRoot.SetActive(true);
  50. //Context.AlertTr.localScale = new Vector3(0.5f, 0.5f, 0.1f);
  51. //Context.BombTr.localScale = new Vector3(0.5f, 0.5f, 0.1f);
  52. // 飞到目标位置
  53. var moveArgs = new MoveArgs()
  54. {
  55. target = targetPosition,
  56. speed = _skillVfxs.SkillVfxSpeed,
  57. timeScale = 1,
  58. moveType = (EMoveType)(_skillVfxs.SkillVfxMoveType - 1),
  59. onComplete = OnMoveCompleted
  60. };
  61. if (Enum.TryParse<DG.Tweening.Ease>(_skillVfxs.SkillVfxMoveEase, true, out var ease))
  62. {
  63. moveArgs.ease = ease;
  64. }
  65. Context.Move.MoveTo(moveArgs);
  66. }
  67. private void OnMoveCompleted()
  68. {
  69. Context.ParticleRoot.SetActive(false);
  70. var scaleAct = ObjectPool.Acquire<ScaleAction>();
  71. scaleAct.localScale = Vector3.one * (_skill.Ranges[0] / TRAP_VFX_RAIDUS);
  72. //陷阱特效包含了陷阱展开的表现,特效时长直接加500毫秒给展开表现
  73. var vfxArgs = ObjectPool.Acquire<VfxArgs>();
  74. vfxArgs.vfxName = _skillVfxs.CustomArgs[0];
  75. vfxArgs.duration = _skillVfxs.SkillVfxTime + 500;
  76. vfxArgs.action = scaleAct;
  77. Context.Vfx.Play(vfxArgs);
  78. {
  79. var interval = _skillVfxs.SkillVfxInterval;
  80. _loopTimes = _skillVfxs.SkillVfxTime == -1 ? -1 : _skillVfxs.SkillVfxTime / interval;
  81. _alertTimer = Context.Time.AddDelayLooperTimer(500, interval, OnAlert, _loopTimes);
  82. };
  83. }
  84. /// <summary>
  85. /// 警戒事件的定时回调
  86. /// </summary>
  87. /// <param name="times"></param>
  88. private void OnAlert(int times)
  89. {
  90. if (times != _loopTimes)
  91. {
  92. var target = Context.Selector.FindSync(new FinderInfo()
  93. {
  94. uid = Context.UID,
  95. position = Context.Position,
  96. radius = _skill.Ranges[0]
  97. }, _targetType, ETargetFindType.InRangeAlive);
  98. if (target == null)
  99. return;
  100. }
  101. _alertTimer.Cancel();
  102. _alertTimer = null;
  103. // 达到时间上限直接爆炸,或者有目标触发爆炸
  104. //TODO 爆炸动作
  105. var vfxArgs = ObjectPool.Acquire<VfxArgs>();
  106. vfxArgs.vfxName = _skillVfxs.CustomArgs[1];
  107. vfxArgs.duration = 500;
  108. vfxArgs.onFinish = () =>
  109. //var explodeRange = _skill.Ranges[1] * 2;
  110. //// 缩放倍数是查询范围的两倍
  111. //var tween = Context.BombTr.DOScale(new Vector3(explodeRange, explodeRange, 0.1f), 0.5f);
  112. //tween.onComplete += () =>
  113. {
  114. var finder = new FinderInfo()
  115. {
  116. uid = Context.UID,
  117. position = Context.Position,
  118. radius = _skill.Ranges[1]
  119. };
  120. var targets = new List<ITarget>();
  121. if (Context.Selector.FindTargets(finder, _targetType, ETargetFindType.InRangeAlive, null, ref targets))
  122. {
  123. // 计算受击伤害
  124. foreach (var target in targets)
  125. {
  126. target.Calculation.Damage(-_skill.Damage, Context.MasterId, _skill);
  127. }
  128. }
  129. EventSingle.Instance.Notify(EventDefine.GameMainMapRemoveSimpleEntity, Context.UID);
  130. };
  131. Context.Vfx.Play(vfxArgs);
  132. }
  133. }
  134. }