FxParabolaBulletLogic.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. using System.Collections.Generic;
  2. using Core.Audio;
  3. using Core.Triiger;
  4. using Core.Utility;
  5. using Fort23.UTool;
  6. using GameLogic.Combat.CombatTool;
  7. using GameLogic.Combat.Hero;
  8. using GameLogic.Combat.Skill;
  9. using UnityEditor;
  10. using UnityEngine;
  11. using UTool.CustomizeTimeLogic.FxLogic.TimeLineEventinterface;
  12. namespace Common.Combat.FxAILogic
  13. {
  14. [AddComponentMenu("特效脚本/弹道/功法通用弹道")]
  15. public class FxParabolaBulletLogic : FxAILogicBasic
  16. {
  17. public float speed;
  18. public Transform roatRoot;
  19. public int raotIndex;
  20. private IUnRegister UnRegister = null;
  21. public enum CurveType
  22. {
  23. DynamicCurve,
  24. Beeline,
  25. CustomizeCurve,
  26. }
  27. [Header("曲线类型")] public CurveType parabolaCurveType;
  28. [Header("释放碰撞到地面时结束")] public bool isTriggerGroundEnd;
  29. [Header("碰撞到地面时的特效")] public string GroundHitFxName;
  30. [Header("是否使用X曲线跟随角色")] public bool isUseX;
  31. [Header("自定义曲线")] public BesselPath BesselPath;
  32. private Vector3 endPos;
  33. private Vector3 dir;
  34. [Header("存在最长时间")] public float maxTime = 10;
  35. // [Header("是否可以穿透")] public bool isPenetrate;
  36. //
  37. // protected float maxDisSpr;
  38. // private BesselPath _besselPath;
  39. protected BesselPath moveBezierPath;
  40. private float _addTime;
  41. protected Vector3 startDir;
  42. private float dirLerpTime;
  43. private float _currRunTime;
  44. private bool isUpdateDir;
  45. protected override void ProInit()
  46. {
  47. // maxDisSpr = maxDis * maxDis;
  48. _currRunTime = 0;
  49. isUpdateDir = true;
  50. if (roatRoot != null && TimeLineEventParticleLogicBasic.timeLinePlayFxSerializtion.isRoat)
  51. {
  52. roatRoot.transform.eulerAngles = TimeLineEventParticleLogicBasic.customizePos[raotIndex];
  53. }
  54. if (isUseCustomTargetEndPos)
  55. {
  56. endPos = TimeLineEventParticleLogicBasic.customizePos[
  57. customTargetEndPosIndex];
  58. }
  59. else
  60. {
  61. endPos = AttTarget.GetSpecialDotInfo("hitpos").GetWorlPos();
  62. // endPos = new Vector3(endPos.x, CurrPos.y, endPos.z);
  63. }
  64. if (parabolaCurveType == CurveType.DynamicCurve)
  65. {
  66. Vector3 off = _currPos - CombatHeroEntity.GameObject.transform.position;
  67. float x = Mathf.Sign(Random.Range(-1, 1));
  68. // if (isUseX)
  69. {
  70. Vector3 pos =
  71. CombatHeroEntity.GameObject.transform.InverseTransformPoint(gameObject.transform.position);
  72. x = Mathf.Sign(pos.x);
  73. }
  74. startDir = CombatHeroEntity.GameObject.transform.TransformPoint(off +
  75. new Vector3(
  76. x,
  77. Random.Range(0.1f, 3f),
  78. Random.Range(0.1f, 4f)));
  79. startDir = (startDir - _currPos).normalized;
  80. dirLerpTime = 0;
  81. dir = (endPos - CurrPos).normalized;
  82. moveTarget.transform.rotation = Quaternion.LookRotation(dir);
  83. }
  84. else if (parabolaCurveType == CurveType.Beeline)
  85. {
  86. dir = (endPos - _currPos).normalized;
  87. gameObject.transform.rotation = Quaternion.LookRotation(dir);
  88. }
  89. else if (parabolaCurveType == CurveType.CustomizeCurve)
  90. {
  91. gameObject.transform.rotation = CombatHeroEntity.GameObject.transform.rotation;
  92. Vector3 pos =
  93. CombatHeroEntity.GameObject.transform.InverseTransformPoint(gameObject.transform.position);
  94. if (moveBezierPath == null)
  95. {
  96. moveBezierPath = new BesselPath();
  97. }
  98. moveBezierPath.controlPoints.Clear();
  99. for (int i = 0; i < BesselPath.controlPoints.Count; i++)
  100. {
  101. Vector3 p = BesselPath.controlPoints[i];
  102. if (isUseX)
  103. {
  104. if (i == 1 || i == 2)
  105. {
  106. p.x = pos.x > 0 ? p.x * -1 : p.x;
  107. }
  108. }
  109. moveBezierPath.controlPoints.Add(gameObject.transform.TransformPoint(p / (size * size)));
  110. }
  111. moveBezierPath.SetLengthAtT();
  112. currTime = 0;
  113. }
  114. CombatUpdate(0.016f);
  115. UnRegister = moveTarget.OnTriggerEnterEvent(this, OnTriggerEnterEvent);
  116. }
  117. private float GetMoveSpeed()
  118. {
  119. float v1 = CombatCalculateTool.Instance.GetVlaueRatioForFloat(speed, extraMoveSpeed);
  120. float v = speed + v1;
  121. if (v < 0)
  122. {
  123. v = 1;
  124. }
  125. return v;
  126. }
  127. void TriggerGround()
  128. {
  129. ITimeLineTriggerEvent timeLineTriggerEvent =
  130. TimeLineEventParticleLogicBasic.ITimeLineTriggerEntity as ITimeLineTriggerEvent;
  131. FinishHit(new Vector3(_currPos.x, 0.5f, _currPos.z), GroundHitFxName);
  132. AudioManager.Instance.PlayAudio(hitAudioName, false);
  133. if (timeLineTriggerEvent != null)
  134. {
  135. timeLineTriggerEvent.TimeLineTriggerGround(
  136. TimeLineEventParticleLogicBasic.TimeLineEventLogicGroup.groupName,
  137. this, triggerData);
  138. }
  139. AudioManager.Instance.PlayAudio(hitAudioName, false);
  140. Dispose();
  141. }
  142. protected void GetTargetPos()
  143. {
  144. if (isUseCustomTargetEndPos)
  145. {
  146. endPos = TimeLineEventParticleLogicBasic.customizePos[
  147. customTargetEndPosIndex];
  148. }
  149. else
  150. {
  151. endPos = AttTarget.GetSpecialDotInfo("hitpos").GetWorlPos();
  152. // endPos = new Vector3(endPos.x, CurrPos.y, endPos.z);
  153. }
  154. }
  155. protected void OnTriggerEnterEvent(Collider collision, ITriggerEntity triggerEntity)
  156. {
  157. if (isTriggerGroundEnd)
  158. {
  159. if (collision.gameObject.CompareTag("dimian"))
  160. {
  161. TriggerGround();
  162. return;
  163. }
  164. }
  165. FxAILogicBasic fxAILogicBasic = triggerEntity as FxAILogicBasic;
  166. if (fxAILogicBasic != null) //击中其他的功法
  167. {
  168. SkillFeaturesData skillFeaturesData = fxAILogicBasic.SkillFeaturesData;
  169. if (skillFeaturesData.isEnemy == SkillFeaturesData.isEnemy)
  170. {
  171. return;
  172. }
  173. CombatCalculateTool.Instance.GongFaPengZhuang(SkillFeaturesData, skillFeaturesData, CombatHeroEntity,
  174. fxAILogicBasic.CombatHeroEntity);
  175. if (SkillFeaturesData.hp > 0)
  176. {
  177. fxAILogicBasic.PlayPengZhuangHit();
  178. fxAILogicBasic.Dispose();
  179. }
  180. else if (skillFeaturesData.hp > 0)
  181. {
  182. Dispose();
  183. PlayPengZhuangHit();
  184. }
  185. else
  186. {
  187. fxAILogicBasic.PlayPengZhuangHit();
  188. fxAILogicBasic.Dispose();
  189. Dispose();
  190. PlayPengZhuangHit();
  191. }
  192. return;
  193. }
  194. else
  195. {
  196. HeroEntityMono heroEntityMono = collision.gameObject.GetComponent<HeroEntityMono>();
  197. if (heroEntityMono != null)
  198. {
  199. TriggerHero(collision, heroEntityMono);
  200. return;
  201. }
  202. BarrierEntityMono barrierEntityMono = collision.gameObject.GetComponent<BarrierEntityMono>();
  203. if (barrierEntityMono != null)
  204. {
  205. TriggerBarrier(collision, barrierEntityMono);
  206. }
  207. }
  208. }
  209. public void PengZhuang(SkillFeaturesData targetSkillFeaturesData, CombatHeroEntity targetCombatHeroEntity)
  210. {
  211. CombatCalculateTool.Instance.GongFaPengZhuang(SkillFeaturesData, targetSkillFeaturesData, CombatHeroEntity,
  212. targetCombatHeroEntity);
  213. if (SkillFeaturesData.hp <= 0)
  214. {
  215. PlayPengZhuangHit();
  216. Dispose();
  217. }
  218. }
  219. protected void TriggerBarrier(Collider collision, BarrierEntityMono barrierEntityMono)
  220. {
  221. if (barrierEntityMono == null)
  222. {
  223. return;
  224. }
  225. CombatHeroEntity target = barrierEntityMono.Barrier.Own as CombatHeroEntity;
  226. if (target.IsEnemy == CombatHeroEntity.IsEnemy || target is CombatMagicWeaponEntity)
  227. {
  228. return;
  229. }
  230. if (TimeLineEventParticleLogicBasic.TimeLineEventLogicGroup.groupName == null)
  231. {
  232. Dispose();
  233. return;
  234. }
  235. bool isOk = barrierEntityMono.Barrier.CollideTriiger(this);
  236. if (!isOk) //被主档
  237. {
  238. BarrierTriggerData.isPenetrate = false;
  239. barrierEntityMono.Barrier.BarrierTriggerData = BarrierTriggerData;
  240. triggerData.IBarrier = barrierEntityMono.Barrier;
  241. ITimeLineTriggerEvent timeLineTriggerEvent =
  242. TimeLineEventParticleLogicBasic.ITimeLineTriggerEntity as ITimeLineTriggerEvent;
  243. if (timeLineTriggerEvent != null)
  244. {
  245. timeLineTriggerEvent.TimeLineTrigger(
  246. TimeLineEventParticleLogicBasic.TimeLineEventLogicGroup.groupName,
  247. target.GetMainHotPoin<ILifetCycleHitPoint>(), this, triggerData, SkillFeaturesData);
  248. if (!string.IsNullOrEmpty(hitFxName))
  249. {
  250. FinishHit(collision.ClosestPoint(moveTarget.transform.position), hitFxName);
  251. }
  252. AudioManager.Instance.PlayAudio(hitAudioName, false);
  253. if (!BarrierTriggerData.isPenetrate)
  254. {
  255. if (!isPenetrate)
  256. {
  257. Dispose();
  258. }
  259. }
  260. }
  261. }
  262. }
  263. protected void TriggerHero(Collider collision, HeroEntityMono heroEntityMono)
  264. {
  265. if (heroEntityMono == null)
  266. {
  267. return;
  268. }
  269. CombatHeroEntity target = heroEntityMono.combatHeroEntity;
  270. if (target.IsEnemy == CombatHeroEntity.IsEnemy || target is CombatMagicWeaponEntity)
  271. {
  272. return;
  273. }
  274. if (TimeLineEventParticleLogicBasic.TimeLineEventLogicGroup.groupName == null)
  275. {
  276. Dispose();
  277. return;
  278. }
  279. ITimeLineTriggerEvent timeLineTriggerEvent =
  280. TimeLineEventParticleLogicBasic.ITimeLineTriggerEntity as ITimeLineTriggerEvent;
  281. if (timeLineTriggerEvent != null)
  282. {
  283. triggerData.IBarrier = null;
  284. isUpdateDir = false;
  285. timeLineTriggerEvent.TimeLineTrigger(TimeLineEventParticleLogicBasic.TimeLineEventLogicGroup.groupName,
  286. target.GetMainHotPoin<ILifetCycleHitPoint>(), this, triggerData, SkillFeaturesData);
  287. if (!string.IsNullOrEmpty(hitFxName))
  288. {
  289. FinishHit(collision.ClosestPoint(moveTarget.transform.position), hitFxName);
  290. }
  291. AudioManager.Instance.PlayAudio(hitAudioName, false);
  292. if (!isPenetrate)
  293. {
  294. Dispose();
  295. }
  296. }
  297. }
  298. private void FinishHit(Vector3 pos, string hitFxName)
  299. {
  300. if (!string.IsNullOrEmpty(hitFxName))
  301. {
  302. CombatController.currActiveCombat.GameTimeLineParticleFactory.CreateParticle(hitFxName,
  303. pos, null, false, null, CallBack);
  304. }
  305. }
  306. private void CallBack(ParticleSystemPool obj)
  307. {
  308. if (roatRoot != null)
  309. {
  310. obj.transform.rotation = roatRoot.rotation;
  311. }
  312. }
  313. protected override void ProCombatUpdate(float time)
  314. {
  315. _currRunTime += time;
  316. if (_currRunTime > maxTime)
  317. {
  318. Dispose();
  319. return;
  320. }
  321. if (!isNotMove)
  322. {
  323. switch (parabolaCurveType)
  324. {
  325. case CurveType.Beeline:
  326. Beeline(time);
  327. break;
  328. case CurveType.DynamicCurve:
  329. DynamicCurve(time);
  330. break;
  331. case CurveType.CustomizeCurve:
  332. CustomizeCurve(time);
  333. break;
  334. }
  335. }
  336. // if (Vector3.Distance(endPos, _currPos) < 0.5f)
  337. // {
  338. // Dispose();
  339. // }
  340. }
  341. private void Beeline(float time)
  342. {
  343. GetTargetPos();
  344. if (isUpdateDir)
  345. {
  346. Vector3 dir = (endPos - _currPos).normalized;
  347. // dir = Vector3.Lerp(startDir, dir, dirLerpTime).normalized;
  348. startDir = dir;
  349. }
  350. Vector3 lasetPos = _currPos;
  351. _currPos += startDir * GetMoveSpeed() * time;
  352. moveTarget.transform.position = _currPos;
  353. moveTarget.transform.rotation = Quaternion.LookRotation(dir);
  354. }
  355. private void CustomizeCurve(float time)
  356. {
  357. _addTime = 1.0f / (moveBezierPath.allDis / GetMoveSpeed());
  358. currTime += _addTime * time;
  359. GetTargetPos();
  360. moveBezierPath.controlPoints[3] = endPos;
  361. // moveBezierPath.controlPoints[0] = (gameObject.transform.TransformPoint(BesselPath.controlPoints[0]));
  362. Vector3 p = moveBezierPath.CalculatePoint(currTime);
  363. Vector3 p2 = moveBezierPath.CalculatePoint(currTime - 0.01f);
  364. moveTarget.transform.position = p;
  365. moveTarget.transform.rotation = Quaternion.LookRotation((p - p2).normalized);
  366. }
  367. private void DynamicCurve(float time)
  368. {
  369. dirLerpTime += time * 4F;
  370. if (dirLerpTime > 1)
  371. {
  372. dirLerpTime = 1;
  373. }
  374. GetTargetPos();
  375. Vector3 dir = (endPos - _currPos).normalized;
  376. dir = Vector3.Lerp(startDir, dir, dirLerpTime).normalized;
  377. // startDir= dir;
  378. Vector3 lasetPos = _currPos;
  379. _currPos += dir * GetMoveSpeed() * time;
  380. moveTarget.transform.position = _currPos;
  381. moveTarget.transform.rotation = Quaternion.LookRotation(dir);
  382. }
  383. protected override void ProDispose()
  384. {
  385. if (UnRegister != null)
  386. {
  387. UnRegister.UnRegister();
  388. }
  389. UnRegister = null;
  390. }
  391. private void OnDrawGizmos()
  392. {
  393. if (parabolaCurveType != CurveType.CustomizeCurve)
  394. {
  395. return;
  396. }
  397. if (BesselPath.controlPoints.Count < 4)
  398. {
  399. BesselPath.controlPoints.Add(Vector3.zero);
  400. BesselPath.controlPoints.Add(new Vector3(-1, 1, -1));
  401. BesselPath.controlPoints.Add(new Vector3(0, 0, 2));
  402. BesselPath.controlPoints.Add(new Vector3(0, 0, 5));
  403. return;
  404. }
  405. Gizmos.color = Color.blue;
  406. // 绘制控制点
  407. // 绘制曲线
  408. Vector3 previousPoint = BesselPath.CalculatePoint(0) + transform.position;
  409. int segments = 50;
  410. for (int i = 1; i <= segments; i++)
  411. {
  412. float t = i / (float)segments;
  413. Vector3 currentPoint = BesselPath.CalculatePoint(t) + transform.position;
  414. // Gizmos.DrawSphere(currentPoint, 0.03f);
  415. Gizmos.DrawLine(previousPoint, currentPoint);
  416. previousPoint = currentPoint;
  417. }
  418. }
  419. #if UNITY_EDITOR
  420. [CustomEditor(typeof(FxParabolaBulletLogic))]
  421. public class MovableCoordinateEditor : Editor
  422. {
  423. void OnSceneGUI()
  424. {
  425. FxParabolaBulletLogic fxParabolaBulletLogic = target as FxParabolaBulletLogic;
  426. if (fxParabolaBulletLogic.parabolaCurveType != CurveType.CustomizeCurve)
  427. {
  428. return;
  429. }
  430. if (fxParabolaBulletLogic.BesselPath.controlPoints.Count < 4)
  431. {
  432. return;
  433. }
  434. // 使用 Handles.PositionHandle 提供一个可拖动的控制柄
  435. for (int i = 0; i < fxParabolaBulletLogic.BesselPath.controlPoints.Count; i++)
  436. {
  437. EditorGUI.BeginChangeCheck();
  438. Vector3 newPosition = Handles.PositionHandle(
  439. fxParabolaBulletLogic.BesselPath.controlPoints[i] +
  440. fxParabolaBulletLogic.gameObject.transform.position,
  441. Quaternion.identity);
  442. if (EditorGUI.EndChangeCheck())
  443. {
  444. Undo.RecordObject(fxParabolaBulletLogic, "Move Coordinate");
  445. fxParabolaBulletLogic.BesselPath.controlPoints[i] =
  446. newPosition - fxParabolaBulletLogic.gameObject.transform.position;
  447. }
  448. }
  449. }
  450. }
  451. #endif
  452. }
  453. }