BaseBulletAdon.cs 7.2 KB

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