FxParabolaBulletLogic.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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));
  98. }
  99. _addTime = 1.0f / (moveBezierPath.GetLengthAtT(0) / speed);
  100. currTime = 0;
  101. }
  102. }
  103. void TriggerGround()
  104. {
  105. ITimeLineTriggerEvent timeLineTriggerEvent =
  106. TimeLineEventParticleLogicBasic.ITimeLineTriggerEntity as ITimeLineTriggerEvent;
  107. FinishHit(new Vector3(_currPos.x, 0.5f, _currPos.z), GroundHitFxName);
  108. AudioManager.Instance.PlayAudio(hitAudioName, false);
  109. if (timeLineTriggerEvent != null)
  110. {
  111. timeLineTriggerEvent.TimeLineTriggerGround(
  112. TimeLineEventParticleLogicBasic.TimeLineEventLogicGroup.groupName,
  113. this, triggerData);
  114. }
  115. AudioManager.Instance.PlayAudio(hitAudioName, false);
  116. Dispose();
  117. }
  118. protected void GetTargetPos()
  119. {
  120. if (isUseCustomTargetEndPos)
  121. {
  122. endPos = TimeLineEventParticleLogicBasic.customizePos[
  123. customTargetEndPosIndex];
  124. }
  125. else
  126. {
  127. endPos = AttTarget.GetSpecialDotInfo("hitpos").GetWorlPos();
  128. // endPos = new Vector3(endPos.x, CurrPos.y, endPos.z);
  129. }
  130. }
  131. protected void OnTriggerEnterEvent(Collider collision, ITriggerEntity triggerEntity)
  132. {
  133. if (isTriggerGroundEnd)
  134. {
  135. if (collision.gameObject.CompareTag("dimian"))
  136. {
  137. TriggerGround();
  138. return;
  139. }
  140. }
  141. FxAILogicBasic fxAILogicBasic = triggerEntity as FxAILogicBasic;
  142. if (fxAILogicBasic != null) //击中其他的功法
  143. {
  144. SkillFeaturesData skillFeaturesData = fxAILogicBasic.SkillFeaturesData;
  145. if (skillFeaturesData.isEnemy == SkillFeaturesData.isEnemy)
  146. {
  147. return;
  148. }
  149. int myRestrained = SkillFeaturesData.GetRestrained(skillFeaturesData.SkillFeaturesType);
  150. int targetRestrained = skillFeaturesData.GetRestrained(SkillFeaturesData.SkillFeaturesType);
  151. int c = myRestrained - targetRestrained;
  152. int myHp = SkillFeaturesData.hp;
  153. int targetHp = skillFeaturesData.hp;
  154. if (c < 0) //我被压制
  155. {
  156. myHp -= CombatCalculateTool.Instance.GetVlaueRatioForInt(myHp, 10 * (Mathf.Abs(c)));
  157. }
  158. else if (c > 0)
  159. {
  160. targetHp -= CombatCalculateTool.Instance.GetVlaueRatioForInt(targetHp, 10 * (Mathf.Abs(c)));
  161. }
  162. if (myHp > targetHp)
  163. {
  164. myHp -= targetHp;
  165. SkillFeaturesData.hp = myHp;
  166. fxAILogicBasic.PlayHit();
  167. fxAILogicBasic.Dispose();
  168. }
  169. else if (myHp < targetHp)
  170. {
  171. targetHp -= myHp;
  172. skillFeaturesData.hp = targetHp;
  173. Dispose();
  174. PlayHit();
  175. }
  176. else if (myHp == targetHp)
  177. {
  178. myHp = 0;
  179. fxAILogicBasic.PlayHit();
  180. fxAILogicBasic.Dispose();
  181. Dispose();
  182. PlayHit();
  183. }
  184. return;
  185. }
  186. else
  187. {
  188. HeroEntityMono heroEntityMono = collision.gameObject.GetComponent<HeroEntityMono>();
  189. TriggerHero(collision, heroEntityMono);
  190. }
  191. }
  192. protected void TriggerHero(Collider collision, HeroEntityMono heroEntityMono)
  193. {
  194. if (heroEntityMono == null)
  195. {
  196. return;
  197. }
  198. CombatHeroEntity target = heroEntityMono.combatHeroEntity;
  199. if (target.IsEnemy == CombatHeroEntity.IsEnemy || target is CombatMagicWeaponEntity)
  200. {
  201. return;
  202. }
  203. if (TimeLineEventParticleLogicBasic.TimeLineEventLogicGroup.groupName == null)
  204. {
  205. Dispose();
  206. return;
  207. }
  208. ITimeLineTriggerEvent timeLineTriggerEvent =
  209. TimeLineEventParticleLogicBasic.ITimeLineTriggerEntity as ITimeLineTriggerEvent;
  210. if (timeLineTriggerEvent != null)
  211. {
  212. timeLineTriggerEvent.TimeLineTrigger(TimeLineEventParticleLogicBasic.TimeLineEventLogicGroup.groupName,
  213. target.GetMainHotPoin<ILifetCycleHitPoint>(), this, triggerData);
  214. if (!string.IsNullOrEmpty(hitFxName))
  215. {
  216. FinishHit(collision.ClosestPoint(gameObject.transform.position), hitFxName);
  217. }
  218. AudioManager.Instance.PlayAudio(hitAudioName, false);
  219. if (!isPenetrate)
  220. {
  221. Dispose();
  222. }
  223. }
  224. }
  225. private void FinishHit(Vector3 pos, string hitFxName)
  226. {
  227. if (!string.IsNullOrEmpty(hitFxName))
  228. {
  229. CombatController.currActiveCombat.GameTimeLineParticleFactory.CreateParticle(hitFxName,
  230. pos, null, false, null, null);
  231. }
  232. }
  233. protected override void ProCombatUpdate(float time)
  234. {
  235. switch (parabolaCurveType)
  236. {
  237. case CurveType.Beeline:
  238. Beeline(time);
  239. break;
  240. case CurveType.DynamicCurve:
  241. DynamicCurve(time);
  242. break;
  243. case CurveType.CustomizeCurve:
  244. CustomizeCurve(time);
  245. break;
  246. }
  247. // if (Vector3.Distance(endPos, _currPos) < 0.5f)
  248. // {
  249. // Dispose();
  250. // }
  251. }
  252. private void Beeline(float time)
  253. {
  254. GetTargetPos();
  255. Vector3 dir = (endPos - _currPos).normalized;
  256. // dir = Vector3.Lerp(startDir, dir, dirLerpTime).normalized;
  257. // startDir= dir;
  258. Vector3 lasetPos = _currPos;
  259. _currPos += dir * speed * time;
  260. gameObject.transform.position = _currPos;
  261. gameObject.transform.rotation = Quaternion.LookRotation(dir);
  262. }
  263. private void CustomizeCurve(float time)
  264. {
  265. currTime += _addTime * time;
  266. GetTargetPos();
  267. moveBezierPath.controlPoints[3] = endPos;
  268. // moveBezierPath.controlPoints[0] = (gameObject.transform.TransformPoint(BesselPath.controlPoints[0]));
  269. Vector3 p = moveBezierPath.CalculatePoint(currTime);
  270. Vector3 p2 = moveBezierPath.CalculatePoint(currTime - 0.01f);
  271. gameObject.transform.position = p;
  272. gameObject.transform.rotation = Quaternion.LookRotation((p - p2).normalized);
  273. }
  274. private void DynamicCurve(float time)
  275. {
  276. currTime += _addTime * time;
  277. dirLerpTime += time * 4F;
  278. if (dirLerpTime > 1)
  279. {
  280. dirLerpTime = 1;
  281. }
  282. GetTargetPos();
  283. Vector3 dir = (endPos - _currPos).normalized;
  284. dir = Vector3.Lerp(startDir, dir, dirLerpTime).normalized;
  285. // startDir= dir;
  286. Vector3 lasetPos = _currPos;
  287. _currPos += dir * speed * time;
  288. gameObject.transform.position = _currPos;
  289. gameObject.transform.rotation = Quaternion.LookRotation(dir);
  290. }
  291. protected override void ProDispose()
  292. {
  293. if (UnRegister != null)
  294. {
  295. UnRegister.UnRegister();
  296. }
  297. UnRegister = null;
  298. }
  299. private void OnDrawGizmos()
  300. {
  301. if (parabolaCurveType != CurveType.CustomizeCurve)
  302. {
  303. return;
  304. }
  305. if (BesselPath.controlPoints.Count < 4)
  306. {
  307. BesselPath.controlPoints.Add(Vector3.zero);
  308. BesselPath.controlPoints.Add(new Vector3(-1, 1, -1));
  309. BesselPath.controlPoints.Add(new Vector3(0, 0, 2));
  310. BesselPath.controlPoints.Add(new Vector3(0, 0, 5));
  311. return;
  312. }
  313. Gizmos.color = Color.blue;
  314. // 绘制控制点
  315. // 绘制曲线
  316. Vector3 previousPoint = BesselPath.CalculatePoint(0) + transform.position;
  317. int segments = 50;
  318. for (int i = 1; i <= segments; i++)
  319. {
  320. float t = i / (float)segments;
  321. Vector3 currentPoint = BesselPath.CalculatePoint(t) + transform.position;
  322. // Gizmos.DrawSphere(currentPoint, 0.03f);
  323. Gizmos.DrawLine(previousPoint, currentPoint);
  324. previousPoint = currentPoint;
  325. }
  326. }
  327. #if UNITY_EDITOR
  328. [CustomEditor(typeof(FxParabolaBulletLogic))]
  329. public class MovableCoordinateEditor : Editor
  330. {
  331. void OnSceneGUI()
  332. {
  333. FxParabolaBulletLogic fxParabolaBulletLogic = target as FxParabolaBulletLogic;
  334. if (fxParabolaBulletLogic.parabolaCurveType != CurveType.CustomizeCurve)
  335. {
  336. return;
  337. }
  338. if (fxParabolaBulletLogic.BesselPath.controlPoints.Count < 4)
  339. {
  340. return;
  341. }
  342. // 使用 Handles.PositionHandle 提供一个可拖动的控制柄
  343. for (int i = 0; i < fxParabolaBulletLogic.BesselPath.controlPoints.Count; i++)
  344. {
  345. EditorGUI.BeginChangeCheck();
  346. Vector3 newPosition = Handles.PositionHandle(
  347. fxParabolaBulletLogic.BesselPath.controlPoints[i] +
  348. fxParabolaBulletLogic.gameObject.transform.position,
  349. Quaternion.identity);
  350. if (EditorGUI.EndChangeCheck())
  351. {
  352. Undo.RecordObject(fxParabolaBulletLogic, "Move Coordinate");
  353. fxParabolaBulletLogic.BesselPath.controlPoints[i] =
  354. newPosition - fxParabolaBulletLogic.gameObject.transform.position;
  355. }
  356. }
  357. }
  358. }
  359. #endif
  360. }
  361. }