River Jiang
2020-10-28 8637f933a9987b4b16dd9725189a1c6ee2685118
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
//using Boo.Lang;
using TowerDefense.Level;
using TowerDefense.Towers;
using UnityEngine;
using UnityEngine.UI;
 
namespace TowerDefense.UI.HUD
{
    /// <summary>
    /// Controls the UI objects that draw the tower data
    /// </summary>
    [RequireComponent(typeof(Canvas))]
    public class TowerUI : MonoBehaviour
    {
        /// <summary>
        /// The text object for the name
        /// </summary>
        public Text towerName;
 
        /// <summary>
        /// The text object for the description
        /// </summary>
        public Text description;
        
        public Text upgradeDescription;
 
        /// <summary>
        /// The attached sell button
        /// </summary>
        public Button sellButton;
 
        /// <summary>
        /// The attached upgrade button
        /// </summary>
        public Button upgradeButton;
 
        /// <summary>
        /// Component to display the relevant information of the tower
        /// </summary>
        public TowerInfoDisplay towerInfoDisplay;
 
        public RectTransform panelRectTransform;
 
        public GameObject[] confirmationButtons;
 
        /// <summary>
        /// The main game camera
        /// </summary>
        protected Camera m_GameCamera;
 
        /// <summary>
        /// The current tower to draw
        /// </summary>
        protected Tower m_Tower;
 
        /// <summary>
        /// The canvas attached to the gameObject
        /// </summary>
        protected Canvas m_Canvas;
 
        /// <summary>
        /// Draws the tower data on to the canvas,在界面上显示塔防信息,显示操作和信息界面。
        /// 继续修改,确保其它方面的内容处理。
        /// </summary>
        /// <param name="towerToShow">
        /// The tower to gain info from
        /// </param>
        public virtual void Show(Tower towerToShow)
        {
            if (towerToShow == null)
            {
                return;
            }
            m_Tower = towerToShow;
            AdjustPosition();
 
            int sellValue = m_Tower.GetSellLevel();
            ///
            // TEST COED TO DELETE:
            if( sellButton != null)
            {
                // TEST CODE TO DELETE:
                if( m_Tower != null )
                    GameUI.instance.startDragTower(m_Tower);
                /** 以下代码用于测试合成:
                if((!m_Tower.isAtMaxLevel) && GameUI.instance.deleteSameLvlTower( m_Tower))
                {
                    GameUI.instance.UpgradeSelectedTower();
                }*/
                return;
            }
            else
            {
                if (GameUI.instance.towerInList(m_Tower) )
                    return;
            }
 
            m_Canvas.enabled = true;
 
 
            ///
            if (sellButton != null)
            {
                sellButton.gameObject.SetActive(sellValue > 0);
            }
            if (upgradeButton != null)
            {
                upgradeButton.interactable = 
                    LevelManager.instance.currency.CanAfford(m_Tower.GetCostForNextLevel());
                bool maxLevel = m_Tower.isAtMaxLevel;
                upgradeButton.gameObject.SetActive(!maxLevel);
                if (!maxLevel)
                {
                    upgradeDescription.text =
                        m_Tower.levels[m_Tower.currentLevel + 1].upgradeDescription.ToUpper();
                }
            }
            LevelManager.instance.currency.currencyChanged += OnCurrencyChanged;
            towerInfoDisplay.Show(towerToShow);
            foreach (var button in confirmationButtons)
            {
                button.SetActive(false);
            }
        }
 
        /// <summary>
        /// Hides the tower info UI and the radius visualizer
        /// </summary>
        public virtual void Hide()
        {
            m_Tower = null;
            if (GameUI.instanceExists)
            {
                GameUI.instance.HideRadiusVisualizer();
            }
            m_Canvas.enabled = false;
            LevelManager.instance.currency.currencyChanged -= OnCurrencyChanged;
        }
 
        /// <summary>
        /// Upgrades the tower through <see cref="GameUI"/>
        /// </summary>
        public void UpgradeButtonClick()
        {
            GameUI.instance.UpgradeSelectedTower();
        }
 
        /// <summary>
        /// Sells the tower through <see cref="GameUI"/>
        /// </summary>
        public void SellButtonClick()
        {
            GameUI.instance.SellSelectedTower();
        }
 
        /// <summary>
        /// Get the text attached to the buttons
        /// </summary>
        protected virtual void Awake()
        {
            m_Canvas = GetComponent<Canvas>();
        }
 
        /// <summary>
        /// Fires when tower is selected/deselected
        /// </summary>
        /// <param name="newTower"></param>
        protected virtual void OnUISelectionChanged(Tower newTower)
        {
            if (newTower != null)
            {
                Show(newTower);
            }
            else
            {
                Hide();
            }
        }
 
        /// <summary>
        /// Subscribe to mouse button action
        /// </summary>
        protected virtual void Start()
        {
            m_GameCamera = Camera.main;
            m_Canvas.enabled = false;
            if (GameUI.instanceExists)
            {
                GameUI.instance.selectionChanged += OnUISelectionChanged;
                GameUI.instance.stateChanged += OnGameUIStateChanged;
            }
        }
 
        /// <summary>
        /// Adjust position when the camera moves
        /// </summary>
        protected virtual void Update()
        {
            AdjustPosition();
        }
 
        /// <summary>
        /// Unsubscribe from currencyChanged
        /// </summary>
        protected virtual void OnDisable()
        {
            if (LevelManager.instanceExists)
            {
                LevelManager.instance.currency.currencyChanged -= OnCurrencyChanged;
            }
        }
 
        /// <summary>
        /// Adjust the position of the UI
        /// </summary>
        protected void AdjustPosition()
        {
            if (m_Tower == null)
            {
                return;
            }
            Vector3 point = m_GameCamera.WorldToScreenPoint(m_Tower.position);
            point.z = 0;
            panelRectTransform.transform.position = point;
        }
 
        /// <summary>
        /// Fired when the <see cref="GameUI"/> state changes
        /// If the new state is <see cref="GameUI.State.GameOver"/> we need to hide the <see cref="TowerUI"/>
        /// </summary>
        /// <param name="oldState">The previous state</param>
        /// <param name="newState">The state to transition to</param>
        protected void OnGameUIStateChanged(GameUI.State oldState, GameUI.State newState)
        {
            if (newState == GameUI.State.GameOver)
            {
                Hide();
            }
        }
 
        /// <summary>
        /// Check if player can afford upgrade on currency changed
        /// </summary>
        void OnCurrencyChanged()
        {
            if (m_Tower != null && upgradeButton != null)
            {
                upgradeButton.interactable = 
                    LevelManager.instance.currency.CanAfford(m_Tower.GetCostForNextLevel());
            }
        }
 
        /// <summary>
        /// Unsubscribe from GameUI selectionChanged and stateChanged
        /// </summary>
        void OnDestroy()
        {
            if (GameUI.instanceExists)
            {
                GameUI.instance.selectionChanged -= OnUISelectionChanged;
                GameUI.instance.stateChanged -= OnGameUIStateChanged;
            }
        }
    }
}