using ActionGameFramework.Health;
using Core.Utilities;
using DG.Tweening;
using TMPro.Examples;
using TowerDefense.Affectors;
using TowerDefense.Agents;
using TowerDefense.Towers.Projectiles;
using UnityEngine;
using UnityEngine.UIElements;
namespace TowerDefense.Towers.TowerLaunchers
{
///
/// An implementation of the tower launcher for hitscan attacks
///
public class HitscanLauncher : Launcher
{
///
/// The particle system used for providing launch feedback
///
public GameObject fireParticleObj;
public ParticleSystem fireParticleSystem1;
public ParticleSystem fireParticleSystem2;
protected Timer effectStopTimer;
protected bool bTimerStart = false;
void Awake()
{
effectStopTimer = new Timer(1.0f, stopParticle);
bTimerStart = false;
}
protected void stopParticle()
{
if (particleToPlay)
particleToPlay.Stop(true, ParticleSystemStopBehavior.StopEmittingAndClear);
}
void Update()
{
if (bTimerStart)
{
if (effectStopTimer.Tick(Time.deltaTime))
{
bTimerStart = false;
effectStopTimer.Reset();
}
}
}
protected int times = 0;
///
/// Assigns the correct damage to the hitscan object and
/// attacks the enemy immediately.
/// Early return if there is not HitscanAttack.cs attached to the attact object
///
///
/// The enemy this tower is targeting
///
///
/// The attacking component used to damage the enemy
///
///
public override void Launch(Targetable enemy, GameObject attack, Transform firingPoint, int idx = 0)
{
var hitscanAttack = attack.GetComponent();
if (hitscanAttack == null)
{
return;
}
/* if(times != 0)
{
times++;
if (times >= 5)
times = 0;
return;
}
else
{
times++;
}
*/
// 是否有AttackAffector,获取AttackRate,计算最终的伤害延迟
AttackAffector aaf = GetComponent();
float delayAttack = 0.0f;
if (aaf)
{
float attackRate = aaf.FireRate;
if (attackRate > 0)
delayAttack = 1.0f / attackRate;
}
TowerLevel tl = transform.parent.gameObject.GetComponent();
hitscanAttack.attackRise = 0.0f;
if (tl != null)
hitscanAttack.attackRise = tl.attackRise;
hitscanAttack.transform.position = firingPoint.position;
hitscanAttack.AttackEnemy(firingPoint.position, enemy, delayAttack);
if (idx == 0)
{
// GameObject obj = Poolable.TryGetPoolable(fireParticleObj);
// obj.transform.position = firingPoint.position;
// obj.transform.LookAt(enemy.position);
// ParticleSystem ps = obj.GetComponent();
// ps = ps ?? obj.transform.GetChild(0).GetComponent();
// ps.Play();
GameObject obj = Instantiate(fireParticleObj, firingPoint.position, Quaternion.identity);
obj.transform.LookAt(enemy.position);
ParticleSystem ps = obj.GetComponent();
if (ps == null)
ps = obj.transform.GetChild(0).GetComponent();
ps?.Play();
}
else if (idx == 1)
{
PlayParticles(fireParticleSystem1, firingPoint.position, enemy.position);
}
else
{
PlayParticles(fireParticleSystem2, firingPoint.position, enemy.position);
}
effectStopTimer.SetTime(delayAttack / 5.0f);
bTimerStart = true;
}
}
}