using Adon.Game.BO;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace AdonGameKit
{
///
/// 言灵法阵
///
public class YLMagicCircleAdon : MonoBehaviour
{
///
/// 步进速度
///
public float velocity = 30f;
///
/// 最小距离
///
public float minDistance = 0.1f;
///
/// 生命周期
///
public float lifeTime = 5f;
///
/// 是否延迟销毁
///
public bool DelayDespawn = false;
///
/// 粒子消退延迟时间
///
public float despawnDelay;
public BaseYLPartAdon m_YL;//释放者挂件(言灵)
public BaseHero m_Hero;//释放者(英雄)
bool isEeach = false;//到达目标位置
bool isFXSpawned = false; // 爆炸光效只生产一次
public Vector3 m_TargetPoint; //目标点
public string m_CurUseSkill;
Vector3 startPoint;//开始的点
float timer = 0f; // 子弹生命周期计时
ParticleSystem[] particles;//子弹粒子系统数组
void Awake()
{
particles = GetComponentsInChildren();
}
// Start is called before the first frame update
void Start()
{
startPoint = transform.position;
}
void FixedUpdate()
{
if (isEeach)//如果到达目标位置
{
if (!isFXSpawned)//只执行一次爆炸生产
{
isFXSpawned = true;
//实例化克隆爆炸光效
Transform effobj = Instantiate(m_YL.m_FxManagerAdon.m_BombEff[0]);
if (!effobj.GetComponent()) effobj.gameObject.AddComponent();//添加延迟销毁脚本
effobj.position = m_TargetPoint;//设置爆炸位置
//设置言灵爆炸脚本
YLMagicCircleBomb bomb = effobj.gameObject.AddComponent();
bomb.SetHero(m_Hero);
bomb.SetBombEff(m_YL.m_FxManagerAdon.m_AttackedEff[0]);
bomb.m_CurUseSkill = m_CurUseSkill;
bomb.m_YL = m_YL;
effobj.gameObject.SetActive(true);
}
// 不延迟则销毁
if (!DelayDespawn || (DelayDespawn && (timer >= despawnDelay)))
OnProjectileDestroy();//销毁
}
else
{
Vector3 step = transform.forward * Time.deltaTime * velocity;//子弹步进
transform.position += step;//向前步进
// 如果到达目标位置,计算与目标对距离
float distance = Vector3.Distance(startPoint, m_TargetPoint);//到达目标的距离
float distance2 = Vector3.Distance(startPoint, transform.position);
if (distance2 >= distance)
{
isEeach = true;
if (DelayDespawn)//延迟重试
{
timer = 0f;
Delay();//碰撞后粒子延迟消逝
}
}
else
{
// 生命周期到了则销毁
if (timer >= lifeTime)
OnProjectileDestroy();
}
}
// 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);
}
}
}
}
}