FxParabolaBulletLogic.cs 16 KB

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