wangguan
2020-12-14 6f40c69336d4dc61567d58880a2bb4952d69be95
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
using ActionGameFramework.Health;
using TowerDefense.Agents;
using UnityEngine;
 
namespace TowerDefense.Affectors
{
    /// <summary>
    /// Uses a trigger to attach and remove <see cref="AgentSlower" /> components to agents
    /// </summary>
    public class SlowAffector : PassiveAffector
    {
        /// <summary>
        /// A normalized value to slow agents by
        /// </summary>
        [Range(0, 1)]
        public float slowFactor;
 
        /// <summary>
        /// The slow factor for displaying to the UI
        /// </summary>
        public string slowFactorFormat = "<b>Slow Factor:</b> {0}";
 
        /// <summary>
        /// The particle system that plays when an entity enters the sphere
        /// </summary>
        public ParticleSystem enterParticleSystem;
 
        public GameObject slowFxPrefab;
 
        /// <summary>
        /// The audio source that plays when an entity enters the sphere
        /// </summary>
        public AudioSource audioSource;
 
        /// <summary>
        /// Subsribes to the relevant targetter events
        /// </summary>
        protected void Awake()
        {
            towerTargetter.targetEntersRange += OnTargetEntersRange;
            towerTargetter.targetExitsRange += OnTargetExitsRange;
        }
 
        /// <summary>
        /// Unsubsribes from the relevant targetter events
        /// </summary>
        void OnDestroy()
        {
            towerTargetter.targetEntersRange -= OnTargetEntersRange;
            towerTargetter.targetExitsRange -= OnTargetExitsRange;
        }
 
        /// <summary>
        /// Attaches a <see cref="AgentSlower" /> to the agent
        /// </summary>
        /// <param name="target">The agent to attach the slower to</param>
        protected void AttachSlowComponent(Agent target)
        {
            var slower = target.GetComponent<AgentSlower>();
            if (slower == null)
            {
                slower = target.gameObject.AddComponent<AgentSlower>();
            }
            slower.Initialize(slowFactor, slowFxPrefab, target.appliedEffectOffset, target.appliedEffectScale);
 
            if (enterParticleSystem != null)
            {
                enterParticleSystem.Play();
            }
            if (audioSource != null)
            {
                audioSource.Play();
            }
        }
 
        /// <summary>
        /// Removes the <see cref="AgentSlower" /> from the agent once it leaves the area
        /// </summary>
        /// <param name="target">The agent to remove the slower from</param>
        protected void RemoveSlowComponent(Agent target)
        {
            if (target == null)
            {
                return;
            }
            var slowComponent = target.gameObject.GetComponent<AgentSlower>();
            if (slowComponent != null)
            {
                slowComponent.RemoveSlow(slowFactor);
            }
        }
 
        /// <summary>
        /// Fired when the targetter aquires a new targetable
        /// </summary>
        protected void OnTargetEntersRange(Targetable other)
        {
            var agent = other as Agent;
            if (agent == null)
            {
                return;
            }
            AttachSlowComponent(agent);
        }
 
        /// <summary>
        /// Fired when the targetter aquires loses a targetable
        /// </summary>
        protected void OnTargetExitsRange(Targetable other)
        {
            var searchable = other as Agent;
            if (searchable == null)
            {
                return;
            }
            RemoveSlowComponent(searchable);
        }
    }
}