YLStarrySkyAdon.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. using Adon.Game.BO;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. namespace AdonGameKit
  6. {
  7. /// <summary>
  8. /// 言灵星空法阵子弹
  9. /// </summary>
  10. public class YLStarrySkyAdon : MonoBehaviour
  11. {
  12. /// <summary>
  13. /// 步进速度
  14. /// </summary>
  15. public float velocity = 30f;
  16. /// <summary>
  17. /// 最小距离
  18. /// </summary>
  19. public float minDistance = 0.1f;
  20. /// <summary>
  21. /// 生命周期
  22. /// </summary>
  23. public float lifeTime = 5f;
  24. /// <summary>
  25. /// 是否延迟销毁
  26. /// </summary>
  27. public bool DelayDespawn = false;
  28. /// <summary>
  29. /// 粒子消退延迟时间
  30. /// </summary>
  31. public float despawnDelay;
  32. [SerializeField]
  33. private BaseYLPartAdon m_YL;//释放者挂件(言灵)
  34. [SerializeField]
  35. private BaseHero m_Hero;//释放者(英雄)
  36. bool isEeach = false;//到达目标位置
  37. bool isFXSpawned = false; // 爆炸光效只生产一次
  38. [SerializeField]
  39. private Vector3 m_TargetPoint; //目标点
  40. [SerializeField]
  41. private string m_CurUseSkill;//当前技能ID
  42. Vector3 startPoint;//开始的点
  43. float timer = 0f; // 子弹生命周期计时
  44. ParticleSystem[] particles;//子弹粒子系统数组
  45. /// <summary>
  46. /// 初始化
  47. /// </summary>
  48. /// <param name="_YL">释放者挂件(言灵)</param>
  49. /// <param name="_hero">释放者(英雄)</param>
  50. /// <param name="_TargetPoint">目标点</param>
  51. /// <param name="_CurUseSkill">当前技能ID</param>
  52. public void init(BaseYLPartAdon _YL, BaseHero _hero, Vector3 _TargetPoint, string _CurUseSkill)
  53. {
  54. m_YL = _YL;
  55. m_Hero = _hero;
  56. m_TargetPoint = _TargetPoint;
  57. m_CurUseSkill = _CurUseSkill;
  58. }
  59. void Awake()
  60. {
  61. particles = GetComponentsInChildren<ParticleSystem>();
  62. }
  63. // Start is called before the first frame update
  64. void Start()
  65. {
  66. startPoint = transform.position;
  67. }
  68. void FixedUpdate()
  69. {
  70. if (isEeach)//如果到达目标位置
  71. {
  72. if (!isFXSpawned)//只执行一次爆炸生产
  73. {
  74. //实例化克隆爆炸光效
  75. Transform effobj = Instantiate(m_YL.m_FxManagerAdon.m_BombEff[0]);
  76. if (!effobj.GetComponent<DestroyObjectDelayed>()) effobj.gameObject.AddComponent<DestroyObjectDelayed>();//添加延迟销毁脚本
  77. effobj.position = m_TargetPoint;//设置爆炸位置
  78. //设置言灵爆炸脚本
  79. YLStarrySkyBomb skyBomb = effobj.gameObject.AddComponent<YLStarrySkyBomb>();
  80. skyBomb.init(m_YL,m_Hero, m_YL.m_FxManagerAdon.m_AttackedEff[0], m_CurUseSkill);
  81. effobj.gameObject.SetActive(true);
  82. isFXSpawned = true;
  83. }
  84. // 不延迟则销毁
  85. if (!DelayDespawn || (DelayDespawn && (timer >= despawnDelay)))
  86. OnProjectileDestroy();//销毁
  87. }
  88. else
  89. {
  90. Vector3 step = transform.forward * Time.deltaTime * velocity;//子弹步进
  91. transform.position += step;//向前步进
  92. // 如果到达目标位置,计算与目标对距离
  93. float distance = Vector3.Distance(startPoint, m_TargetPoint);//到达目标的距离
  94. float distance2 = Vector3.Distance(startPoint, transform.position);
  95. if (distance2 >= distance)
  96. {
  97. isEeach = true;
  98. if (DelayDespawn)//延迟重试
  99. {
  100. timer = 0f;
  101. Delay();//碰撞后粒子延迟消逝
  102. }
  103. }
  104. else
  105. {
  106. // 生命周期到了则销毁
  107. if (timer >= lifeTime)
  108. OnProjectileDestroy();
  109. }
  110. }
  111. // Updates projectile timer
  112. timer += Time.deltaTime;
  113. }
  114. /// <summary>
  115. /// 子弹销毁
  116. /// </summary>
  117. void OnProjectileDestroy()
  118. {
  119. //销毁,后期改为对象池
  120. Destroy(this.gameObject);
  121. }
  122. /// <summary>
  123. /// 停止粒子释放并逐渐消逝
  124. /// </summary>
  125. void Delay()
  126. {
  127. if (particles.Length > 0)
  128. {
  129. for (int i = 0; i < particles.Length; i++)
  130. {
  131. particles[i].Stop(false);
  132. }
  133. }
  134. }
  135. }
  136. }