YLMagicCircleAdon.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 YLMagicCircleAdon : 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. public BaseYLPartAdon m_YL;//释放者挂件(言灵)
  33. public BaseHero m_Hero;//释放者(英雄)
  34. bool isEeach = false;//到达目标位置
  35. bool isFXSpawned = false; // 爆炸光效只生产一次
  36. public Vector3 m_TargetPoint; //目标点
  37. public string m_CurUseSkill;
  38. Vector3 startPoint;//开始的点
  39. float timer = 0f; // 子弹生命周期计时
  40. ParticleSystem[] particles;//子弹粒子系统数组
  41. void Awake()
  42. {
  43. particles = GetComponentsInChildren<ParticleSystem>();
  44. }
  45. // Start is called before the first frame update
  46. void Start()
  47. {
  48. startPoint = transform.position;
  49. }
  50. void FixedUpdate()
  51. {
  52. if (isEeach)//如果到达目标位置
  53. {
  54. if (!isFXSpawned)//只执行一次爆炸生产
  55. {
  56. isFXSpawned = true;
  57. //实例化克隆爆炸光效
  58. Transform effobj = Instantiate(m_YL.m_FxManagerAdon.m_BombEff[0]);
  59. if (!effobj.GetComponent<DestroyObjectDelayed>()) effobj.gameObject.AddComponent<DestroyObjectDelayed>();//添加延迟销毁脚本
  60. effobj.position = m_TargetPoint;//设置爆炸位置
  61. //设置言灵爆炸脚本
  62. YLMagicCircleBomb bomb = effobj.gameObject.AddComponent<YLMagicCircleBomb>();
  63. bomb.SetHero(m_Hero);
  64. bomb.SetBombEff(m_YL.m_FxManagerAdon.m_AttackedEff[0]);
  65. bomb.m_CurUseSkill = m_CurUseSkill;
  66. bomb.m_YL = m_YL;
  67. effobj.gameObject.SetActive(true);
  68. }
  69. // 不延迟则销毁
  70. if (!DelayDespawn || (DelayDespawn && (timer >= despawnDelay)))
  71. OnProjectileDestroy();//销毁
  72. }
  73. else
  74. {
  75. Vector3 step = transform.forward * Time.deltaTime * velocity;//子弹步进
  76. transform.position += step;//向前步进
  77. // 如果到达目标位置,计算与目标对距离
  78. float distance = Vector3.Distance(startPoint, m_TargetPoint);//到达目标的距离
  79. float distance2 = Vector3.Distance(startPoint, transform.position);
  80. if (distance2 >= distance)
  81. {
  82. isEeach = true;
  83. if (DelayDespawn)//延迟重试
  84. {
  85. timer = 0f;
  86. Delay();//碰撞后粒子延迟消逝
  87. }
  88. }
  89. else
  90. {
  91. // 生命周期到了则销毁
  92. if (timer >= lifeTime)
  93. OnProjectileDestroy();
  94. }
  95. }
  96. // Updates projectile timer
  97. timer += Time.deltaTime;
  98. }
  99. /// <summary>
  100. /// 子弹销毁
  101. /// </summary>
  102. void OnProjectileDestroy()
  103. {
  104. //销毁,后期改为对象池
  105. Destroy(this.gameObject);
  106. }
  107. /// <summary>
  108. /// 停止粒子释放并逐渐消逝
  109. /// </summary>
  110. void Delay()
  111. {
  112. if (particles.Length > 0)
  113. {
  114. for (int i = 0; i < particles.Length; i++)
  115. {
  116. particles[i].Stop(false);
  117. }
  118. }
  119. }
  120. }
  121. }