using Core.Utilities;
using TowerDefense.Agents;
using UnityEngine;
namespace TowerDefense.Towers.Projectiles
{
///
/// For objects that destroyer themselves on contact
///
public class ContactDestroyer : MonoBehaviour
{
///
/// The y-value of the position the object will destroy itself
///
public float yDestroyPoint = -50;
///
/// The attached collider
///
protected Collider m_AttachedCollider;
///
/// Caches the attached collider
///
protected virtual void Awake()
{
m_AttachedCollider = GetComponent();
}
///
/// Checks the y-position against
///
protected virtual void Update()
{
if (transform.position.y < yDestroyPoint)
{
ReturnToPool();
}
}
void OnCollisionEnter(Collision other)
{
ReturnToPool();
}
///
/// 这是一个替代的方案,必须得找到核心的原因,并解决这个问题:
///
///
protected void OnTriggerEnter(Collider other)
{
Agent ag = other.gameObject.GetComponent();
if (ag != null)
ReturnToPool();
return;
// if ((other.name == "BoxAgent(Clone)") || (other.name == "Hoverbuggy(Clone)")
// || (other.name == "HoverbuggyElite(Clone)"))
// {
// var damage = gameObject.GetComponent();
// var damageable = other.GetComponent();
// if (damageable == null)
// return;
// //Debug.Log("破坏者名字:" + this.gameObject.name +
// // "破坏数据是:" + damage.damage + "," + damage.finalDamage + "," + damage.tuid );
// float finalDamage = damage.finalDamage;
// bool crit = damage.isCrit;
// if (crit)
// {
// finalDamage += finalDamage;
// //damageable.transform.DOShakePosition(0.5f);
// }
// int tid = damageable.liveID;
// Vector3 backPos = damageable.position;
// damageable.TakeDamage(finalDamage, damageable.position, null);
// // 处理血量飘字的效果:
// //if( damageable.liveID == tid )
// GameUI.instance.generateBloodText(backPos, finalDamage, crit);
// ReturnToPool();
// }
}
///
/// Returns the object to pool if possible, otherwise destroys
///
void ReturnToPool()
{
if (!gameObject.activeInHierarchy) return;
ParticleSystem ps = gameObject.GetComponent();
if (ps == null)
ps = gameObject.transform.GetChild(0).GetComponent();
ps?.Stop();
Poolable.TryPool(gameObject);
}
}
}