chenxin
2020-11-26 9af3e3dbede79e70262ff9d206299dcca363c1bb
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
using Core.Health;
using TowerDefense.Affectors;
using TowerDefense.Towers;
using UnityEngine;
 
namespace TowerDefense.Agents
{
    /// <summary>
    /// An implementation of Agent that will attack 
    /// any Towers that block its path
    /// </summary>
    public class AttackingAgent : Agent
    {
        /// <summary>
        /// Tower to target
        /// </summary>
        protected Tower m_TargetTower;
 
        /// <summary>
        /// The attached attack affector
        /// </summary>
        protected AttackAffector m_AttackAffector;
        
        /// <summary>
        /// Is this agent currently engaging a tower?
        /// </summary>
        protected bool m_IsAttacking;
 
        public override void Initialize()
        {
            base.Initialize();
            
            // Attack affector
            m_AttackAffector.Initialize(configuration.alignmentProvider);
            
            // We don't want agents to attack towers until their path is blocked, 
            // so disable m_AttackAffector until it is needed
            m_AttackAffector.enabled = false;
        }
 
        /// <summary>
        /// Unsubscribes from tracked towers removed event
        /// and disables the attached attack affector
        /// </summary>
        public override void Remove()
        {
            base.Remove();
            if (m_TargetTower != null)
            {
                m_TargetTower.removed -= OnTargetTowerDestroyed;
            }
            m_AttackAffector.enabled = false;
            m_TargetTower = null;
        }
 
        /// <summary>
        /// Gets the closest tower to the agent
        /// </summary>
        /// <returns>The closest tower</returns>
        protected Tower GetClosestTower()
        {
            var towerController = m_AttackAffector.towerTargetter.GetTarget( -1 ) as Tower;
            return towerController;
        }
 
        /// <summary>
        /// Caches the Attack Affector if necessary
        /// </summary>
        protected override void LazyLoad()
        {
            base.LazyLoad();
            if (m_AttackAffector == null)
            {
                m_AttackAffector = GetComponent<AttackAffector>();
            }
        }
        
        /// <summary>
        /// If the tower is destroyed while other agents attack it, ensure it becomes null
        /// </summary>
        /// <param name="tower">The tower that has been destroyed</param>
        protected virtual void OnTargetTowerDestroyed(DamageableBehaviour tower)
        {
            if (m_TargetTower == tower)
            {
                m_TargetTower.removed -= OnTargetTowerDestroyed;
                m_TargetTower = null;
            }
        }
        
        /// <summary>
        /// Peforms the relevant path update for the states <see cref="Agent.State.OnCompletePath"/>, 
        /// <see cref="Agent.State.OnPartialPath"/> and <see cref="Agent.State.Attacking"/>
        /// </summary>
        protected override void PathUpdate()
        {
            switch (state)
            {
                case State.OnCompletePath:
                    OnCompletePathUpdate();
                    break;
                case State.OnPartialPath:
                    OnPartialPathUpdate();
                    break;
                case State.Attacking:
                    AttackingUpdate();
                    break;
            }
        }
        
        /// <summary>
        /// Change to <see cref="Agent.State.OnCompletePath" /> when path is no longer blocked or to
        /// <see cref="Agent.State.Attacking" /> when the agent reaches <see cref="AttackingAgent.m_TargetTower" />
        /// </summary>
        protected override void OnPartialPathUpdate()
        {
            if (!isPathBlocked)
            {
                state = State.OnCompletePath;
                return;
            }
 
            // Check for closest tower at the end of the partial path
            m_AttackAffector.towerTargetter.transform.position = m_NavMeshAgent.pathEndPosition;
            Tower tower = GetClosestTower();
            if (tower != m_TargetTower)
            {
                // if the current target is to be replaced, unsubscribe from removed event
                if (m_TargetTower != null)
                {
                    m_TargetTower.removed -= OnTargetTowerDestroyed;
                }
                
                // assign target, can be null
                m_TargetTower = tower;
                
                // if new target found subscribe to removed event
                if (m_TargetTower != null)
                {
                    m_TargetTower.removed += OnTargetTowerDestroyed;
                }
            }
            if (m_TargetTower == null)
            {
                return;
            }
            float distanceToTower = Vector3.Distance(transform.position, m_TargetTower.transform.position);
            if (!(distanceToTower < m_AttackAffector.towerTargetter.effectRadius))
            {
                return;
            }
            if (!m_AttackAffector.enabled)
            {
                m_AttackAffector.towerTargetter.transform.position = transform.position;
                m_AttackAffector.enabled = true;
            }
            state = State.Attacking;
            m_NavMeshAgent.isStopped = true;
        }
        
        /// <summary>
        /// The agent attacks until the path is available again or it has killed the target tower
        /// </summary>
        protected void AttackingUpdate()
        {
            if (m_TargetTower != null)
            {
                return;
            }
            // River mod: 修改了Agent的移动方式,这个函数的模式已修改.
            //MoveToNode();
 
            // Resume path once blocking has been cleared
            m_IsAttacking = false;
            m_NavMeshAgent.isStopped = false;
            m_AttackAffector.enabled = false;
            state = isPathBlocked ? State.OnPartialPath : State.OnCompletePath;
            // Move the Targetter back to the agent's position
            m_AttackAffector.towerTargetter.transform.position = transform.position;
        }
    }
}