using Core.Health;
using Core.Utilities;
using System.Collections;
using System.Collections.Generic;
using TowerDefense.Agents;
using UnityEngine;
///
/// 游戏内的电弧效果处理,这个主要是显示从A点电击到B点的电弧特效
///
public class LightBoltEffect : AgentEffect
{
///
/// 电弧的播放特效
///
protected GameObject m_LightBoltFx;
///
/// 电弧击中的效果
///
protected GameObject m_LightHitFx;
///
/// 删除Timer.
///
protected Timer m_DestroyTimer;
///
/// 用于备份粒子系统的默认Scale,在设置完Parent之后,会有对自己Scale的设置,从而导致还回内存池之后,
/// LocalScale会缩小的情况发生。
///
protected Vector3 m_BackScale;
protected Vector3 m_BackScale1;
///
/// 在srcAgent和dstAgent之间播放一个闪电弧。
///
///
///
///
///
///
public void Initialize(float maxPlaySec, GameObject lightBoltPrefab ,GameObject lightHit, Agent srcAgent,Agent dstAgent )
{
m_LightBoltFx = Poolable.TryGetPoolable(lightBoltPrefab);
if (!m_LightBoltFx) return;
m_BackScale = m_LightBoltFx.transform.localScale;
m_LightBoltFx.transform.SetParent(transform, true);
// 获取位置和放置方向,播放当前的特效
Vector3 offsetPos = dstAgent.position - srcAgent.position;
m_LightBoltFx.transform.localPosition = Vector3.zero;
float xScale = offsetPos.magnitude / 10.0f;
if (xScale < 1) xScale = 1;
xScale *= 0.5f;
m_LightBoltFx.transform.localScale = new Vector3(xScale, 1.0f, 1.0f);
// 解决 Look rotation viewing vector is zero 这个异常的Bug.
if (offsetPos.magnitude < 0.0001f)
{
offsetPos.y = 1;
}
// 旋转方向的处理:
Quaternion look = Quaternion.LookRotation(offsetPos, Vector3.up);
m_LightBoltFx.transform.rotation = look;
// 处理其它相关的内容.
var particle = m_LightBoltFx.GetComponent();
if (particle)
{
particle.Simulate(0.0f);
particle.Play();
}
// 处理LightHitEffect.
m_LightHitFx = Poolable.TryGetPoolable(lightHit);
m_BackScale1 = m_LightHitFx.transform.localScale;
m_LightHitFx.transform.SetParent(transform, true);
m_LightHitFx.transform.localPosition = Vector3.zero;
m_LightHitFx.transform.localScale = new Vector3(2.0f, 2.0f, 2.0f);
particle = m_LightHitFx.GetComponent();
if (particle)
{
particle.Simulate(0.0f);
particle.Play();
}
// 死亡后直接去除.
m_Agent.removed += OnRemoved;
m_DestroyTimer = new Timer(maxPlaySec, RemoveEffect);
}
///
/// A simply function to convert an angle to a -180/180 wrap
///
static float Wrap180(float angle)
{
angle %= 360;
if (angle < -180)
{
angle += 360;
}
else if (angle > 180)
{
angle -= 360;
}
return angle;
}
///
/// 获取新的Vec相对于(0,0,1)绕Y轴的旋转角度.
///
///
///
protected float getYRotAngle( Vector3 vec3rot)
{
return 0;
}
// Update is called once per frame
void Update()
{
if( m_DestroyTimer != null )
m_DestroyTimer.Tick( Time.deltaTime );
}
protected void RemoveEffect()
{
m_Agent.removed -= OnRemoved;
ResetAgent();
}
void OnRemoved(DamageableBehaviour targetable)
{
m_Agent.removed -= OnRemoved;
ResetAgent();
}
void ResetAgent()
{
m_Agent.navMeshNavMeshAgent.speed = m_Agent.originalMovementSpeed;
if (m_LightBoltFx != null)
{
var particle = m_LightBoltFx.GetComponent();
if (particle)
particle.Stop();
Poolable.TryPool(m_LightBoltFx);
m_LightBoltFx.transform.localScale = m_BackScale;
m_LightBoltFx = null;
}
if( m_LightHitFx != null)
{
var particle = m_LightHitFx.GetComponent();
if (particle)
particle.Stop();
Poolable.TryPool(m_LightHitFx);
m_LightHitFx.transform.localScale = m_BackScale1;
m_LightHitFx = null;
}
Destroy(this);
}
}