FxParabolaBulletLogic.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  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&&barrierEntityMono.Barrier!=null)
  204. {
  205. TriggerBarrier(collision, barrierEntityMono);
  206. }
  207. }
  208. }
  209. public void PengZhuang(SkillFeaturesData targetSkillFeaturesData, CombatHeroEntity targetCombatHeroEntity)
  210. {
  211. if (targetSkillFeaturesData == null)
  212. {
  213. PlayPengZhuangHit();
  214. Dispose();
  215. return;
  216. }
  217. CombatCalculateTool.Instance.GongFaPengZhuang(SkillFeaturesData, targetSkillFeaturesData, CombatHeroEntity,
  218. targetCombatHeroEntity);
  219. if (SkillFeaturesData.hp <= 0)
  220. {
  221. PlayPengZhuangHit();
  222. Dispose();
  223. }
  224. }
  225. protected void TriggerBarrier(Collider collision, BarrierEntityMono barrierEntityMono)
  226. {
  227. if (barrierEntityMono == null)
  228. {
  229. return;
  230. }
  231. CombatHeroEntity target = barrierEntityMono.Barrier.Own as CombatHeroEntity;
  232. if (target.IsEnemy == CombatHeroEntity.IsEnemy || target is CombatMagicWeaponEntity)
  233. {
  234. return;
  235. }
  236. if (TimeLineEventParticleLogicBasic.TimeLineEventLogicGroup.groupName == null)
  237. {
  238. Dispose();
  239. return;
  240. }
  241. bool isOk = barrierEntityMono.Barrier.CollideTriiger(this);
  242. if (!isOk) //被主档
  243. {
  244. BarrierTriggerData.isPenetrate = false;
  245. barrierEntityMono.Barrier.BarrierTriggerData = BarrierTriggerData;
  246. triggerData.IBarrier = barrierEntityMono.Barrier;
  247. ITimeLineTriggerEvent timeLineTriggerEvent =
  248. TimeLineEventParticleLogicBasic.ITimeLineTriggerEntity as ITimeLineTriggerEvent;
  249. if (timeLineTriggerEvent != null)
  250. {
  251. timeLineTriggerEvent.TimeLineTrigger(
  252. TimeLineEventParticleLogicBasic.TimeLineEventLogicGroup.groupName,
  253. target.GetMainHotPoin<ILifetCycleHitPoint>(), this, triggerData, SkillFeaturesData);
  254. if (!string.IsNullOrEmpty(hitFxName))
  255. {
  256. FinishHit(collision.ClosestPoint(moveTarget.transform.position), hitFxName);
  257. }
  258. AudioManager.Instance.PlayAudio(hitAudioName, false);
  259. if (!BarrierTriggerData.isPenetrate)
  260. {
  261. if (!isPenetrate)
  262. {
  263. Dispose();
  264. }
  265. }
  266. }
  267. }
  268. }
  269. protected void TriggerHero(Collider collision, HeroEntityMono heroEntityMono)
  270. {
  271. if (heroEntityMono == null)
  272. {
  273. return;
  274. }
  275. CombatHeroEntity target = heroEntityMono.combatHeroEntity;
  276. if (target.IsEnemy == CombatHeroEntity.IsEnemy || target is CombatMagicWeaponEntity)
  277. {
  278. return;
  279. }
  280. if (TimeLineEventParticleLogicBasic.TimeLineEventLogicGroup.groupName == null)
  281. {
  282. Dispose();
  283. return;
  284. }
  285. ITimeLineTriggerEvent timeLineTriggerEvent =
  286. TimeLineEventParticleLogicBasic.ITimeLineTriggerEntity as ITimeLineTriggerEvent;
  287. if (timeLineTriggerEvent != null)
  288. {
  289. triggerData.IBarrier = null;
  290. isUpdateDir = false;
  291. timeLineTriggerEvent.TimeLineTrigger(TimeLineEventParticleLogicBasic.TimeLineEventLogicGroup.groupName,
  292. target.GetMainHotPoin<ILifetCycleHitPoint>(), this, triggerData, SkillFeaturesData);
  293. if (!string.IsNullOrEmpty(hitFxName))
  294. {
  295. FinishHit(collision.ClosestPoint(moveTarget.transform.position), hitFxName);
  296. }
  297. AudioManager.Instance.PlayAudio(hitAudioName, false);
  298. if (!isPenetrate)
  299. {
  300. Dispose();
  301. }
  302. }
  303. }
  304. private void FinishHit(Vector3 pos, string hitFxName)
  305. {
  306. if (!string.IsNullOrEmpty(hitFxName))
  307. {
  308. CombatController.currActiveCombat.GameTimeLineParticleFactory.CreateParticle(hitFxName,
  309. pos, null, false, null, CallBack);
  310. }
  311. }
  312. private void CallBack(ParticleSystemPool obj)
  313. {
  314. if (roatRoot != null)
  315. {
  316. obj.transform.rotation = roatRoot.rotation;
  317. }
  318. }
  319. protected override void ProCombatUpdate(float time)
  320. {
  321. _currRunTime += time;
  322. if (_currRunTime > maxTime)
  323. {
  324. Dispose();
  325. return;
  326. }
  327. if (!isNotMove)
  328. {
  329. switch (parabolaCurveType)
  330. {
  331. case CurveType.Beeline:
  332. Beeline(time);
  333. break;
  334. case CurveType.DynamicCurve:
  335. DynamicCurve(time);
  336. break;
  337. case CurveType.CustomizeCurve:
  338. CustomizeCurve(time);
  339. break;
  340. }
  341. }
  342. // if (Vector3.Distance(endPos, _currPos) < 0.5f)
  343. // {
  344. // Dispose();
  345. // }
  346. }
  347. private void Beeline(float time)
  348. {
  349. GetTargetPos();
  350. if (isUpdateDir)
  351. {
  352. Vector3 dir = (endPos - _currPos).normalized;
  353. // dir = Vector3.Lerp(startDir, dir, dirLerpTime).normalized;
  354. startDir = dir;
  355. }
  356. Vector3 lasetPos = _currPos;
  357. _currPos += startDir * GetMoveSpeed() * time;
  358. moveTarget.transform.position = _currPos;
  359. moveTarget.transform.rotation = Quaternion.LookRotation(dir);
  360. }
  361. private void CustomizeCurve(float time)
  362. {
  363. _addTime = 1.0f / (moveBezierPath.allDis / GetMoveSpeed());
  364. currTime += _addTime * time;
  365. GetTargetPos();
  366. moveBezierPath.controlPoints[3] = endPos;
  367. // moveBezierPath.controlPoints[0] = (gameObject.transform.TransformPoint(BesselPath.controlPoints[0]));
  368. Vector3 p = moveBezierPath.CalculatePoint(currTime);
  369. Vector3 p2 = moveBezierPath.CalculatePoint(currTime - 0.01f);
  370. moveTarget.transform.position = p;
  371. moveTarget.transform.rotation = Quaternion.LookRotation((p - p2).normalized);
  372. }
  373. private void DynamicCurve(float time)
  374. {
  375. dirLerpTime += time * 4F;
  376. if (dirLerpTime > 1)
  377. {
  378. dirLerpTime = 1;
  379. }
  380. GetTargetPos();
  381. Vector3 dir = (endPos - _currPos).normalized;
  382. dir = Vector3.Lerp(startDir, dir, dirLerpTime).normalized;
  383. // startDir= dir;
  384. Vector3 lasetPos = _currPos;
  385. _currPos += dir * GetMoveSpeed() * time;
  386. moveTarget.transform.position = _currPos;
  387. moveTarget.transform.rotation = Quaternion.LookRotation(dir);
  388. }
  389. protected override void ProDispose()
  390. {
  391. if (UnRegister != null)
  392. {
  393. UnRegister.UnRegister();
  394. }
  395. UnRegister = null;
  396. }
  397. private void OnDrawGizmos()
  398. {
  399. if (parabolaCurveType != CurveType.CustomizeCurve)
  400. {
  401. return;
  402. }
  403. if (BesselPath.controlPoints.Count < 4)
  404. {
  405. BesselPath.controlPoints.Add(Vector3.zero);
  406. BesselPath.controlPoints.Add(new Vector3(-1, 1, -1));
  407. BesselPath.controlPoints.Add(new Vector3(0, 0, 2));
  408. BesselPath.controlPoints.Add(new Vector3(0, 0, 5));
  409. return;
  410. }
  411. Gizmos.color = Color.blue;
  412. // 绘制控制点
  413. // 绘制曲线
  414. Vector3 previousPoint = BesselPath.CalculatePoint(0) + transform.position;
  415. int segments = 50;
  416. for (int i = 1; i <= segments; i++)
  417. {
  418. float t = i / (float)segments;
  419. Vector3 currentPoint = BesselPath.CalculatePoint(t) + transform.position;
  420. // Gizmos.DrawSphere(currentPoint, 0.03f);
  421. Gizmos.DrawLine(previousPoint, currentPoint);
  422. previousPoint = currentPoint;
  423. }
  424. }
  425. #if UNITY_EDITOR
  426. [CustomEditor(typeof(FxParabolaBulletLogic))]
  427. public class MovableCoordinateEditor : Editor
  428. {
  429. void OnSceneGUI()
  430. {
  431. FxParabolaBulletLogic fxParabolaBulletLogic = target as FxParabolaBulletLogic;
  432. if (fxParabolaBulletLogic.parabolaCurveType != CurveType.CustomizeCurve)
  433. {
  434. return;
  435. }
  436. if (fxParabolaBulletLogic.BesselPath.controlPoints.Count < 4)
  437. {
  438. return;
  439. }
  440. // 使用 Handles.PositionHandle 提供一个可拖动的控制柄
  441. for (int i = 0; i < fxParabolaBulletLogic.BesselPath.controlPoints.Count; i++)
  442. {
  443. EditorGUI.BeginChangeCheck();
  444. Vector3 newPosition = Handles.PositionHandle(
  445. fxParabolaBulletLogic.BesselPath.controlPoints[i] +
  446. fxParabolaBulletLogic.gameObject.transform.position,
  447. Quaternion.identity);
  448. if (EditorGUI.EndChangeCheck())
  449. {
  450. Undo.RecordObject(fxParabolaBulletLogic, "Move Coordinate");
  451. fxParabolaBulletLogic.BesselPath.controlPoints[i] =
  452. newPosition - fxParabolaBulletLogic.gameObject.transform.position;
  453. }
  454. }
  455. }
  456. }
  457. #endif
  458. }
  459. }