using Adon.Game.BO;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace AdonGameKit
{
///
/// 言灵子弹
///
public class BaseBulletAdon : MonoBehaviour
{
///
/// 子弹步进速度
///
public float velocity = 300f;
///
/// 推进倍增值
///
public float RaycastAdvance = 2f;
///
/// 射线碰撞层
///
public LayerMask layerMask;
///
/// 生命周期
///
public float lifeTime = 5f;
///
/// 是否延迟销毁
///
public bool DelayDespawn = false;
///
/// 粒子消退延迟时间
///
public float despawnDelay;
//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()
{
particles = GetComponentsInChildren();
}
// Start is called before the first frame update
void Start()
{
}
public void Init(BaseHero _Hero, float _velocity = 300f)
{
m_Hero = _Hero;
velocity = _velocity;
layerMask = LayerMask.GetMask("Enemy");
}
void FixedUpdate()
{
if (isHit)//如果有碰撞
{
if (!isFXSpawned)//执行一次
{
isFXSpawned = true;
DefendMonster defend = hitPoint.transform.GetComponent();
if(defend != null)
{
Debug.Log("打中了防御罩");
//实例化克隆防御光效
Transform effobj = Instantiate(defend.m_DefendHitObj);
if (!effobj.GetComponent()) effobj.gameObject.AddComponent();//添加延迟销毁脚本
effobj.position = hitPoint.point;//设置爆炸位置
effobj.gameObject.SetActive(true);
//实例化克隆爆炸光效
Transform effobj2 = Instantiate(m_Hero.m_FxManagerAdon.m_BombEff[0]);
if (!effobj2.GetComponent()) effobj2.gameObject.AddComponent();//添加延迟销毁脚本
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().curLayerMask = LayerMask.NameToLayer("Player");
}
else
{
BaseMonsterAdon component = hitPoint.transform.GetComponent();
if(component != null)
{
//实例化克隆爆炸光效
Transform effobj = Instantiate(m_Hero.m_FxManagerAdon.m_BombEff[0]);
if (!effobj.GetComponent()) effobj.gameObject.AddComponent();//添加延迟销毁脚本
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);
int AttackIdx = m_Hero.m_CurAttackIdx;
if (AttackIdx == 0) AttackIdx = 1;
//X2Battle.X2BattleManager.Instance.mBulletModel.OnCastSkill(
// m_Hero.mData.UID, m_Hero.mData.GetSKillNormal(1, AttackIdx),
// false, X2Battle.EBulletCastPoint.ECAST_POINT_DEFLUAT, component.mData.UID, false);
BulletMessage.catSkillFun(m_Hero.mData.UID, m_Hero.mData.GetSKillNormal(1, AttackIdx),
false, (int)X2Battle.EBulletCastPoint.ECAST_POINT_DEFLUAT, 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,
layerMask))
{
isHit = true;//碰撞
if (DelayDespawn)//粒子延迟淡出再销毁
{
timer = 0f;
Delay();//碰撞后粒子延迟消逝
}
}
else
{
// 生命周期到了则销毁
if (timer >= lifeTime)
OnProjectileDestroy();
}
transform.position += step;//向前步进
}
// Updates projectile timer
timer += Time.deltaTime;
}
///
/// 子弹销毁
///
void OnProjectileDestroy()
{
//销毁,后期改为对象池
Destroy(this.gameObject);
}
///
/// 停止粒子释放并逐渐消逝
///
void Delay()
{
if (particles.Length > 0)
{
for (int i = 0; i < particles.Length; i++)
{
particles[i].Stop(false);
}
}
}
}
}