YLBulletAdon.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 YLBulletAdon : MonoBehaviour
  11. {
  12. /// <summary>
  13. /// 子弹步进速度
  14. /// </summary>
  15. public float velocity = 30;
  16. /// <summary>
  17. /// 推进倍增值
  18. /// </summary>
  19. public float RaycastAdvance = 2f;
  20. /// <summary>
  21. /// 射线碰撞层
  22. /// </summary>
  23. public int curLayerMask = 0;
  24. /// <summary>
  25. /// 生命周期
  26. /// </summary>
  27. public float lifeTime = 5f;
  28. /// <summary>
  29. /// 是否延迟销毁
  30. /// </summary>
  31. public bool DelayDespawn = false;
  32. /// <summary>
  33. /// 粒子消退延迟时间
  34. /// </summary>
  35. public float despawnDelay = 0;
  36. public BaseYLPartAdon m_YL;//释放者挂件(言灵)
  37. public BaseHero m_Hero;//释放者(英雄)
  38. public string m_CurUseSkill;
  39. bool isHit = false;
  40. bool isFXSpawned = false; // 碰撞光效只生产一次
  41. RaycastHit hitPoint; //碰撞点
  42. float timer = 0f; // 子弹生命周期计时
  43. ParticleSystem[] particles;//子弹粒子系统数组
  44. void Awake()
  45. {
  46. curLayerMask = LayerMask.NameToLayer("Enemy");
  47. particles = GetComponentsInChildren<ParticleSystem>();
  48. }
  49. // Start is called before the first frame update
  50. void Start()
  51. {
  52. }
  53. void FixedUpdate()
  54. {
  55. if (isHit)//如果有碰撞
  56. {
  57. if (!isFXSpawned)//执行一次
  58. {
  59. isFXSpawned = true;
  60. DefendMonster defend = hitPoint.transform.GetComponent<DefendMonster>();
  61. if (defend != null)
  62. {
  63. Debug.Log("打中了防御罩");
  64. //实例化克隆防御光效
  65. Transform effobj = Instantiate(defend.m_DefendHitObj);
  66. if (!effobj.GetComponent<DestroyObjectDelayed>()) effobj.gameObject.AddComponent<DestroyObjectDelayed>();//添加延迟销毁脚本
  67. effobj.position = hitPoint.point;//设置爆炸位置
  68. effobj.gameObject.SetActive(true);
  69. //实例化克隆爆炸光效
  70. Transform effobj2 = Instantiate(m_YL.m_FxManagerAdon.m_BombEff[0]);
  71. if (!effobj2.GetComponent<DestroyObjectDelayed>()) effobj2.gameObject.AddComponent<DestroyObjectDelayed>();//添加延迟销毁脚本
  72. effobj2.position = hitPoint.point;//设置爆炸位置
  73. effobj2.gameObject.SetActive(true);
  74. //计算角度子弹与盾对角度差
  75. //Vector3 targetDir = transform.forward - hitPoint.transform.forward ;
  76. //float angle = Vector3.Angle(targetDir, transform.forward);
  77. //print("角度B:" + angle);
  78. float angle = Random.Range(-60, 60);
  79. //反射一颗新子弹
  80. Transform newBulle = Instantiate(transform);
  81. newBulle.position = hitPoint.point;
  82. newBulle.eulerAngles = new Vector3(hitPoint.transform.eulerAngles.z, hitPoint.transform.eulerAngles.y - angle, hitPoint.transform.eulerAngles.z);
  83. newBulle.GetComponent<YLBulletAdon>().curLayerMask = LayerMask.NameToLayer("Player");
  84. }
  85. else
  86. {
  87. BaseMonsterAdon component = hitPoint.transform.GetComponent<BaseMonsterAdon>();
  88. if (component != null)
  89. {
  90. //实例化克隆爆炸光效
  91. Transform effobj = Instantiate(m_YL.m_FxManagerAdon.m_BombEff[0]);
  92. if (!effobj.GetComponent<DestroyObjectDelayed>()) effobj.gameObject.AddComponent<DestroyObjectDelayed>();//添加延迟销毁脚本
  93. effobj.position = hitPoint.point;//设置爆炸位置
  94. effobj.gameObject.SetActive(true);
  95. //发送伤害消息
  96. //X2Battle.X2BattleManager.Instance.mBulletModel.OnCastSkill(
  97. // m_Hero.mData.UID, m_CurUseSkill,
  98. // false, m_YL.M_YLType, component.mData.UID, false);
  99. BulletMessage.catSkillFun(m_Hero.mData.UID, m_CurUseSkill,
  100. false, (int)m_YL.M_YLType, component.mData.UID, false);
  101. }
  102. }
  103. }
  104. // 不延迟则销毁
  105. if (!DelayDespawn || (DelayDespawn && (timer >= despawnDelay)))
  106. OnProjectileDestroy();//销毁
  107. }
  108. else
  109. {
  110. Vector3 step = transform.forward * Time.deltaTime * velocity;//子弹步进
  111. // 如果射线碰撞
  112. if (Physics.Raycast(transform.position, transform.forward, out hitPoint, step.magnitude * RaycastAdvance, 1 << curLayerMask))
  113. {
  114. isHit = true;//碰撞
  115. if (DelayDespawn)//粒子延迟淡出再销毁
  116. {
  117. timer = 0f;
  118. Delay();//碰撞后粒子延迟消逝
  119. }
  120. }
  121. else
  122. {
  123. // 生命周期到了则销毁
  124. if (timer >= lifeTime)
  125. OnProjectileDestroy();
  126. }
  127. transform.position += step;//向前步进
  128. }
  129. // Updates projectile timer
  130. timer += Time.deltaTime;
  131. }
  132. /// <summary>
  133. /// 子弹销毁
  134. /// </summary>
  135. void OnProjectileDestroy()
  136. {
  137. //销毁,后期改为对象池
  138. Destroy(this.gameObject);
  139. }
  140. /// <summary>
  141. /// 停止粒子释放并逐渐消逝
  142. /// </summary>
  143. void Delay()
  144. {
  145. if (particles.Length > 0)
  146. {
  147. for (int i = 0; i < particles.Length; i++)
  148. {
  149. particles[i].Stop(false);
  150. }
  151. }
  152. }
  153. }
  154. }