chenxin
2020-10-24 2fc3753ac9c591a01f0d966984abe659113fe57f
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
using ActionGameFramework.Health;
using Core.Health;
using UnityEngine;
 
namespace TowerDefense.Towers.Projectiles
{
    /// <summary>
    /// Component that will apply splash damage on collision enter
    /// </summary>
    public class SplashDamager : MonoBehaviour
    {
        /// <summary>
        /// The Area this projectile will attack in
        /// </summary>
        public float attackRange = 0.6f;
 
        /// <summary>
        /// The amount of damage done, a percentage of the damager damage
        /// </summary>
        public float damageAmount;
 
        /// <summary>
        /// The physics layer mask to search on
        /// </summary>
        public LayerMask mask = -1;
 
        /// <summary>
        /// The alignment of the projectile
        /// </summary>
        public SerializableIAlignmentProvider alignment;
 
        static readonly Collider[] s_Enemies = new Collider[64];
 
        public float damage
        {
            get { return damageAmount; }
        }
 
        /// <summary>
        /// Gets this damager's alignment
        /// </summary>
        public IAlignmentProvider alignmentProvider
        {
            get { return alignment != null ? alignment.GetInterface() : null; }
        }
 
        /// <summary>
        /// Searches for Targetables within a radius of <see cref="attackRange"/>
        /// and damages them if valid
        /// </summary>
        protected virtual void OnCollisionEnter(Collision other)
        {
            int number = Physics.OverlapSphereNonAlloc(transform.position, attackRange, s_Enemies, mask);
            for (int index = 0; index < number; index++)
            {
                Collider enemy = s_Enemies[index];
                var damageable = enemy.GetComponent<Targetable>();
                if (damageable == null)
                {
                    continue;
                }
                damageable.TakeDamage(damageAmount, damageable.position, alignmentProvider);
            }
        }
 
        /// <summary>
        /// 看看Trigger的加入能不能有助于处理:
        /// </summary>
        /// <param name="other"></param>
        protected void OnTriggerEnter(Collider other)
        {
            int number = Physics.OverlapSphereNonAlloc(transform.position, attackRange, s_Enemies, mask);
            for (int index = 0; index < number; index++)
            {
                Collider enemy = s_Enemies[index];
                var damageable = enemy.GetComponent<Targetable>();
                if (damageable == null)
                {
                    continue;
                }
                damageable.TakeDamage(damageAmount, damageable.position, alignmentProvider);
            }
        }
    }
}