FxParabolaBulletLogic.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  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.PlayHit();
  178. fxAILogicBasic.Dispose();
  179. }
  180. else if (skillFeaturesData.hp > 0)
  181. {
  182. Dispose();
  183. PlayHit();
  184. }
  185. else
  186. {
  187. fxAILogicBasic.PlayHit();
  188. fxAILogicBasic.Dispose();
  189. Dispose();
  190. PlayHit();
  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. PlayHit();
  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. isUpdateDir = false;
  284. timeLineTriggerEvent.TimeLineTrigger(TimeLineEventParticleLogicBasic.TimeLineEventLogicGroup.groupName,
  285. target.GetMainHotPoin<ILifetCycleHitPoint>(), this, triggerData, SkillFeaturesData);
  286. if (!string.IsNullOrEmpty(hitFxName))
  287. {
  288. FinishHit(collision.ClosestPoint(moveTarget.transform.position), hitFxName);
  289. }
  290. AudioManager.Instance.PlayAudio(hitAudioName, false);
  291. if (!isPenetrate)
  292. {
  293. Dispose();
  294. }
  295. }
  296. }
  297. private void FinishHit(Vector3 pos, string hitFxName)
  298. {
  299. if (!string.IsNullOrEmpty(hitFxName))
  300. {
  301. CombatController.currActiveCombat.GameTimeLineParticleFactory.CreateParticle(hitFxName,
  302. pos, null, false, null, CallBack);
  303. }
  304. }
  305. private void CallBack(ParticleSystemPool obj)
  306. {
  307. if (roatRoot != null)
  308. {
  309. obj.transform.rotation = roatRoot.rotation;
  310. }
  311. }
  312. protected override void ProCombatUpdate(float time)
  313. {
  314. _currRunTime += time;
  315. if (_currRunTime > maxTime)
  316. {
  317. Dispose();
  318. return;
  319. }
  320. if (!isNotMove)
  321. {
  322. switch (parabolaCurveType)
  323. {
  324. case CurveType.Beeline:
  325. Beeline(time);
  326. break;
  327. case CurveType.DynamicCurve:
  328. DynamicCurve(time);
  329. break;
  330. case CurveType.CustomizeCurve:
  331. CustomizeCurve(time);
  332. break;
  333. }
  334. }
  335. // if (Vector3.Distance(endPos, _currPos) < 0.5f)
  336. // {
  337. // Dispose();
  338. // }
  339. }
  340. private void Beeline(float time)
  341. {
  342. GetTargetPos();
  343. if (isUpdateDir)
  344. {
  345. Vector3 dir = (endPos - _currPos).normalized;
  346. // dir = Vector3.Lerp(startDir, dir, dirLerpTime).normalized;
  347. startDir = dir;
  348. }
  349. Vector3 lasetPos = _currPos;
  350. _currPos += startDir * GetMoveSpeed() * time;
  351. moveTarget.transform.position = _currPos;
  352. moveTarget.transform.rotation = Quaternion.LookRotation(dir);
  353. }
  354. private void CustomizeCurve(float time)
  355. {
  356. _addTime = 1.0f / (moveBezierPath.allDis / GetMoveSpeed());
  357. currTime += _addTime * time;
  358. GetTargetPos();
  359. moveBezierPath.controlPoints[3] = endPos;
  360. // moveBezierPath.controlPoints[0] = (gameObject.transform.TransformPoint(BesselPath.controlPoints[0]));
  361. Vector3 p = moveBezierPath.CalculatePoint(currTime);
  362. Vector3 p2 = moveBezierPath.CalculatePoint(currTime - 0.01f);
  363. moveTarget.transform.position = p;
  364. moveTarget.transform.rotation = Quaternion.LookRotation((p - p2).normalized);
  365. }
  366. private void DynamicCurve(float time)
  367. {
  368. dirLerpTime += time * 4F;
  369. if (dirLerpTime > 1)
  370. {
  371. dirLerpTime = 1;
  372. }
  373. GetTargetPos();
  374. Vector3 dir = (endPos - _currPos).normalized;
  375. dir = Vector3.Lerp(startDir, dir, dirLerpTime).normalized;
  376. // startDir= dir;
  377. Vector3 lasetPos = _currPos;
  378. _currPos += dir * GetMoveSpeed() * time;
  379. moveTarget.transform.position = _currPos;
  380. moveTarget.transform.rotation = Quaternion.LookRotation(dir);
  381. }
  382. protected override void ProDispose()
  383. {
  384. if (UnRegister != null)
  385. {
  386. UnRegister.UnRegister();
  387. }
  388. UnRegister = null;
  389. }
  390. private void OnDrawGizmos()
  391. {
  392. if (parabolaCurveType != CurveType.CustomizeCurve)
  393. {
  394. return;
  395. }
  396. if (BesselPath.controlPoints.Count < 4)
  397. {
  398. BesselPath.controlPoints.Add(Vector3.zero);
  399. BesselPath.controlPoints.Add(new Vector3(-1, 1, -1));
  400. BesselPath.controlPoints.Add(new Vector3(0, 0, 2));
  401. BesselPath.controlPoints.Add(new Vector3(0, 0, 5));
  402. return;
  403. }
  404. Gizmos.color = Color.blue;
  405. // 绘制控制点
  406. // 绘制曲线
  407. Vector3 previousPoint = BesselPath.CalculatePoint(0) + transform.position;
  408. int segments = 50;
  409. for (int i = 1; i <= segments; i++)
  410. {
  411. float t = i / (float)segments;
  412. Vector3 currentPoint = BesselPath.CalculatePoint(t) + transform.position;
  413. // Gizmos.DrawSphere(currentPoint, 0.03f);
  414. Gizmos.DrawLine(previousPoint, currentPoint);
  415. previousPoint = currentPoint;
  416. }
  417. }
  418. #if UNITY_EDITOR
  419. [CustomEditor(typeof(FxParabolaBulletLogic))]
  420. public class MovableCoordinateEditor : Editor
  421. {
  422. void OnSceneGUI()
  423. {
  424. FxParabolaBulletLogic fxParabolaBulletLogic = target as FxParabolaBulletLogic;
  425. if (fxParabolaBulletLogic.parabolaCurveType != CurveType.CustomizeCurve)
  426. {
  427. return;
  428. }
  429. if (fxParabolaBulletLogic.BesselPath.controlPoints.Count < 4)
  430. {
  431. return;
  432. }
  433. // 使用 Handles.PositionHandle 提供一个可拖动的控制柄
  434. for (int i = 0; i < fxParabolaBulletLogic.BesselPath.controlPoints.Count; i++)
  435. {
  436. EditorGUI.BeginChangeCheck();
  437. Vector3 newPosition = Handles.PositionHandle(
  438. fxParabolaBulletLogic.BesselPath.controlPoints[i] +
  439. fxParabolaBulletLogic.gameObject.transform.position,
  440. Quaternion.identity);
  441. if (EditorGUI.EndChangeCheck())
  442. {
  443. Undo.RecordObject(fxParabolaBulletLogic, "Move Coordinate");
  444. fxParabolaBulletLogic.BesselPath.controlPoints[i] =
  445. newPosition - fxParabolaBulletLogic.gameObject.transform.position;
  446. }
  447. }
  448. }
  449. }
  450. #endif
  451. }
  452. }