wangguan
2020-11-14 4d7b6d4f3827f2aa9a59f45e1432b50a15df7fc6
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
using System;
using System.Collections.Generic;
using ActionGameFramework.Audio;
using Core.Health;
using DG.Tweening;
using MoreMountains.NiceVibrations;
using TowerDefense.Agents;
using TowerDefense.UI.HUD;
using UnityEngine;
using UnityEngine.UI;
 
namespace TowerDefense.Level
{
    /// <summary>
    /// A class representing the home base that players must defend
    /// </summary>
    public class PlayerHomeBase : DamageableBehaviour
    {
        /// <summary>
        /// The particle system when an attack is charging
        /// </summary>
        public ParticleSystem chargePfx;
 
        /// <summary>
        /// Sound to play when charge effect starts
        /// </summary>
        public RandomAudioSource chargeSound;
        
        /// <summary>
        /// The particle system for an attack
        /// </summary>
        public ParticleSystem attackPfx;
        
        /// <summary>
        /// Sound to play when attack effect starts
        /// </summary>
        public RandomAudioSource attackSound;
 
        /// <summary>
        /// 是否对手的HomeBase
        /// </summary>
        public bool opponent = false;
 
        /// <summary>
        /// 用于确认HomeBase血量的ImageList.
        /// </summary>
        public List<Image> listHeart;
 
 
        /// <summary>
        /// The current Agents within the home base attack zone
        /// </summary>
        protected List<Agent> m_CurrentAgentsInside = new List<Agent>();
 
        /// <summary>
        /// Subscribes to damaged event
        /// </summary>
        protected virtual void Start()
        {
            configuration.damaged += OnDamaged;
        }
 
        /// <summary>
        /// 当前HomeBase对应的X索引,用于跟TowerPlacementGrid的塔位联系起来
        /// </summary>
        public int homebaseIdx
        {
            get;set;
        }
 
        /// <summary>
        /// Unsubscribes to damaged event
        /// </summary>
        protected virtual void OnDestroy()
        {
            configuration.damaged -= OnDamaged;
        }
 
        /// <summary>
        /// Plays <see cref="attackPfx"/> if assigned
        /// </summary>
        protected virtual void OnDamaged(HealthChangeInfo obj)
        {
            if ((attackPfx != null)&&(!opponent) )
            {
                ParticleSystem playParticle = Instantiate(attackPfx);
                playParticle.transform.position = this.position;
                playParticle.Simulate(0.0f);
                playParticle.Play();
 
                // 
                // 播放屏幕振动和手机振动:
                ViewPortAdj.instance.cachedCamera.DOShakePosition(0.25f, 1.5f, 4);
                MMVibrationManager.Haptic(HapticTypes.HeavyImpact);
 
            }
            if (attackSound != null)
            {
                attackSound.PlayRandomClip();
            }
        }
        
        /// <summary>
        /// Adds triggered Agent to tracked Agents, subscribes to Agent's
        /// removed event and plays pfx
        /// </summary>
        /// <param name="other">Triggered collider</param>
        void OnTriggerEnter(Collider other)
        {
            var homeBaseAttacker = other.GetComponent<HomeBaseAttacker>();
            if (homeBaseAttacker == null)
            {
                // 场景内所有放置的Tower上的Targetter都会在这碰撞出现!
                //if( other.transform.parent )
                //    Debug.Log("PlayerHomeBase Name is:" + other.transform.parent.name);
                return;
            }
            //Debug.Log("Agent到达HomeBase的位置...");
            m_CurrentAgentsInside.Add(homeBaseAttacker.agent);
            homeBaseAttacker.agent.removed += OnAgentRemoved;
            if (chargePfx != null)
            {
                chargePfx.Play();
            }
            if (chargeSound != null)
            {
                chargeSound.PlayRandomClip();
            }
            // 播放攻击动作:
            //homeBaseAttacker.agent.PlayAttack();
 
        }
        
        /// <summary>
        /// If the entity that has entered the collider
        /// has an <see cref="Agent"/> component on it
        /// </summary>
        void OnTriggerExit(Collider other)
        {
            var homeBaseAttacker = other.GetComponent<HomeBaseAttacker>();
            if (homeBaseAttacker == null)
            {
                return;
            }
            RemoveTarget(homeBaseAttacker.agent);
        }
        
        /// <summary>
        /// Removes Agent from tracked <see cref="m_CurrentAgentsInside"/>
        /// </summary>
        void OnAgentRemoved(DamageableBehaviour targetable)
        {
            targetable.removed -= OnAgentRemoved;
            Agent attackingAgent = targetable as Agent;
            RemoveTarget(attackingAgent);
        }
 
        /// <summary>
        /// Removes <paramref name="agent"/> from <see cref="m_CurrentAgentsInside"/> and stops pfx 
        /// if there are no more <see cref="Agent"/>s
        /// </summary>
        /// <param name="agent">
        /// The agent to remove
        /// </param>
        void RemoveTarget(Agent agent)
        {
            if (agent == null)
            {
                return;
            }
            m_CurrentAgentsInside.Remove(agent);
            if (m_CurrentAgentsInside.Count == 0 && chargePfx != null)
            {
                chargePfx.Stop();
            }
        }
 
        /// <summary>
        /// Fires kill events
        /// </summary>
        protected override void OnDeath()
        {
            base.OnDeath();
 
            // WORK START: 看看是否对手塔位可以被干掉.对手Wave的出兵规则,需要重新开始处理。
            // 干掉当前塔位,显示成破坏状态
            Debug.Log("当前HomeBase被干掉:" + homebaseIdx.ToString());
            if (GameUI.instanceExists)
                GameUI.instance.DestroyTowerGrid(homebaseIdx, this.opponent);
        }
    }
}