FxParabolaBulletLogic.cs 17 KB

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