River Jiang
2020-10-28 00451158ba904571b1c6c755f34292dc76e25d80
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
using System;
using System.Collections.Generic;
using ActionGameFramework.Audio;
using ActionGameFramework.Health;
using Core.Health;
using TowerDefense.Agents;
using TowerDefense.Targetting;
using TowerDefense.Towers;
using TowerDefense.Towers.Projectiles;
using UnityEngine;
using KTGMGemClient;
 
namespace TowerDefense.Affectors
{
    /// <summary>
    /// The common effect for handling firing projectiles to attack
    /// 
    /// Requires an ILauncher but it is not automatically added
    /// Add an ILauncher implementation to this GameObject before you add this script
    /// </summary>
    [RequireComponent(typeof(ILauncher))]
    public class AttackAffector : Affector, ITowerRadiusProvider
    {
        /// <summary>
        /// The projectile used to attack
        /// </summary>
        public GameObject projectile;
 
        // 
        protected GameObject projectile1;
        protected GameObject projectile2;
 
        /// <summary>
        /// The list of points to launch the projectiles from
        /// </summary>
        public Transform[] projectilePoints;
 
        /// <summary>
        /// The reference to the center point where the tower will search from
        /// </summary>
        public Transform epicenter;
 
        /// <summary>
        /// Configuration for when the tower does splash damage
        /// </summary>
        public bool isMultiAttack;
 
        /// <summary>
        /// 如果是多目标攻击,最多攻击目标
        /// </summary>
        public int maxAttackNum = 1;
 
 
        /// <summary>
        /// The fire rate in fires-per-second
        /// </summary>
        public float fireRate;
 
 
        /// <summary>
        /// 是否木属性数据
        /// </summary>
        public bool bWoodAffector = false;
 
        /// <summary>
        /// The audio source to play when firing
        /// </summary>
        public RandomAudioSource randomAudioSource;
 
        /// <summary>
        /// Gets the targetter
        /// </summary>
        public Targetter towerTargetter;
 
        /// <summary>
        /// Color of effect radius visualization
        /// </summary>
        public Color radiusEffectColor;
 
        /// <summary>
        /// Search condition
        /// </summary>
        public Filter searchCondition;
 
        /// <summary>
        /// Fire condition
        /// </summary>
        public Filter fireCondition;
 
        /// <summary>
        /// The reference to the attached launcher
        /// </summary>
        protected ILauncher m_Launcher;
 
        /// <summary>
        /// The time before firing is possible
        /// </summary>
        protected float m_FireTimer;
 
        /// <summary>
        /// Reference to the current tracked enemy
        /// </summary>
        protected Targetable m_TrackingEnemy;
 
        /// <summary>
        /// 处理装弹时间.
        /// </summary>
        protected float fillBulletTime = 0.0f;
 
        /// <summary>
        /// 充能时间
        /// </summary>
        protected float energyCalTime = 0;
        protected float fInEnergy = 0;
        protected float fBackupTimer = 0.0f;
        /// <summary>
        /// Gets the search rate from the targetter
        /// </summary>
        public float searchRate
        {
            get { return towerTargetter.searchRate; }
            set { towerTargetter.searchRate = value; }
        }
 
        /// <summary>
        /// Gets the targetable
        /// </summary>
        public Targetable trackingEnemy
        {
            get { return m_TrackingEnemy; }
        }
 
        /// <summary>
        /// Gets or sets the attack radius
        /// </summary>
        public float effectRadius
        {
            get { return towerTargetter.effectRadius; }
        }
 
        public Color effectColor 
        {
            get { return radiusEffectColor; }
        }
 
        public Targetter targetter 
        {
            get { return towerTargetter; }
        }
 
        /// <summary>
        /// Initializes the attack affector
        /// </summary>
        public override void Initialize(IAlignmentProvider affectorAlignment)
        {
            Initialize(affectorAlignment, -1);
        }
 
        /// <summary>
        /// 返回可能存在的Targetter.
        /// </summary>
        /// <returns></returns>
        public override TowerDefense.Targetting.Targetter GetTargetter()
        {
            return targetter;
        }
 
        /// <summary>
        /// Initialises the  attack affector with a layer mask
        /// </summary>
        public override void Initialize(IAlignmentProvider affectorAlignment, LayerMask mask)
        {
            base.Initialize(affectorAlignment, mask);
            SetUpTimers();
 
            towerTargetter.ResetTargetter();
            towerTargetter.alignment = affectorAlignment;
            towerTargetter.acquiredTarget += OnAcquiredTarget;
            towerTargetter.lostTarget += OnLostTarget;
        }
 
        void OnDestroy()
        {
            towerTargetter.acquiredTarget -= OnAcquiredTarget;
            towerTargetter.lostTarget -= OnLostTarget;
        }
 
        void OnLostTarget()
        {
            m_TrackingEnemy = null;
        }
 
        void OnAcquiredTarget(Targetable acquiredTarget)
        {
            m_TrackingEnemy = acquiredTarget;
        }
 
        public Damager damagerProjectile
        {
            get { return projectile == null ? null : projectile.GetComponent<Damager>(); }
        }
 
        public Damager damagerProjectile1
        {
            get { return projectile == null ? null : projectile.GetComponent<Damager>(); }
        }
 
        public Damager damagerProjectile2
        {
            get { return projectile == null ? null : projectile.GetComponent<Damager>(); }
        }
 
 
        /// <summary>
        /// Returns the total projectile damage 
        /// </summary>
        public float GetProjectileDamage()
        {
            var splash = projectile.GetComponent<SplashDamager>();
            float splashDamage = splash != null ? splash.damage : 0;
            return damagerProjectile.finalDamage + splashDamage;
        }
 
        /// <summary>
        /// Initialise the RepeatingTimer
        /// </summary>
        protected virtual void SetUpTimers()
        {
            m_FireTimer = 1 / fireRate;
            m_Launcher = GetComponent<ILauncher>();
        }
 
 
        protected void updateTowerSkillData()
        {
            // 
            // 预留出来装填子弹的时间.
            if (fillBulletTime > 0)
            {
                fillBulletTime -= Time.deltaTime;
                if (fillBulletTime <= 0.3f)
                {
                    if (towerPtr && towerPtr.bulletCtl)
                        towerPtr.bulletCtl.resetToMaxBullet();
                }
 
                if (fillBulletTime <= 0)
                {
                    fillBulletTime = 0;
                }
            }
 
            // 
            // 充能时间的处理
            if( towerPtr && towerPtr.energyCtl)
            {
                if( this.fInEnergy <= 0)
                {
                    this.energyCalTime += Time.deltaTime;
                    float process = energyCalTime % 11.0f;
                    int proint = (int)Math.Floor(process);
                    proint += towerPtr.uiProOffset;
                    towerPtr.energyCtl.SetEnergyProcessFloat( process );
                    if (proint == 10)
                    {
                        fInEnergy = 5.0f;
 
                        // 设置多倍攻击速度
                        fBackupTimer = m_FireTimer;
                        m_FireTimer = m_FireTimer / 3.0f;
 
                        towerPtr.uiProOffset = 0;
                        towerPtr.PlayEnergyEffect(true);
                    }
                }
                else
                {
                    fInEnergy -= Time.deltaTime;
                    if( fInEnergy <= 0)
                    {
                        EventCenter.Ins.BroadCast((int)KTGMGemClient.EventType.FireTowerChargeEnd);
                        fInEnergy = 0.0f;
                        this.energyCalTime = 0.0f;    
                        towerPtr.energyCtl.SetEnergyProgress(0);
 
                        // 恢复正常攻击速度 
                        m_FireTimer = fBackupTimer;
 
                        towerPtr.PlayEnergyEffect(false);
 
                    }
                }
 
            }
        }
 
        /// <summary>
        /// Update the timers
        /// </summary>
        protected virtual void Update()
        {
            // 处理当前Affector所在Tower对应的技能
            updateTowerSkillData();
 
            m_FireTimer -= Time.deltaTime;
            if( trackingEnemy == null )
                m_TrackingEnemy = targetter.GetTarget(waveLineID, bWoodAffector);
            if (trackingEnemy != null && m_FireTimer <= 0.0f)
            {
                OnFireTimer();
                m_FireTimer = 1 / fireRate;
 
                // 多倍攻速:
                if (fInEnergy > 0.0f)
                    m_FireTimer = m_FireTimer / 3.0f;
            }
        }
 
        /// <summary>
        /// Fired at every poll of the fire rate timer
        /// </summary>
        protected virtual void OnFireTimer()
        {
            if (fireCondition != null)
            {
                if (!fireCondition())
                {
                    return;
                }
            }
            FireProjectile();
        }
 
        /// <summary>
        /// Common logic when attacking
        /// 调用攻击的核心函数,由这个函数发起真正的攻击,多目标或者单目标
        /// </summary>
        protected virtual void FireProjectile()
        {
            // 不再处理多子弹攻击,确保只有一个弹道
            isMultiAttack = false; 
            m_TrackingEnemy = targetter.GetTarget( waveLineID, bWoodAffector );
            if ( (m_TrackingEnemy == null) || (fillBulletTime>0) )
            {
                if (this.towerPtr) 
                    towerPtr.setTowerState(false);
                return;
            }else
            {
                if (this.towerPtr) 
                    towerPtr.setTowerState(true);
            }
 
            // 
            // 处理子弹充能相关的内容
            if( towerPtr && (towerPtr.bulletCtl != null))
            {
                int bnum = towerPtr.bulletCtl.decBullet();
                // 
                if (bnum == 0)
                {
                    damagerProjectile.damageMulti = 2.0f;    
                    fillBulletTime = 2.0f;
                }
            }
 
            if (isMultiAttack)
            {
                List<Targetable> enemies = towerTargetter.GetAllTargets();
                if( (enemies != null)&&(Targetter.bSearchTarget) )
                    m_Launcher.Launch(enemies, projectile, projectilePoints,this.maxAttackNum);
            }
            else
            {
                if(Targetter.bSearchTarget )
                    m_Launcher.Launch(m_TrackingEnemy, damagerProjectile.gameObject, projectilePoints);
            }
            if (randomAudioSource != null)
            {
                if( Targetter.bSearchTarget )
                    randomAudioSource.PlayRandomClip();
            }
        }
 
        /// <summary>
        /// A delegate to compare distances of components
        /// </summary>
        /// <param name="first"></param>
        /// <param name="second"></param>
        protected virtual int ByDistance(Targetable first, Targetable second)
        {
            float firstSqrMagnitude = Vector3.SqrMagnitude(first.position - epicenter.position);
            float secondSqrMagnitude = Vector3.SqrMagnitude(second.position - epicenter.position);
            return firstSqrMagnitude.CompareTo(secondSqrMagnitude);
        }
 
#if UNITY_EDITOR
        /// <summary>
        /// Draws the search area
        /// </summary>
        void OnDrawGizmosSelected()
        {
            Gizmos.DrawWireSphere(epicenter.position, towerTargetter.effectRadius);
        }
#endif
    }
 
    /// <summary>
    /// A delegate for boolean calculation logic
    /// </summary>
    public delegate bool Filter();
}