chenxin
2020-10-27 49f88e6493466a1723dd6b3967ff4c70f723db5d
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
using System.Collections.Generic;
using Core.Health;
using Core.Utilities;
using UnityEngine;
 
namespace TowerDefense.Agents
{
    /// <summary>
    /// This effect will get attached to an agent that is within range of the SlowAffector radius
    /// </summary>
    public class AgentSlower : AgentEffect
    {
        protected GameObject m_SlowFx;
 
        /// <summary>
        /// 列表的原因是这个效果是可以叠加的,多个效果叠加在一起必须得使用列表的形式来处理。
        /// </summary>
        protected List<float> m_CurrentEffects = new List<float>();
 
        /// <summary>
        /// Initializes the slower with the parameters configured in the SlowAffector
        /// </summary>
        /// <param name="slowFactor">Normalized float that represents the % slowdown applied to the agent</param>
        /// <param name="slowfxPrefab">The instantiated object to visualize the slow effect</param>
        /// <param name="position"></param>
        /// <param name="scale"></param>
        public void Initialize(float slowFactor, GameObject slowfxPrefab = null, 
                               Vector3 position = default(Vector3),
                               float scale = 1)
        {
            LazyLoad();
            m_CurrentEffects.Add(slowFactor);
 
            // find greatest slow effect
            float min = slowFactor;
            foreach (float item in m_CurrentEffects)
            {
                min = Mathf.Min(min, item);
            }
            
            float originalSpeed = m_Agent.originalMovementSpeed;
            float newSpeed = originalSpeed * min;
            m_Agent.navMeshNavMeshAgent.speed = newSpeed;
 
            if (m_SlowFx == null && slowfxPrefab != null)
            {
                m_SlowFx = Poolable.TryGetPoolable(slowfxPrefab);
                m_SlowFx.transform.parent = transform;
                m_SlowFx.transform.localPosition = position;
                m_SlowFx.transform.localScale *= scale;
            }
            m_Agent.removed += OnRemoved;
        }
 
        /// <summary>
        /// Resets the agent's speed 
        /// </summary>
        public void RemoveSlow(float slowFactor)
        {
            m_Agent.removed -= OnRemoved;
            
            m_CurrentEffects.Remove(slowFactor);
            if (m_CurrentEffects.Count != 0)
            {
                return;
            }
            
            // No more slow effects
            ResetAgent();
        }
 
        /// <summary>
        /// Agent has died, remove affect
        /// </summary>
        void OnRemoved(DamageableBehaviour targetable)
        {
            m_Agent.removed -= OnRemoved;
            ResetAgent();
        }
 
        void ResetAgent()
        {
            if (m_Agent != null)
            {
                m_Agent.navMeshNavMeshAgent.speed = m_Agent.originalMovementSpeed;
            }
            if (m_SlowFx != null)
            {
                Poolable.TryPool(m_SlowFx);
                m_SlowFx.transform.localScale = Vector3.one;
                m_SlowFx = null;
            }
            Destroy(this);
        }
    }
}