wangguan
2020-12-26 e371272a7885723c7b0ef31a20ae5d0fbead1d30
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
using KTGMGemClient;
using ActionGameFramework.Health;
using Core.Health;
using Core.Utilities;
using TowerDefense.Level;
using TowerDefense.Nodes;
using TowerDefense.UI.HUD;
using UnityEngine;
 
namespace TowerDefense.Agents
{
    /// <summary>
    /// A component that attacks a home base when an agent reaches it 
    /// </summary>
    [RequireComponent(typeof(Agent))]
    public class HomeBaseAttacker : MonoBehaviour
    {
        /// <summary>
        /// How long the agent charges for before it attacks
        /// the home base
        /// </summary>
        public float homeBaseAttackChargeTime = 0.5f;
 
        /// <summary>
        /// Timer used to stall attack to the home base
        /// </summary>
        protected Timer m_HomeBaseAttackTimer;
 
        /// <summary>
        /// If the agent has reached the Player Home Base and is charging an attack
        /// </summary>
        protected bool m_IsChargingHomeBaseAttack;
 
        /// <summary>
        /// The DamageableBehaviour on the home base 
        /// </summary>
        protected DamageableBehaviour m_FinalDestinationDamageableBehaviour;
 
        /// <summary>
        /// The agent component attached to this gameObject
        /// </summary>
        public Agent agent { get; protected set; }
 
        /// <summary>
        /// Fired on completion of <see cref="m_HomeBaseAttackTimer"/>
        /// Applies damage to the homebase
        /// </summary>
        protected void AttackHomeBase()
        {
            m_IsChargingHomeBaseAttack = false;
            var damager = GetComponent<Damager>();
            if (damager != null)
            {
                if (!EndlessLevelManager.instanceExists)
                {
                    //Debug.Log("开始扣血");
                    m_FinalDestinationDamageableBehaviour.TakeDamage(damager.finalDamage, transform.position, agent.configuration.alignmentProvider);
                    PlayerHomeBase homebase = m_FinalDestinationDamageableBehaviour.GetComponent<PlayerHomeBase>();
                    if (homebase && (!homebase.isDead))
                    {
                        // 显示塔位血条
                        float hscale = homebase.healthVal / homebase.configuration.maxHealth;
                        if (hscale < 1.0)
                        {
                            if (homebase.opponent)
                                OpponentMgr.instance.m_CurrentArea.setTowerPosHealth(homebase.homebaseIdx, hscale);
                            else
                            {
                                if (null != GameUI.instance.selfTowerPlaceArea)
                                    GameUI.instance.selfTowerPlaceArea.setTowerPosHealth(homebase.homebaseIdx, hscale);
                            }
                        }
                    }
                }
                else
                {
                    if (GameConfig.IsNewbie)
                    {
                        if (GameConfig.TowerFirstTakeDamage)
                        {
                            GameConfig.TowerFirstTakeDamage = false;
                            EventCenter.Ins.BroadCast((int)KTGMGemClient.EventType.EndlessLoseHeart, 1);
                            //EndlessLevelManager.instance.StopSecondWave();
                        }
                        else
                        {
                            if (GameConfig.TowerCanTakeDamage)
                                EventCenter.Ins.BroadCast((int)KTGMGemClient.EventType.EndlessLoseHeart, 1);
                        }
                    }
                    else
                        EventCenter.Ins.BroadCast((int)KTGMGemClient.EventType.EndlessLoseHeart, 1);
                }
            }
 
            if (EndlessGameUI.instance.state == EndlessGameUI.State.GameOver) return;
 
            EventCenter.Ins.BroadCast((int)KTGMGemClient.EventType.EndlessAgentTaskDamage, agent.configuration.currentHealth);
            agent.Remove();
        }
 
        /// <summary>
        /// Ticks the attack timer
        /// </summary>
        protected virtual void Update()
        {
            // Update HomeBaseAttack Timer
            if (m_IsChargingHomeBaseAttack)
            {
                m_HomeBaseAttackTimer.Tick(Time.deltaTime);
            }
        }
 
        /// <summary>
        /// Caches the attached Agent and subscribes to the destinationReached event
        /// </summary>
        protected virtual void Awake()
        {
            agent = GetComponent<Agent>();
            agent.destinationReached += OnDestinationReached;
            agent.died += OnDied;
        }
 
        /// <summary>
        /// Unsubscribes from the destinationReached event
        /// </summary>
        protected virtual void OnDestroy()
        {
            if (agent != null)
            {
                agent.destinationReached -= OnDestinationReached;
                agent.died -= OnDied;
            }
        }
 
        /// <summary>
        /// Stops the attack on the home base
        /// </summary>
        void OnDied(DamageableBehaviour damageableBehaviour)
        {
            m_IsChargingHomeBaseAttack = false;
        }
 
        /// <summary>
        /// Fired then the agent reached its final node,
        /// Starts the attack timer
        /// </summary>
        /// <param name="homeBase"></param>
        void OnDestinationReached(Node homeBase)
        {
            if (agent.bInDeathState) return;
 
            m_FinalDestinationDamageableBehaviour = homeBase.GetComponent<DamageableBehaviour>();
            // start timer 
            if (m_HomeBaseAttackTimer == null)
            {
                m_HomeBaseAttackTimer = new Timer(homeBaseAttackChargeTime, AttackHomeBase);
                //Debug.Log("HomeBase Timer 设置起来...");
                agent.PlayAttack();
            }
            else
            {
                //m_HomeBaseAttackTimer.Reset();
                // Debug.Log("重复设置导致有可能怪物不会消失.");
            }
            m_IsChargingHomeBaseAttack = true;
        }
    }
}