using Adon.Game.BO;
using Adon.Game.Helper;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace AdonGameKit
{
///
/// 言灵距离形子弹
///
public class YLDistanceBulletAdon : MonoBehaviour
{
///
/// 子弹步进速度
///
public float m_Velocity = 10;
///
/// 子弹最大距离
///
public float m_DistanceMax = 10;
///
/// 攻击距离
///
public float m_AttackRange = 5;
///
/// 生命周期
///
public float m_LifeTime = 2f;
public BaseYLPartAdon m_YL;//释放者挂件(言灵)
public BaseHero m_Hero;//释放者(英雄)
public string m_CurUseSkill;
public Transform m_HitEff;//受击光效
///
/// 子弹当前距离
///
private float curDistance = 0;
bool isFXSpawned = false; // 碰撞光效只生产一次
float timer = 0f; // 子弹生命周期计时
void Awake()
{
}
// Start is called before the first frame update
void Start()
{
}
void FixedUpdate()
{
//计算距离,达到距离则悬停,添加开启碰撞检测
if (!isFXSpawned)//执行一次
{
if (curDistance < m_DistanceMax)//距离计算
{
Vector3 step = transform.forward * Time.deltaTime * m_Velocity;//子弹步进
curDistance += Vector3.Distance(transform.position, (transform.position + step));//距离增加
transform.position += step;//向前步进
}
else
{
//达到距离则执行攻击
isFXSpawned = true;
//查找周围敌人
List monsters = PlayerHelperAdon.GetNearEnemysDistance(this.gameObject, m_AttackRange);
for (int i = 0; i < monsters.Count; i++)
{
BaseMonsterAdon component = monsters[i].gameObject.GetComponent();
//发送伤害消息
X2Battle.X2BattleManager.Instance.mBulletModel.OnCastSkill(m_Hero.mData.UID, m_YL.m_CurUseSkill, true, m_YL.M_YLType, component.mData.UID, false);
//释放受击效果
Transform hitEffObj = Instantiate(m_HitEff);
hitEffObj.gameObject.SetActive(true);
hitEffObj.position = component.m_HitObj.position;
hitEffObj.LookAt(transform.position);
hitEffObj.eulerAngles = new Vector3(0,hitEffObj.eulerAngles.y-180,0);
//hitEffObj.rotation = Quaternion.Euler(component.transform.position - transform.position);
//hitEffObj.LookAt(transform);
if (!hitEffObj.GetComponent()) hitEffObj.gameObject.AddComponent();//添加延迟销毁脚本
}
}
}
// 生命周期到了则销毁
if (timer >= m_LifeTime)
OnProjectileDestroy();
timer += Time.deltaTime;
}
///
/// 子弹销毁
///
void OnProjectileDestroy()
{
//销毁,后期改为对象池
Destroy(this.gameObject);
}
}
}