FxParabolaBulletLogic.cs 16 KB

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