chenxin
2020-11-17 40b918d5189a154bc0ff31627e69867742934720
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
using KTGMGemClient;
using System.Collections;
using System;
using ActionGameFramework.Health;
using Core.Utilities;
using TowerDefense.Level;
using TowerDefense.Targetting;
using TowerDefense.Towers.Placement;
using TowerDefense.UI.HUD;
using UnityEngine;
 
namespace TowerDefense.Towers
{
    public enum EFeatureTower
    {
        NULL,
        GrowUp,
        CopyCat,
        LightTower,
        Skill_Fire,
        Skill_Bomb,
        END
    }
 
    public enum ETowerFuntion
    {
        NULL,
        BULLET,   // 子弹塔
        ENERGY,   // 能量充能.
        FREEZE, // 水精灵充能
        END
    }
 
    /// <summary>
    /// Common functionality for all types of towers
    /// </summary>
    public class Tower : Targetable
    {
        public readonly float INSCENE_TU_DAMAGE = 30f;
        public static readonly int MAX_LEVEL = 4;
 
        /// <summary>
        /// A generalised name common to a levels
        /// </summary>
        public string towerName;
 
        /// <summary>
        /// The size of the tower's footprint
        /// </summary>
        public IntVector2 dimensions;
 
        /// <summary>
        /// 是否是特色骰子,如果是,则选择一个Enum类型。
        /// </summary>
        public EFeatureTower towerFeature = EFeatureTower.NULL;
 
        /// <summary>
        /// 当前塔防对应的MonsterMaterial,自身和对方显示不同的Mat.
        /// </summary>
        public Material materialMonsterSelf;
        public Material materialMonsterOppo;
 
        /// <summary>
        /// 当前塔防对应的攻击和等待Mat.
        /// </summary>
        public Material materialTowerAttackSelf;
        public Material materialTowerWaitSelf;
        public Material materialTowerAttackOppo;
        public Material materialTowerWaitOppo;
 
        /// <summary>
        /// 0 空状态  1 等待状态  2 攻击状态.
        /// </summary>
        protected int curActionState = 0;
 
        /// <summary>
        /// 塔防对应的充能状态.
        /// </summary>
        public ETowerFuntion eTowerFuntion = ETowerFuntion.NULL;
        public BulletUICtl bulletCtl = null;
        public EnergyUICtl energyCtl = null;
 
        public FreezeBreath FreezeBreathCtrl = null;
 
        /// <summary>
        /// The tower levels associated with this tower
        /// </summary>
        public TowerLevel[] levels;
 
        /// <summary>
        /// 当前塔防对应的AttributeID.
        /// </summary>
        public int attributeId;
 
        /// <summary>
        /// 当前的Tower在游戏内UI界面上用到的Image数据,再以后应该是直接在表格内配置才合适
        /// </summary>
        public Sprite uiImage;
 
        /// <summary>
        /// The physics mask the tower searches on
        /// </summary>
        public LayerMask enemyLayerMask;
 
        /// <summary>
        /// The current level of the tower
        /// </summary>
        public int currentLevel { get; protected set; }
 
        protected bool m_bInAttackMode = false;
 
        /// <summary>
        /// 精英怪和Boss双倍伤害.
        /// </summary>
        public bool bDoubleHitElit = false;
 
        /// <summary>
        /// Reference to the data of the current level
        /// </summary>
        public TowerLevel currentTowerLevel { get; protected set; }
 
        /// <summary>
        /// 攻击增加
        /// </summary>
        public float attackRise { get; set; }
 
        /// <summary>
        /// 用于界面部分的数据位移
        /// </summary>
        protected int progressOffset = 0;
 
        public int FreezeBreathProgressOffset { get; set; }
 
        public bool PlayWaveLineFlash { get; set; } = true;
 
        /// <summary>
        /// 塔防数据的局内升级
        /// </summary>
        public int inSceneTowerLevel
        {
            get; set;
        }
 
        public int uiProOffset
        {
            get { return progressOffset; }
            set { progressOffset = value; }
        }
 
        /// <summary>
        /// Gets whether the tower can level up anymore
        /// </summary>
        public bool isAtMaxLevel
        {
            get { return currentLevel == levels.Length - 1; }
        }
 
        public void setTowerState(bool attack)
        {
            if (!materialTowerAttackSelf) return;
 
            if (opponentSide)
            {
                if (attack && (curActionState != 2))
                {
                    currentTowerLevel.SetTowerMonsterMat(materialTowerAttackOppo, true);
                    curActionState = 2;
                }
 
                if ((!attack) && (curActionState != 1))
                {
                    currentTowerLevel.SetTowerMonsterMat(materialTowerWaitOppo, false);
                    curActionState = 1;
                }
            }
            else
            {
                if (attack && (curActionState != 2))
                {
                    currentTowerLevel.SetTowerMonsterMat(materialTowerAttackSelf, true);
                    curActionState = 2;
                }
 
                if ((!attack) && (curActionState != 1))
                {
                    currentTowerLevel.SetTowerMonsterMat(materialTowerWaitSelf, false);
                    curActionState = 1;
                }
            }
        }
 
        /// <summary>
        /// 是否是对手塔防
        /// </summary>
        public bool opponentSide { get; set; }
 
        /// <summary>
        /// 播放充能状态特效.
        /// </summary>
        /// <param name="play"></param>
        public void PlayEnergyEffect(bool play, bool isClose = true)
        {
            if (energyCtl && isClose)
                energyCtl.gameObject.SetActive(!play);
            else if (!isClose)
            {
 
            }
 
            if (!opponentSide)
            {
                if (GameUI.instanceExists)
                    ((TowerPlacementGrid)GameUI.instance.selfTowerPlaceArea).PlayEnergyEffect(gridPosition.x, play);
                else if (EndlessGameUI.instanceExists)
                    ((TowerPlacementGridEndless)EndlessGameUI.instance.selfTowerPlaceArea).PlayEnergyEffect(gridPosition.x, gridPosition.y, play);
            }
            else
                ((TowerPlacementGrid)OpponentMgr.instance.m_CurrentArea).PlayEnergyEffect(gridPosition.x, play);
        }
 
        /// <summary>
        /// 播放水精灵充能满了特效
        /// </summary>
        /// <param name="play"></param>
        /// <param name="isClose"></param>
        public void PlayFreezeBreathEffect(bool play, bool isClose = true)
        {
            if (FreezeBreathCtrl && isClose)
                FreezeBreathCtrl.gameObject.SetActive(!play);
 
            if (!opponentSide)
            {
                if (EndlessGameUI.instanceExists)
                    ((TowerPlacementGridEndless)EndlessGameUI.instance.selfTowerPlaceArea).PlayFreezeBreathEffect(gridPosition.x, gridPosition.y, play);
            }
        }
 
        /// <summary>
        /// 当前是否处于攻击模式
        /// </summary>
        public bool bInAttackMode
        {
            get { return m_bInAttackMode; }
 
            // 设置是否处于攻击状态:
            set
            {
                m_bInAttackMode = value;
                currentTowerLevel.SetAffectorState(m_bInAttackMode, gridPosition.x);
                currentTowerLevel.SetAttackState(value);
 
                if (value)
                {
                    if (opponentSide)
                    {
                        OpponentMgr.instance.SetTowerAttID(gridPosition.x, attributeId, currentLevel);
                    }
                    else
                    {
                        if (LevelManager.instanceExists)
                        {
                            LevelManager.instance.SetTowerAttID(gridPosition.x, attributeId, currentLevel);
                            WaveLineSelMgr.instance.attackTowerFixed(gridPosition.x);
                        }
                        else if (EndlessLevelManager.instanceExists)
                        {
                            EndlessLevelManager.instance.SetTowerAttID(gridPosition.x, attributeId, currentLevel);
                            EndlessWaveLineManager.instance.AttackTowerFixed(gridPosition.x, PlayWaveLineFlash);
                        }
                    }
 
                    // 处理塔位边上的界面.
                    OnTowerUICtrl();
                }
            }
        }
 
        /// <summary>
        /// 去掉当前Tower对应的界面数据.
        /// </summary>
        public void DisableTowerUICtrl()
        {
            // 根据是否是子弹塔防来决定是否显示相应的界面
            BulletUICtl buc = placementArea.GetBulletUICtl(gridPosition.x, gridPosition.y);
 
            if ((eTowerFuntion == ETowerFuntion.BULLET) && (buc != null))
            {
                buc.gameObject.SetActive(false);
                bulletCtl = null;
            }
 
            EnergyUICtl euc = placementArea.GetEnergyUICtl(gridPosition.x, gridPosition.y);
            if ((eTowerFuntion == ETowerFuntion.ENERGY) && (euc != null))
            {
                // 设置数据
                euc.gameObject.SetActive(false);
                energyCtl = null;
            }
 
            FreezeBreath ctrl = placementArea.GetFreezeBreath(gridPosition.x, gridPosition.y);
            if (eTowerFuntion == ETowerFuntion.FREEZE && ctrl != null)
            {
                ctrl.gameObject.SetActive(false);
                ctrl = null;
            }
        }
 
        public int GetTowerUICtrlProgress()
        {
            // 根据是否是子弹塔防来决定是否显示相应的界面
            BulletUICtl buc = placementArea.GetBulletUICtl(gridPosition.x, gridPosition.y);
 
            if ((eTowerFuntion == ETowerFuntion.BULLET) && (buc != null))
                return buc.GetCtlProgress();
 
            EnergyUICtl euc = placementArea.GetEnergyUICtl(gridPosition.x, gridPosition.y);
            if ((eTowerFuntion == ETowerFuntion.ENERGY) && (euc != null))
                return euc.GetCtlProgress();
            
            FreezeBreath ctrl = placementArea.GetFreezeBreath(gridPosition.x, gridPosition.y);
 
            if (eTowerFuntion == ETowerFuntion.FREEZE && ctrl != null)
                return ctrl.CurrentProgress;
 
            return 0;
        }
 
        public void SetTowerUICtlProcess(int pro)
        {
            // 根据是否是子弹塔防来决定是否显示相应的界面
            BulletUICtl buc = placementArea.GetBulletUICtl(gridPosition.x, gridPosition.y);
 
            if ((eTowerFuntion == ETowerFuntion.BULLET) && (buc != null))
            {
                buc.SetCtlProcess(pro);
                progressOffset = pro;
                return;
            }
 
            EnergyUICtl euc = placementArea.GetEnergyUICtl(gridPosition.x, gridPosition.y);
            if ((eTowerFuntion == ETowerFuntion.ENERGY) && (euc != null))
            {
                euc.SetCtlProcess(pro);
                progressOffset = pro;
                if (progressOffset == 10)
                    progressOffset = 0;
                return;
            }
 
            FreezeBreath ctrl = placementArea.GetFreezeBreath(gridPosition.x, gridPosition.y);
 
            if (eTowerFuntion == ETowerFuntion.FREEZE && ctrl != null)
            {
                ctrl.SetCtrlProgress(pro);
                FreezeBreathProgressOffset = pro;
                if (FreezeBreathProgressOffset == 10)
                    FreezeBreathProgressOffset = 0;
            }
        }
 
        /// <summary>
        /// 充能技能相关的代码开关。包括子弹充能和时间充能
        /// </summary>
        protected void OnTowerUICtrl()
        {
            HandleBulletUICtrl();
            HandleEnergyUICtrl();
            HandleFreezeBreathCtrl();
        }
 
        private void HandleBulletUICtrl()
        {
            // 根据是否是子弹塔防来决定是否显示相应的界面
            BulletUICtl buc = placementArea.GetBulletUICtl(gridPosition.x, gridPosition.y);
 
            if (buc == null) return;
 
            if ((eTowerFuntion == ETowerFuntion.BULLET) && (buc != null))
            {
                // 设置数据
                buc.gameObject.SetActive(true);
                bulletCtl = buc;
                buc.ResetToMaxBullet();
            }
            else
            {
                // 清空数据
                buc.gameObject.SetActive(false);
                bulletCtl = null;
            }
        }
 
        private void HandleEnergyUICtrl()
        {
            // 根据是否是能量充能来决定是否显示相应的界面.
            EnergyUICtl euc = placementArea.GetEnergyUICtl(gridPosition.x, gridPosition.y);
            if ((eTowerFuntion == ETowerFuntion.ENERGY) && (euc != null))
            {
                // 设置数据
                euc.gameObject.SetActive(true);
                energyCtl = euc;
                euc.SetEnergyProgress(0);
            }
            else
            {
                // 清空数据
                euc.gameObject.SetActive(false);
                energyCtl = null;
            }
        }
 
        private void HandleFreezeBreathCtrl()
        {
            FreezeBreath ctrl = placementArea.GetFreezeBreath(gridPosition.x, gridPosition.y);
 
            if (eTowerFuntion == ETowerFuntion.FREEZE && ctrl != null)
            {
                ctrl.gameObject.SetActive(true);
                FreezeBreathCtrl = ctrl;
                ctrl.SetProgress(0);
            }
            else
            {
                ctrl.gameObject.SetActive(false);
                FreezeBreathCtrl = null;
            }
        }
 
        /// <summary>
        /// This function is called when the object becomes enabled and active.
        /// </summary>
        void OnEnable()
        {
 
            // if (bulletCtl != null)
            // {
            //     Debug.Log("打开了bulletCtl");
            //     bulletCtl.gameObject.SetActive(true);
            // }
 
 
            // if (energyCtl != null)
            // {
            //     Debug.Log("打开了energyCtl");
            //     energyCtl.gameObject.SetActive(true);
            // }
        }
 
        /// <summary>
        /// This function is called when the behaviour becomes disabled or inactive.
        /// </summary>
        void OnDisable()
        {
            if (bulletCtl != null)
            {
                bulletCtl.gameObject.SetActive(false);
            }
 
            if (energyCtl != null)
            {
                energyCtl.gameObject.SetActive(false);
            }
 
            if (FreezeBreathCtrl != null)
                FreezeBreathCtrl.gameObject.SetActive(false);
        }
 
        /// <summary>
        /// 初始化当前塔防的局内升级,lvl从1开始.
        /// </summary>
        /// <param name="lvl"></param>
        public void initInSceneTowerLevel(int lvl)
        {
            inSceneTowerLevel = lvl;
            if (lvl <= 1)
            {
                ResetInSceneTowerLevel();
                return;
            }
            // 设置攻击数据的加强,暂时是测试数据,后面需要读取表格数据处理:
            float damageAdd = 0;
            // float damageAdd = (inSceneTowerLevel - 1) * INSCENE_TU_DAMAGE;
            for (int ti = 0; ti < levels.Length; ti++)
            {
                if (levels[ti].levelDamager)
                {
                    levels[ti].levelDamager.doubleHit = bDoubleHitElit;
                    levels[ti].levelDamager.inSceneUpGradeDamage = damageAdd;
                }
            }
        }
 
        protected void ResetInSceneTowerLevel()
        {
            for (int ti = 0; ti < levels.Length; ti++)
            {
                if (levels[ti].levelDamager)
                {
                    levels[ti].levelDamager.inSceneUpGradeDamage = 0;
                    levels[ti].levelDamager.doubleHit = bDoubleHitElit;
                }
            }
            return;
        }
 
        /// <summary>
        /// 升级当前塔防的局内等级,需要播放相关的特效
        /// </summary>
        public void upGradeInSceneTL()
        {
            inSceneTowerLevel++;
 
            // 设置攻击数据的加强,暂时是测试数据,后面需要读取表格数据处理:
            float damageAdd = inSceneTowerLevel * INSCENE_TU_DAMAGE;
 
            for (int ti = 0; ti < levels.Length; ti++)
            {
                if (levels[ti].levelDamager)
                {
                    levels[ti].levelDamager.inSceneUpGradeDamage = damageAdd;
                    levels[ti].levelDamager.towerName = name;
                    levels[ti].levelDamager.bSet = true;
                }
            }
 
            Debug.Log("Upgrade Tower name is:" + name);
 
            // 播放相关的特效
            currentTowerLevel.PlayUpGradeEffect();
        }
 
        /// <summary>
        /// Gets the first level tower ghost prefab
        /// </summary>
        public TowerPlacementGhost towerGhostPrefab
        {
            get { return levels[currentLevel].towerGhostPrefab; }
        }
 
        /// <summary>
        /// Gets the grid position for this tower on the <see cref="placementArea"/>
        /// </summary>
        public IntVector2 gridPosition { get; private set; }
 
        /// <summary>
        /// The placement area we've been built on
        /// </summary>
        public IPlacementArea placementArea { get; private set; }
 
        /// <summary>
        /// The purchase cost of the tower
        /// </summary>
        public int purchaseCost
        {
            get { return levels[0].cost; }
        }
 
        /// <summary>
        /// The event that fires off when a player deletes a tower
        /// </summary>
        public Action towerDeleted;
 
        /// <summary>
        /// The event that fires off when a tower has been destroyed
        /// </summary>
        public Action towerDestroyed;
 
        /// <summary>
        /// 放置到目标位置
        /// </summary>
        /// <param name="destination"></param>
        public virtual void SetToDestination(IntVector2 destination)
        {
            gridPosition = destination;
            transform.position = placementArea.GridToWorld(destination, dimensions);
        }
 
        /// <summary>
        /// Provide the tower with data to initialize with
        /// </summary>
        /// <param name="targetArea">The placement area configuration</param>
        /// <param name="destination">The destination position</param>
        public virtual void Initialize(IPlacementArea targetArea, IntVector2 destination, int lvl = 0)
        {
            placementArea = targetArea;
            gridPosition = destination;
 
            if (targetArea != null)
            {
                transform.position = placementArea.GridToWorld(destination, dimensions);
                transform.rotation = placementArea.transform.rotation;
                targetArea.Occupy(destination, dimensions);
            }
 
            SetLevel(lvl);
            if (LevelManager.instanceExists)
            {
                LevelManager.instance.levelStateChanged += OnLevelStateChanged;
            }
            else if (EndlessLevelManager.instanceExists)
                EndlessLevelManager.instance.LevelStateChanged += OnLevelStateChanged;
 
            // 查找Targetter:
            Targetter target = GetComponentInChildren<Targetter>();
            if (target)
            {
                target.bOpponent = opponentSide;
            }
            else
                Debug.Log("在当前的Tower中找不到Targetter.");
 
            // 
            // 初始化当前的局内Tower等级数据
            initInSceneTowerLevel(SceneTowerLvl.getInSceneTowerLvl(towerName));
 
            StartCoroutine(ResetScale());
        }
 
        IEnumerator ResetScale()
        {
            yield return new WaitForSeconds(0.2f);
            if (gridPosition.y > 1)
            {
                currentTowerLevel.ResetScale();
            }
            else
            {
                currentTowerLevel.NormalScale();
            }
        }
 
        /// <summary>
        /// Provides information on the cost to upgrade
        /// </summary>
        /// <returns>Returns -1 if the towers is already at max level, other returns the cost to upgrade</returns>
        public int GetCostForNextLevel()
        {
            if (isAtMaxLevel)
            {
                return -1;
            }
            return levels[currentLevel + 1].cost;
        }
 
        /// <summary>
        /// 当前的Tower是显示还是隐藏,用于减少游戏内Tower创建和删除对应的一系列数据操作
        /// </summary>
        /// <param name="vis"></param>
        public void showTower(bool vis)
        {
            if (vis)
            {
                gameObject.SetActive(true);
            }
            else
            {
                gameObject.SetActive(false);
            }
        }
 
        /// <summary>
        /// Kills this tower
        /// </summary>
        public void KillTower()
        {
            // Invoke base kill method
            Kill();
        }
 
        /// <summary>
        /// Provides the value recived for selling this tower
        /// </summary>
        /// <returns>A sell value of the tower</returns>
        public int GetSellLevel()
        {
            return GetSellLevel(currentLevel);
        }
 
        /// <summary>
        /// Provides the value recived for selling this tower of a particular level
        /// </summary>
        /// <param name="level">Level of tower</param>
        /// <returns>A sell value of the tower</returns>
        public int GetSellLevel(int level)
        {
            // sell for full price if waves haven't started yet
            if (LevelManager.instanceExists && LevelManager.instance.levelState == LevelState.Building
                || EndlessLevelManager.instanceExists && EndlessLevelManager.instance.EndlessLeveltate == LevelState.Building)
            {
                int cost = 0;
                for (int i = 0; i <= level; i++)
                {
                    cost += levels[i].cost;
                }
 
                return cost;
            }
            return levels[currentLevel].sell;
        }
 
        /// <summary>
        /// Used to (try to) upgrade the tower data
        /// </summary>
        public virtual bool UpgradeTower()
        {
            if (isAtMaxLevel)
            {
                return false;
            }
            SetLevel(currentLevel + 1);
            return true;
        }
 
        /// <summary>
        /// A method for downgrading tower
        /// </summary>
        /// <returns>
        /// <value>false</value> if tower is at lowest level
        /// </returns>
        public virtual bool DowngradeTower()
        {
            if (currentLevel == 0)
            {
                return false;
            }
            SetLevel(currentLevel - 1);
            return true;
        }
 
        /// <summary>
        /// Used to set the tower to any valid level
        /// </summary>
        /// <param name="level">
        /// The level to upgrade the tower to
        /// </param>
        /// <returns>
        /// True if successful
        /// </returns>
        public virtual bool UpgradeTowerToLevel(int level)
        {
            if (level < 0 || isAtMaxLevel || level >= levels.Length)
            {
                return false;
            }
            SetLevel(level);
            return true;
        }
 
        public void Sell()
        {
            Remove();
        }
 
        /// <summary>
        /// Removes tower from placement area and destroys it
        /// </summary>
        public override void Remove()
        {
            base.Remove();
 
            // 清空局内升级数据:
            ResetInSceneTowerLevel();
            attackRise = 0.0f;
            placementArea.Clear(gridPosition, dimensions);
            Destroy(gameObject);
        }
 
        /// <summary>
        /// unsubsribe when necessary
        /// </summary>
        protected virtual void OnDestroy()
        {
            if (LevelManager.instanceExists)
            {
                LevelManager.instance.levelStateChanged += OnLevelStateChanged;
            }
            else if (EndlessLevelManager.instanceExists)
                EndlessLevelManager.instance.LevelStateChanged += OnLevelStateChanged;
        }
 
        /// <summary>
        /// Cache and update oftenly used data
        /// </summary>
        public void SetLevel(int level)
        {
            if (level < 0 || level >= levels.Length)
            {
                return;
            }
            currentLevel = level;
            if (currentTowerLevel != null)
            {
                Destroy(currentTowerLevel.gameObject);
            }
 
            // instantiate the visual representation
            currentTowerLevel = Instantiate(levels[currentLevel], transform);
 
            // initialize TowerLevel
            currentTowerLevel.Initialize(this, enemyLayerMask, configuration.alignmentProvider);
 
            // health data
            ScaleHealth();
 
            // 
            // disable affectors
            LevelState levelState = LevelState.Intro;
            if (LevelManager.instanceExists)
                levelState = LevelManager.instance.levelState;
            else if (EndlessLevelManager.instanceExists)
                levelState = EndlessLevelManager.instance.EndlessLeveltate;
            bool initialise = levelState == LevelState.AllEnemiesSpawned || levelState == LevelState.SpawningEnemies;
            initialise = false;
            currentTowerLevel.SetAffectorState(initialise, gridPosition.x);
        }
 
        /// <summary>
        /// Scales the health based on the previous health
        /// Requires override when the rules for scaling health on upgrade changes
        /// </summary>
        protected virtual void ScaleHealth()
        {
            configuration.SetMaxHealth(currentTowerLevel.maxHealth);
 
            if (currentLevel == 0)
            {
                configuration.SetHealth(currentTowerLevel.maxHealth);
            }
            else
            {
                int currentHealth = Mathf.FloorToInt(configuration.normalisedHealth * currentTowerLevel.maxHealth);
                configuration.SetHealth(currentHealth);
            }
        }
 
        /// <summary>
        /// Intiailises affectors based on the level state
        /// </summary>
        protected virtual void OnLevelStateChanged(LevelState previous, LevelState current)
        {
            bool initialise = current == LevelState.AllEnemiesSpawned || current == LevelState.SpawningEnemies;
            initialise = false;
            //currentTowerLevel.SetAffectorState(initialise);
            currentTowerLevel.SetAffectorState(bInAttackMode, gridPosition.x);
        }
    }
}