using Core.Utilities;
|
using TowerDefense.Agents;
|
using UnityEngine;
|
|
namespace TowerDefense.Towers.Projectiles
|
{
|
/// <summary>
|
/// For objects that destroyer themselves on contact
|
/// </summary>
|
public class ContactDestroyer : MonoBehaviour
|
{
|
/// <summary>
|
/// The y-value of the position the object will destroy itself
|
/// </summary>
|
public float yDestroyPoint = -50;
|
|
/// <summary>
|
/// The attached collider
|
/// </summary>
|
protected Collider m_AttachedCollider;
|
|
/// <summary>
|
/// Caches the attached collider
|
/// </summary>
|
protected virtual void Awake()
|
{
|
m_AttachedCollider = GetComponent<Collider>();
|
}
|
|
/// <summary>
|
/// Checks the y-position against <see cref="yDestroyPoint"/>
|
/// </summary>
|
protected virtual void Update()
|
{
|
if (transform.position.y < yDestroyPoint)
|
{
|
ReturnToPool();
|
}
|
}
|
|
void OnCollisionEnter(Collision other)
|
{
|
ReturnToPool();
|
}
|
|
/// <summary>
|
/// 这是一个替代的方案,必须得找到核心的原因,并解决这个问题:
|
/// </summary>
|
/// <param name="other"></param>
|
protected void OnTriggerEnter(Collider other)
|
{
|
Agent ag = other.gameObject.GetComponent<Agent>();
|
if (ag != null)
|
Destroy(gameObject);
|
// ReturnToPool();
|
return;
|
// if ((other.name == "BoxAgent(Clone)") || (other.name == "Hoverbuggy(Clone)")
|
// || (other.name == "HoverbuggyElite(Clone)"))
|
// {
|
// var damage = gameObject.GetComponent<Damager>();
|
// var damageable = other.GetComponent<Targetable>();
|
// 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();
|
// }
|
}
|
/// <summary>
|
/// Returns the object to pool if possible, otherwise destroys
|
/// </summary>
|
void ReturnToPool()
|
{
|
if (!gameObject.activeInHierarchy) return;
|
|
ParticleSystem ps = gameObject.GetComponent<ParticleSystem>();
|
if (ps == null)
|
ps = gameObject.transform.GetChild(0).GetComponent<ParticleSystem>();
|
if (ps == null)
|
ps = transform.GetChild(0).GetChild(0).GetComponent<ParticleSystem>();
|
ps?.Stop();
|
Poolable.TryPool(gameObject);
|
}
|
}
|
}
|