FxParabolaBulletLogic.cs 17 KB

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