chenxin
2020-11-07 53d87db7d89d71eede56b19e25e5cb95ce2bbe76
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
 
using Core.Game;
using Core.Health;
using DG.Tweening;
using TowerDefense.Game;
using TowerDefense.Level;
using TowerDefense.Towers.Placement;
using TowerDefense.UI.HUD;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
 
namespace TowerDefense.UI
{
    /// <summary>
    /// UI to display the game over screen
    /// </summary>
    public class EndGameScreen : MonoBehaviour
    {
        /// <summary>
        /// AudioClip to play when victorious
        /// </summary>
        public AudioClip victorySound;
 
        /// <summary>
        /// AudioClip to play when failed
        /// </summary>
        public AudioClip defeatSound;
 
        /// <summary>
        /// AudioSource that plays the sound
        /// </summary>
        public AudioSource audioSource;
 
        /// <summary>
        /// The containing panel of the End Game UI
        /// </summary>
        public Canvas endGameCanvas;
 
        /// <summary>
        /// 游戏胜利时应该显示的Image.
        /// </summary>
        public Sprite victoryImg;
 
        /// <summary>
        /// 游戏失败时应该显示的Image.
        /// </summary>
        public Sprite failedImg;
 
        /// <summary>
        /// 显示游戏结果的图片.
        /// </summary>
        public Image gameResultImgOpp;
        public Image gameResultImgSelf;
 
        /// <summary>
        /// Panel that shows final star rating
        /// </summary>
        //public ScorePanel scorePanel;
 
        /// <summary>
        /// Name of level select screen
        /// </summary>
        public string menuSceneName = "MainManuScene";
 
        /// <summary>
        /// Text to be displayed on popup
        /// </summary>
        public string levelCompleteText = "{0} COMPLETE!";
        
        public string levelFailedText = "{0} FAILED!";
 
        /// <summary>
        /// Background image
        /// </summary>
        public Image background;
 
        /// <summary>
        /// Color to set background
        /// </summary>
        //public Color winBackgroundColor;
        //public Color loseBackgroundColor;
 
        /// <summary>
        /// The Canvas that holds the button to go to the next level
        /// if the player has beaten the level
        /// </summary>
        //public Canvas nextLevelButton;
 
        /// <summary>
        /// Reference to the <see cref="LevelManager" />
        /// </summary>
        protected LevelManager m_LevelManager;
 
        /// <summary>
        /// Safely unsubscribes from <see cref="LevelManager" /> events.
        /// Go back to the main menu scene
        /// 这个是旧版本的回到主菜单,暂时是用不上的。
        /// </summary>
        public void GoToMainMenu()
        {
            DOTween.Clear();
            UIStart.bFirstLoaded = false;
            UIStart.bGameStart = false;
 
            SafelyUnsubscribe();
            SceneManager.LoadScene(menuSceneName);
        }
 
 
 
        /// <summary>
        /// Safely unsubscribes from <see cref="LevelManager" /> events.
        /// Reloads the active scene
        /// </summary>
        public void RestartLevel()
        {
            UIStart.bFirstLoaded = false;
            UIStart.bGameStart = false;
 
            SafelyUnsubscribe();
            string currentSceneName = SceneManager.GetActiveScene().name;
            SceneManager.LoadScene(currentSceneName);
 
 
            // 清空所有Tween数据:
            DOTween.Clear();
 
            TowerPlacementGrid.GRID_OPENCASH_OPPO = 100;
            TowerPlacementGrid.GRID_OPENCASH_SELF = 100;
 
            // 关键数据需要重置: 
            // 1: 局内升级相关的数据
            SceneTowerLvl.clearInSceneTowerData();
            // 2: GameUI需要清空.
            GameUI.instance.restartLevel();
            // 3: AgentInsManager需要重来.
            AgentInsManager.instance.restartLevel();
            // 4: 格子相关的数据重新开始.
 
        }
 
        /// <summary>
        /// 回到主界面的功能,暂时也使用重新开启新关卡.
        /// </summary>
        public void ReturnToMainMenu()
        {
            // 清空所有Tween数据:
            DOTween.Clear();
            UIStart.bFirstLoaded = false;
            UIStart.bGameStart = false;
 
            TowerPlacementGrid.GRID_OPENCASH_OPPO = 100;
            TowerPlacementGrid.GRID_OPENCASH_SELF = 100;
 
 
            SafelyUnsubscribe();
            SceneManager.LoadScene(menuSceneName);
        }
 
        /// <summary>
        /// Safely unsubscribes from <see cref="LevelManager" /> events.
        /// Goes to the next scene if valid
        /// </summary>
        public void GoToNextLevel()
        {
            SafelyUnsubscribe();
            if (!GameManager.instanceExists)
            {
                return;
            }
            GameManager gm = GameManager.instance;
            LevelItem item = gm.GetLevelForCurrentScene();
            LevelList list = gm.levelList;
            int levelCount = list.Count;
            int index = -1;
            for (int i = 0; i < levelCount; i++)
            {
                if (item == list[i])
                {
                    index = i + 1;
                    break;
                }
            }
            if (index < 0 || index >= levelCount)
            {
                return;
            }
            LevelItem nextLevel = gm.levelList[index];
            SceneManager.LoadScene(nextLevel.sceneName);
        }
 
        /// <summary>
        /// Hide the panel if it is active at the start.
        /// Subscribe to the <see cref="LevelManager" /> completed/failed events.
        /// </summary>
        protected void Start()
        {
            LazyLoad();
            endGameCanvas.enabled = false;
            //nextLevelButton.enabled = false;
            //nextLevelButton.gameObject.SetActive(false);
 
            m_LevelManager.levelCompleted += Victory;
            m_LevelManager.levelFailed += Defeat;
        }
 
        /// <summary>
        /// Shows the end game screen
        /// </summary>
        protected void OpenEndGameScreen( bool victory )
        {
            //endResultText = "关卡失败!";
            //LevelItem level = GameManager.instance.GetLevelForCurrentScene();
            endGameCanvas.enabled = true;
 
            int score = CalculateFinalScore();
 
            // 根据胜利失败,展示不同的背景图片:
            if (victory)
            {
                gameResultImgOpp.sprite = this.failedImg;
                gameResultImgSelf.sprite = this.victoryImg;
            }
            else
            {
                gameResultImgOpp.sprite = this.victoryImg;
                gameResultImgSelf.sprite = this.failedImg;
            }
                
 
            if (!HUD.GameUI.instanceExists)
            {
                return;
            }
            if (HUD.GameUI.instance.state == HUD.GameUI.State.Building)
            {
                HUD.GameUI.instance.CancelGhostPlacement();
            }
            HUD.GameUI.instance.GameOver();
        }
 
        /// <summary>
        /// Occurs when the level is sucessfully completed
        /// </summary>
        protected void Victory()
        {
            OpenEndGameScreen(true);
            // if ((victorySound != null) && (audioSource != null))
            // {
            //     audioSource.PlayOneShot(victorySound);
            // }
            //background.color = winBackgroundColor;
 
            //first check if there are any more levels after this one
            //if (nextLevelButton == null || !GameManager.instanceExists)
            //{
            //    return;
            //}
 
/*            GameManager gm = GameManager.instance;
            LevelItem item = gm.GetLevelForCurrentScene();
            LevelList list = gm.levelList;
            int levelCount = list.Count;
            int index = -1;
            for (int i = 0; i < levelCount; i++)
            {
                if (item == list[i])
                {
                    index = i;
                    break;
                }
            }
            //if the level does not exist or this is the last level
            //hide the next level button
            if (index < 0 || index == levelCount - 1)
            {
                //nextLevelButton.enabled = false;
                //nextLevelButton.gameObject.SetActive(false);
                return;
            }*/
            //nextLevelButton.enabled = true;
            //nextLevelButton.gameObject.SetActive(true);
        }
 
        /// <summary>
        /// Occurs when level is failed
        /// </summary>
        protected void Defeat()
        {
            OpenEndGameScreen( false );// levelFailedText);
            //if (nextLevelButton != null)
            //{
            //    nextLevelButton.enabled = false;
            //    nextLevelButton.gameObject.SetActive(false);
            //}
            // if ((defeatSound != null) && (audioSource != null))
            // {
            //     audioSource.PlayOneShot(defeatSound);
            // }
            //background.color = loseBackgroundColor;
        }
 
        /// <summary>
        /// Safely unsubscribes from <see cref="LevelManager" /> events.
        /// </summary>
        protected void OnDestroy()
        {
            SafelyUnsubscribe();
            if (HUD.GameUI.instanceExists)
            {
                HUD.GameUI.instance.Unpause();
            }
        }
 
        /// <summary>
        /// Ensure that <see cref="LevelManager" /> events are unsubscribed from when necessary
        /// </summary>
        protected void SafelyUnsubscribe()
        {
            LazyLoad();
            m_LevelManager.levelCompleted -= Victory;
            m_LevelManager.levelFailed -= Defeat;
        }
 
        /// <summary>
        /// Ensure <see cref="m_LevelManager" /> is not null
        /// </summary>
        protected void LazyLoad()
        {
            if ((m_LevelManager == null) && LevelManager.instanceExists)
            {
                m_LevelManager = LevelManager.instance;
            }
        }
 
        /// <summary>
        /// Add up the health of all the Home Bases and return a score
        /// </summary>
        /// <returns>Final score</returns>
        protected int CalculateFinalScore()
        {
            int homeBaseCount = m_LevelManager.numberOfHomeBases;
            PlayerHomeBase[] homeBases = m_LevelManager.playerHomeBases;
 
            float totalRemainingHealth = 0f;
            float totalBaseHealth = 0f;
            for (int i = 0; i < homeBaseCount; i++)
            {
                Damageable config = homeBases[i].configuration;
                totalRemainingHealth += config.currentHealth;
                totalBaseHealth += config.maxHealth;
            }
            int score = CalculateScore(totalRemainingHealth, totalBaseHealth);
            return score;
        }
 
        /// <summary>
        /// Take the final remaining health of all bases and rates them
        /// </summary>
        /// <param name="remainingHealth">the total remaining health of all home bases</param>
        /// <param name="maxHealth">the total maximum health of all home bases</param>
        /// <returns>0 to 3 depending on how much health is remaining</returns>
        protected int CalculateScore(float remainingHealth, float maxHealth)
        {
            float normalizedHealth = remainingHealth / maxHealth;
            if (Mathf.Approximately(normalizedHealth, 1f))
            {
                return 3;
            }
            if ((normalizedHealth <= 0.9f) && (normalizedHealth >= 0.5f))
            {
                return 2;
            }
            if ((normalizedHealth < 0.5f) && (normalizedHealth > 0f))
            {
                return 1;
            }
            return 0;
        }
    }
}