YLParabolicBulletAdon.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. using Adon.Game.BO;
  2. using Adon.Game.Helper;
  3. using DG.Tweening;
  4. using System;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using UnityEngine;
  8. namespace AdonGameKit
  9. {
  10. /// <summary>
  11. /// 言灵 抛物线子弹
  12. /// </summary>
  13. public class YLParabolicBulletAdon : MonoBehaviour
  14. {
  15. /// <summary>
  16. /// 子弹步进速度
  17. /// </summary>
  18. public float velocity = 30;
  19. /// <summary>
  20. /// 推进倍增值
  21. /// </summary>
  22. public float RaycastAdvance = 2f;
  23. /// <summary>
  24. /// 射线碰撞层
  25. /// </summary>
  26. public int curLayerMask = 0;
  27. /// <summary>
  28. /// 生命周期
  29. /// </summary>
  30. public float lifeTime = 5f;
  31. /// <summary>
  32. /// 是否延迟销毁
  33. /// </summary>
  34. public bool DelayDespawn = false;
  35. /// <summary>
  36. /// 粒子消退延迟时间
  37. /// </summary>
  38. public float despawnDelay = 0;
  39. public BaseYLPartAdon m_YL;//释放者挂件(言灵)
  40. public BaseHero m_Hero;//释放者(英雄)
  41. public string m_CurUseSkill;
  42. public Transform m_HitEff;
  43. /// <summary>
  44. /// 抛物完成回掉
  45. /// </summary>
  46. public Action<GameObject> m_OnComplete;
  47. bool isHit = false;
  48. bool isFXSpawned = false; // 碰撞光效只生产一次
  49. RaycastHit hitPoint; //碰撞点
  50. float timer = 0f; // 子弹生命周期计时
  51. ParticleSystem[] particles;//子弹粒子系统数组
  52. Sequence seq;
  53. Vector3 m_targetPot;
  54. void Awake()
  55. {
  56. curLayerMask = LayerMask.NameToLayer("Enemy");
  57. particles = GetComponentsInChildren<ParticleSystem>();
  58. seq = DOTween.Sequence();
  59. }
  60. // Start is called before the first frame update
  61. void Start()
  62. {
  63. }
  64. void FixedUpdate()
  65. {
  66. if (isHit)//如果有碰撞
  67. {
  68. if (!isFXSpawned)//执行一次
  69. {
  70. isFXSpawned = true;
  71. BaseMonsterAdon component = hitPoint.transform.GetComponent<BaseMonsterAdon>();
  72. if (component != null)
  73. {
  74. //实例化克隆爆炸光效
  75. Transform effobj = Instantiate(m_HitEff);
  76. if (!effobj.GetComponent<DestroyObjectDelayed>()) effobj.gameObject.AddComponent<DestroyObjectDelayed>();//添加延迟销毁脚本
  77. effobj.position = new Vector3(hitPoint.point.x, component.transform.position.y, hitPoint.point.z);//设置爆炸位置
  78. effobj.gameObject.SetActive(true);
  79. if (m_OnComplete != null) m_OnComplete(effobj.gameObject);//抛物线完成回调
  80. //发送伤害消息
  81. //X2Battle.X2BattleManager.Instance.mBulletModel.OnCastSkill(
  82. // m_Hero.mData.UID, m_CurUseSkill,
  83. // false, m_YL.M_YLType, component.mData.UID, false);
  84. BulletMessage.catSkillFun(m_Hero.mData.UID, m_CurUseSkill,
  85. false, (int)m_YL.M_YLType, component.mData.UID, false);
  86. }
  87. }
  88. // 不延迟则销毁
  89. if (!DelayDespawn || (DelayDespawn && (timer >= despawnDelay)))
  90. OnProjectileDestroy();//销毁
  91. }
  92. else
  93. {
  94. Vector3 step = transform.forward * Time.deltaTime * velocity;//子弹步进
  95. // 如果射线碰撞
  96. if (Physics.Raycast(transform.position, transform.forward, out hitPoint, step.magnitude * RaycastAdvance, 1 << curLayerMask))
  97. {
  98. isHit = true;//碰撞
  99. if (DelayDespawn)//粒子延迟淡出再销毁
  100. {
  101. timer = 0f;
  102. Delay();//碰撞后粒子延迟消逝
  103. }
  104. }
  105. else
  106. {
  107. // 生命周期到了则销毁
  108. if (timer >= lifeTime)
  109. OnProjectileDestroy();
  110. }
  111. }
  112. // Updates projectile timer
  113. timer += Time.deltaTime;
  114. }
  115. /// <summary>
  116. /// 子弹销毁
  117. /// </summary>
  118. void OnProjectileDestroy()
  119. {
  120. //销毁,后期改为对象池
  121. Destroy(this.gameObject);
  122. }
  123. /// <summary>
  124. /// 停止粒子释放并逐渐消逝
  125. /// </summary>
  126. void Delay()
  127. {
  128. if (particles.Length > 0)
  129. {
  130. for (int i = 0; i < particles.Length; i++)
  131. {
  132. particles[i].Stop(false);
  133. }
  134. }
  135. }
  136. public void ParabolicDoTween(Vector3 targetPot)
  137. {
  138. m_targetPot = targetPot;
  139. var ParabolicDis = Vector3.Distance(targetPot, this.transform.position);
  140. var ParabolicDown = targetPot.y;
  141. var speed = 5.0f;
  142. var time = ParabolicDis / speed;
  143. seq.Append(this.transform.DOMove(targetPot, time).SetEase(Ease.Linear));
  144. //seq.Append(this.transform.DOMoveZ(targetPot.z, time / 2).SetEase(Ease.Unset));
  145. seq.Insert(0, this.transform.DOMoveY(targetPot.y + 3, time * 0.5f).SetEase(Ease.OutCirc));
  146. seq.Insert(time * 0.5f, this.transform.DOMoveY(ParabolicDown, time * 0.5f).SetEase(Ease.InCirc));
  147. seq.OnComplete<Sequence>(() => { OnDoTweenComplete(); });
  148. }
  149. public void OnDoTweenComplete()
  150. {
  151. Debug.Log("抛物线完成!");
  152. //实例化克隆爆炸光效
  153. Transform effobj = Instantiate(m_HitEff);
  154. if (!effobj.GetComponent<DestroyObjectDelayed>()) effobj.gameObject.AddComponent<DestroyObjectDelayed>();//添加延迟销毁脚本
  155. effobj.position = m_targetPot;//设置爆炸位置
  156. effobj.gameObject.SetActive(true);
  157. if (m_OnComplete != null) m_OnComplete(effobj.gameObject);
  158. Destroy(this.gameObject);
  159. }
  160. }
  161. }