BulletAI.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. using FL.Battle.Actions;
  2. using FL.Battle.Skills;
  3. using System;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. using XGame;
  7. using XGame.Database;
  8. using XGame.Framework;
  9. using XGame.Framework.Components;
  10. namespace FL.Battle.Components.AI
  11. {
  12. public interface IBulletAIContext
  13. {
  14. long UID { get; }
  15. long MasterId { get; }
  16. int TableId { get; }
  17. ITargetSelector Selector { get; }
  18. Vector3 Position { get; }
  19. VfxComponent Vfx { get; }
  20. MoveComponent Move { get; }
  21. }
  22. /// <summary>
  23. /// AI组件和具体的EntityView绑定,其他Component最好不要尝试获取AI组件
  24. /// </summary>
  25. public class BulletAI : Component<IBulletAIContext>
  26. {
  27. /// <summary>
  28. /// 已攻击过的目标id
  29. /// </summary>
  30. private HashSet<long> _lastTargetIds = new HashSet<long>();
  31. private SkillTable _skill;
  32. private SkillVfxsTable _skillVfxs;
  33. protected override void OnDispose()
  34. {
  35. _skill = null;
  36. _skillVfxs = null;
  37. _lastTargetIds.Clear();
  38. }
  39. public void Start(long targetId, Vector3 targetPosition, EEntityType targetType, int delay)
  40. {
  41. _skill = SkillTableRepo.Get(Context.TableId);
  42. _skillVfxs = SkillVfxsTableRepo.Get(Context.TableId);
  43. var bulletType = (ESkillVfxType)_skill.SkillVfxType;
  44. _lastTargetIds.Clear();
  45. // 子弹的移动方向
  46. var direction = Vector3.Normalize(targetPosition - Context.Position);
  47. var moveArgs = new MoveArgs()
  48. {
  49. target = targetPosition,
  50. speed = _skillVfxs.SkillVfxSpeed,
  51. timeScale = 1,
  52. delay = delay,
  53. moveType = (EMoveType)(_skillVfxs.SkillVfxMoveType - 1),
  54. onComplete = () =>
  55. {
  56. if (bulletType == ESkillVfxType.BulletMortar)
  57. {
  58. MortarExplode(targetPosition, targetType);
  59. End();
  60. return;
  61. }
  62. var target = Context.Selector.GetTarget(targetId);
  63. if (target == null || target.IsDead)
  64. {
  65. //Log.Debug($"子弹移动到目标位置, 目标已死亡. Target:{targetId} Bullet:{Context.TableId}");
  66. End();
  67. return;
  68. }
  69. PlayHitVfx(_skillVfxs.HitVfx, targetPosition);
  70. target.Calculation.Damage(-_skill.Damage, Context.MasterId, _skill);
  71. switch (bulletType)
  72. {
  73. case ESkillVfxType.BulletSplit:
  74. // 分裂
  75. _lastTargetIds.Add(targetId);
  76. Split(direction, target.Entity.EntityType);
  77. End();
  78. break;
  79. case ESkillVfxType.BulletJump:
  80. // 弹跳
  81. _lastTargetIds.Add(targetId);
  82. Jump(target.Entity.EntityType, 1);
  83. break;
  84. default:
  85. End();
  86. break;
  87. }
  88. }
  89. };
  90. if (Enum.TryParse<DG.Tweening.Ease>(_skillVfxs.SkillVfxMoveEase, true, out var ease))
  91. {
  92. moveArgs.ease = ease;
  93. }
  94. Context.Move.MoveTo(moveArgs);
  95. }
  96. /// <summary>
  97. /// 受击特效
  98. /// </summary>
  99. /// <param name="hitVfx"></param>
  100. /// <param name="targetPosition"></param>
  101. private void PlayHitVfx(string hitVfx, Vector3 targetPosition)
  102. {
  103. var actPosition = ObjectPool.Acquire<PositionAction>();
  104. actPosition.position = targetPosition;
  105. var hitArgs = ObjectPool.Acquire<VfxArgs>();
  106. hitArgs.vfxName = hitVfx;
  107. hitArgs.duration = _skillVfxs.HitVfxTime;
  108. hitArgs.followType = EVfxFollowType.World;
  109. hitArgs.action = actPosition;
  110. Context.Vfx.Play(hitArgs);
  111. }
  112. /// <summary>
  113. /// 子弹分裂
  114. /// DamageArgs[数量,夹角(为零则就近原则找目标),缩放万分比]
  115. /// DamageArgs[1]大于零时,以子弹的初始移动方向为中心,按顺序(先左后右)累加角度获取分裂后子弹的方向
  116. /// 怪物在该方向上则命中,否则子弹按直线飞出屏幕
  117. /// </summary>
  118. /// <param name="direction">子弹的移动方向</param>
  119. /// <param name="targetType">目标类型</param>
  120. private void Split(Vector3 direction, EEntityType targetType)
  121. {
  122. var childCount = _skill.DamageArgs[0];
  123. var childAngle = _skill.DamageArgs[1];
  124. var childScale = _skill.DamageArgs[2];
  125. var from = Context.Position;
  126. var finderInfo = new FinderInfo()
  127. {
  128. uid = Context.UID,
  129. position = from,
  130. radius = 0,
  131. };
  132. for (var i = 0; i < childCount; i++)
  133. {
  134. var target = Context.Selector.FindSync(finderInfo, targetType, ETargetFindType.Nearest, (a) =>
  135. {
  136. if (_lastTargetIds.Contains(a.Entity.EntityId))
  137. {
  138. return true;
  139. }
  140. if (childAngle == 0)
  141. return false;
  142. var tarAngle = ((i + 1) / 2) * childAngle;
  143. var tempDir = Vector3.Normalize(a.Position - from);
  144. // angle:[0, 180]
  145. var angle = Vector3.Angle(direction, tempDir);
  146. //Log.Debug($"Bullet Split index:{i} direction:{direction} temp:{tempDir} angle:{angle} tar:{tarAngle}");
  147. if (Mathf.Abs(angle - tarAngle) < 20)
  148. { //角度范围-5到5
  149. if (i == 0)
  150. return false;
  151. //cross.z > 0 目标向量在方向向量的左侧
  152. var cross = Vector3.Cross(direction, tempDir);
  153. if (i % 2 == 0)
  154. { // i为偶数在右边
  155. return cross.z > 0;
  156. }
  157. return cross.z < 0;
  158. }
  159. return true;
  160. });
  161. if (target == null && childAngle == 0)
  162. {
  163. continue;
  164. }
  165. Vector3 to;
  166. if (target != null)
  167. {
  168. _lastTargetIds.Add(target.Entity.EntityId);
  169. to = target.Position;
  170. }
  171. else
  172. {
  173. if (i == 0)
  174. {
  175. to = from + direction * 12;
  176. }
  177. else
  178. {
  179. // i为偶数在右边 负数
  180. var sign = i % 2 == 0 ? -1 : 1;
  181. var tarAngle = ((i + 1) / 2) * childAngle;
  182. //var tempDir = direction.RotateLeft(sign * tarAngle);
  183. //var angle = Vector3.Angle(direction, tempDir);
  184. //Log.Debug($"Bullet Split index:{i} direction:{direction} temp:{tempDir} angle:{angle} tar:{tarAngle} {Vector3.Angle(direction, Vector3.up)} {Vector3.Angle(tempDir, Vector3.up)}");
  185. to = from + direction.RotateLeft(sign * tarAngle) * 12;
  186. }
  187. }
  188. //子弹移动
  189. var duration = Mathf.CeilToInt(Vector3.Distance(from, to) * 1000 / _skillVfxs.SkillVfxSpeed);
  190. if (duration == 0)
  191. { // 位置重合,直接结算伤害
  192. Log.Debug($"子弹分裂目标重合: from:{from} to:{to} speed:{_skillVfxs.SkillVfxSpeed}");
  193. target?.Calculation.Damage(-_skill.Damage, Context.MasterId, _skill);
  194. continue;
  195. }
  196. var actMove = ObjectPool.Acquire<MoveLineAction>();
  197. actMove.args = new MoveActionArgs()
  198. {
  199. from = from,
  200. to = to,
  201. duration = duration
  202. };
  203. //子弹特效
  204. var bulletArgs = ObjectPool.Acquire<VfxArgs>();
  205. bulletArgs.vfxName = _skillVfxs.SkillVfx;
  206. bulletArgs.duration = duration;
  207. bulletArgs.followType = EVfxFollowType.World;
  208. bulletArgs.action = actMove;
  209. if (target != null)
  210. {
  211. var skill = _skill;
  212. var masterId = Context.MasterId;
  213. bulletArgs.onFinish = () =>
  214. {
  215. if (target == null || target.IsDead)
  216. {
  217. return;
  218. }
  219. target.Calculation.Damage(-skill.Damage, masterId, skill);
  220. };
  221. //受击特效位置
  222. var actPosition = ObjectPool.Acquire<PositionAction>();
  223. actPosition.position = to;
  224. //受击特效
  225. var hitArgs = ObjectPool.Acquire<VfxArgs>();
  226. hitArgs.vfxName = _skillVfxs.HitVfx;
  227. hitArgs.duration = _skillVfxs.HitVfxTime;
  228. hitArgs.followType = EVfxFollowType.World;
  229. hitArgs.action = actPosition;
  230. bulletArgs.next = hitArgs;
  231. }
  232. Context.Vfx.Play(bulletArgs);
  233. }
  234. }
  235. /// <summary>
  236. /// 弹跳
  237. /// DamageArgs[弹跳次数,伤害递减万分比]
  238. /// </summary>
  239. /// <param name="targetType">目标类型</param>
  240. /// <param name="jumpTimes">弹跳次数</param>
  241. private void Jump(EEntityType targetType, int jumpTimes)
  242. {
  243. var jumpLimit = _skill.DamageArgs[0];
  244. var hurtReduce = _skill.DamageArgs[1];
  245. var finderInfo = new FinderInfo()
  246. {
  247. uid = Context.UID,
  248. position = Context.Position,
  249. radius = 0,
  250. };
  251. var target = Context.Selector.FindSync(finderInfo, targetType, ETargetFindType.Nearest, (a) =>
  252. {
  253. if (_lastTargetIds.Contains(a.Entity.EntityId))
  254. {
  255. return true;
  256. }
  257. return false;
  258. });
  259. if (target == null)
  260. {
  261. End();
  262. return;
  263. }
  264. var targetId = target.Entity.EntityId;
  265. _lastTargetIds.Add(targetId);
  266. var tarPosition = target.Position;
  267. var moveArgs = new MoveArgs()
  268. {
  269. target = tarPosition,
  270. speed = _skillVfxs.SkillVfxSpeed * 0.8f,
  271. timeScale = 1,
  272. onComplete = () =>
  273. {
  274. target = Context.Selector.GetTarget(targetId);
  275. if (target == null || target.IsDead)
  276. {
  277. Log.Debug($"子弹移动到目标位置, 目标已死亡. Target:{targetId} Bullet:{Context.TableId}");
  278. End();
  279. return;
  280. }
  281. // 受击特效
  282. PlayHitVfx(_skillVfxs.HitVfx, tarPosition);
  283. // 计算受击伤害
  284. target.Calculation.Damage(-JumpHurt(_skill.Damage, jumpTimes, hurtReduce), Context.MasterId, _skill);
  285. jumpTimes++;
  286. if (jumpTimes > jumpLimit)
  287. {
  288. End();
  289. return;
  290. }
  291. Jump(targetType, jumpTimes);
  292. }
  293. };
  294. Context.Move.MoveTo(moveArgs);
  295. }
  296. private int JumpHurt(int baseVal, int jumpTimes, int hurtReduce)
  297. {
  298. float hurtVal = baseVal;
  299. for (var i = 0; i < jumpTimes; i++)
  300. {
  301. hurtVal *= (1 - hurtReduce * 0.0001f);
  302. }
  303. return Mathf.RoundToInt(hurtVal);
  304. }
  305. /// <summary>
  306. /// 迫击炮爆炸
  307. /// </summary>
  308. /// <param name="targetPosition"></param>
  309. /// <param name="targetType"></param>
  310. private void MortarExplode(Vector3 targetPosition, EEntityType targetType)
  311. {
  312. var hitRadius = _skill.Ranges[0];
  313. var explodeRadius = _skill.Ranges[1];
  314. var explodeHurt = _skill.DamageArgs[0];
  315. PlayHitVfx(_skillVfxs.HitVfx, targetPosition);
  316. // 查找受击目标
  317. var finderInfo = new FinderInfo()
  318. {
  319. uid = Context.UID,
  320. position = Context.Position,
  321. radius = hitRadius,
  322. };
  323. var hitTarget = Context.Selector.FindSync(finderInfo, targetType, ETargetFindType.NearestAlive);
  324. if (hitTarget != null)
  325. {
  326. hitTarget.Calculation.Damage(-_skill.Damage, Context.MasterId, _skill);
  327. }
  328. // 查找爆炸受击目标
  329. finderInfo.radius = explodeRadius;
  330. var targets = new List<ITarget>();
  331. if (Context.Selector.FindTargets(finderInfo, targetType, ETargetFindType.InRangeAlive, null, ref targets))
  332. {
  333. foreach (var explodeTar in targets)
  334. {
  335. explodeTar.Calculation.Damage(-explodeHurt, Context.MasterId, _skill);
  336. }
  337. }
  338. }
  339. /// <summary>
  340. /// 结束
  341. /// </summary>
  342. private void End()
  343. {
  344. _skill = null;
  345. _skillVfxs = null;
  346. _lastTargetIds.Clear();
  347. EventSingle.Instance.Notify(EventDefine.GameMainMapRemoveSimpleEntity, Context.UID);
  348. }
  349. }
  350. }