123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- using Adon.Game.BO;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- namespace AdonGameKit
- {
- /// <summary>
- /// 言灵子弹
- /// </summary>
- public class YLBulletAdon : MonoBehaviour
- {
- /// <summary>
- /// 子弹步进速度
- /// </summary>
- public float velocity = 30;
- /// <summary>
- /// 推进倍增值
- /// </summary>
- public float RaycastAdvance = 2f;
- /// <summary>
- /// 射线碰撞层
- /// </summary>
- public int curLayerMask = 0;
- /// <summary>
- /// 生命周期
- /// </summary>
- public float lifeTime = 5f;
- /// <summary>
- /// 是否延迟销毁
- /// </summary>
- public bool DelayDespawn = false;
- /// <summary>
- /// 粒子消退延迟时间
- /// </summary>
- public float despawnDelay = 0;
- public BaseYLPartAdon m_YL;//释放者挂件(言灵)
- public BaseHero m_Hero;//释放者(英雄)
- public string m_CurUseSkill;
- bool isHit = false;
- bool isFXSpawned = false; // 碰撞光效只生产一次
- RaycastHit hitPoint; //碰撞点
- float timer = 0f; // 子弹生命周期计时
- ParticleSystem[] particles;//子弹粒子系统数组
- void Awake()
- {
- curLayerMask = LayerMask.NameToLayer("Enemy");
- particles = GetComponentsInChildren<ParticleSystem>();
- }
- // Start is called before the first frame update
- void Start()
- {
- }
- void FixedUpdate()
- {
- if (isHit)//如果有碰撞
- {
- if (!isFXSpawned)//执行一次
- {
- isFXSpawned = true;
- DefendMonster defend = hitPoint.transform.GetComponent<DefendMonster>();
- if (defend != null)
- {
- Debug.Log("打中了防御罩");
- //实例化克隆防御光效
- Transform effobj = Instantiate(defend.m_DefendHitObj);
- if (!effobj.GetComponent<DestroyObjectDelayed>()) effobj.gameObject.AddComponent<DestroyObjectDelayed>();//添加延迟销毁脚本
- effobj.position = hitPoint.point;//设置爆炸位置
- effobj.gameObject.SetActive(true);
- //实例化克隆爆炸光效
- Transform effobj2 = Instantiate(m_YL.m_FxManagerAdon.m_BombEff[0]);
- if (!effobj2.GetComponent<DestroyObjectDelayed>()) effobj2.gameObject.AddComponent<DestroyObjectDelayed>();//添加延迟销毁脚本
- effobj2.position = hitPoint.point;//设置爆炸位置
- effobj2.gameObject.SetActive(true);
- //计算角度子弹与盾对角度差
- //Vector3 targetDir = transform.forward - hitPoint.transform.forward ;
- //float angle = Vector3.Angle(targetDir, transform.forward);
- //print("角度B:" + angle);
- float angle = Random.Range(-60, 60);
- //反射一颗新子弹
- Transform newBulle = Instantiate(transform);
- newBulle.position = hitPoint.point;
- newBulle.eulerAngles = new Vector3(hitPoint.transform.eulerAngles.z, hitPoint.transform.eulerAngles.y - angle, hitPoint.transform.eulerAngles.z);
- newBulle.GetComponent<YLBulletAdon>().curLayerMask = LayerMask.NameToLayer("Player");
- }
- else
- {
- BaseMonsterAdon component = hitPoint.transform.GetComponent<BaseMonsterAdon>();
- if (component != null)
- {
- //实例化克隆爆炸光效
- Transform effobj = Instantiate(m_YL.m_FxManagerAdon.m_BombEff[0]);
- if (!effobj.GetComponent<DestroyObjectDelayed>()) effobj.gameObject.AddComponent<DestroyObjectDelayed>();//添加延迟销毁脚本
- effobj.position = hitPoint.point;//设置爆炸位置
- effobj.gameObject.SetActive(true);
- //发送伤害消息
- //X2Battle.X2BattleManager.Instance.mBulletModel.OnCastSkill(
- // m_Hero.mData.UID, m_CurUseSkill,
- // false, m_YL.M_YLType, component.mData.UID, false);
- BulletMessage.catSkillFun(m_Hero.mData.UID, m_CurUseSkill,
- false, (int)m_YL.M_YLType, component.mData.UID, false);
- }
- }
- }
- // 不延迟则销毁
- if (!DelayDespawn || (DelayDespawn && (timer >= despawnDelay)))
- OnProjectileDestroy();//销毁
- }
- else
- {
- Vector3 step = transform.forward * Time.deltaTime * velocity;//子弹步进
- // 如果射线碰撞
- if (Physics.Raycast(transform.position, transform.forward, out hitPoint, step.magnitude * RaycastAdvance, 1 << curLayerMask))
- {
- isHit = true;//碰撞
- if (DelayDespawn)//粒子延迟淡出再销毁
- {
- timer = 0f;
- Delay();//碰撞后粒子延迟消逝
- }
- }
- else
- {
- // 生命周期到了则销毁
- if (timer >= lifeTime)
- OnProjectileDestroy();
- }
- transform.position += step;//向前步进
- }
- // Updates projectile timer
- timer += Time.deltaTime;
- }
- /// <summary>
- /// 子弹销毁
- /// </summary>
- void OnProjectileDestroy()
- {
- //销毁,后期改为对象池
- Destroy(this.gameObject);
- }
- /// <summary>
- /// 停止粒子释放并逐渐消逝
- /// </summary>
- void Delay()
- {
- if (particles.Length > 0)
- {
- for (int i = 0; i < particles.Length; i++)
- {
- particles[i].Stop(false);
- }
- }
- }
- }
- }
|