wangguan
2020-12-08 3d13f2e8a23602aedb8adf5d2d02e377a330a61b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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)
                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);
        }
    }
}