liuzhiwei
2020-12-26 56d95fa59a9f6dc12d0fc0723ea449e5b317f994
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
using UnityEngine;
using TowerDefense.Towers.Projectiles;
using TowerDefense.Agents;
using ActionGameFramework.Projectiles;
 
namespace ActionGameFramework.Health
{
    /// <summary>
    /// Damage collider - a collider based implementation of DamageZone
    /// </summary>
    [RequireComponent(typeof(Collider))]
    public class DamageCollider : DamageZone
    {
        /// <summary>
        /// On collision enter, see if the colliding object has a Damager and then make the damageableBehaviour take damage
        /// </summary>
        /// <param name="c">The collider</param>
        protected void OnCollisionEnter(Collision c)
        {
            var damager = c.gameObject.GetComponent<Damager>();
            if (damager == null)
            {
                return;
            }
 
            // 非Agent不参与碰撞:
            var agent = this.gameObject.GetComponent<Agent>();
            if (agent == null)
                return;
 
            LazyLoad();
 
            BallisticAttack ballisticAttack = damager.GetComponent<BallisticAttack>();
            Vector3 collisionPosition = ConvertContactsToPosition(c.contacts);
            ballisticAttack.DealDamage((Targetable)damageableBehaviour);
 
            damager.HasDamaged(collisionPosition, damageableBehaviour.configuration.alignmentProvider);
        }
 
        protected void OnTriggerEnter(Collider c)
        {
            var damager = c.gameObject.GetComponent<Damager>();
            if (damager == null)
            {
                return;
            }
 
            // 非Projectile不参与此次的碰撞
            var projec = c.gameObject.GetComponent<IProjectile>();
            if (projec == null)
                return;
 
            // 非Agent不参与碰撞:
            var agent = this.gameObject.GetComponent<Agent>();
            if (agent == null || agent.isDead)
                return;
 
            BallisticProjectile ballistic = c.gameObject.GetComponent<BallisticProjectile>();
 
            LazyLoad();
 
            BallisticAttack ballisticAttack = damager.GetComponent<BallisticAttack>();
            ballisticAttack.DealDamage((Targetable)damageableBehaviour, damager);
 
            damager.HasDamaged(c.transform.position, damageableBehaviour.configuration.alignmentProvider);
        }
 
        /// <summary>
        /// Averages the contacts to get the position.
        /// </summary>
        /// <returns>The average position.</returns>
        /// <param name="contacts">Contacts.</param>
        protected Vector3 ConvertContactsToPosition(ContactPoint[] contacts)
        {
            Vector3 output = Vector3.zero;
            int length = contacts.Length;
 
            if (length == 0)
            {
                return output;
            }
 
            for (int i = 0; i < length; i++)
            {
                output += contacts[i].point;
            }
 
            output = output / length;
            return output;
        }
    }
}