chenxin
2020-10-27 38136a34de9aa36bf15ec7471abd56e2cba6c26f
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
using UnityEngine;
using UnityEngine.UI;
using TowerDefense.UI.HUD;
using DG.Tweening;
using TowerDefense.Towers.Placement;
using UnityEngine.SceneManagement;
using TowerDefense.Level;
 
/**
 * 无尽模式结算
 * @Author: chenxin
 * @Date: 2020-10-19 17:11:03
 */
namespace KTGMGemClient
{
    public class EndlessSettlement : MonoBehaviour
    {
        public AudioClip VictoryAudio;
 
        public AudioSource AudioSource;
 
        /// <summary>
        /// Name of level select screen
        /// </summary>
        public string menuSceneName = "MainManuScene";
 
        /// <summary>
        /// Reference to the <see cref="EndlessLevelManager" />
        /// </summary>
        protected EndlessLevelManager levelManager;
 
        /// <summary>
        /// 最终波次文本
        /// </summary>
        public Text FinalWaveText;
 
        /// <summary>
        /// 道具列表UI
        /// </summary>
        public GameObject PropListUI;
 
        public GameObject SettlementUI;
 
        public void Init()
        {
            LazyLoad();
            levelManager.LevelCompleted -= Victory;
            levelManager.LevelCompleted += Victory;
        }
 
        /// <summary>
        /// 回到主界面的功能,暂时也使用重新开启新关卡.
        /// </summary>
        public void ReturnToMainMenu()
        {
            if (GameConfig.IsNewbieGuideCompleted)
                GameConfig.IsNewbie = false;
 
            // 清空所有Tween数据:
            DOTween.Clear();
            EndlessUIStart.bFirstLoaded = false;
            EndlessUIStart.bGameStart = false;
 
            TowerPlacementGridEndless.GRID_OPENCASH = 100;
 
            SafelyUnsubscribe();
            SceneManager.LoadScene(menuSceneName);
        }
 
        /// <summary>
        /// Shows the end game screen
        /// </summary>
        protected void OpenEndGameScreen(bool victory)
        {
            if (!EndlessGameUI.instanceExists) return;
 
            if (EndlessGameUI.instance.state == EndlessGameUI.State.Building)
                EndlessGameUI.instance.CancelGhostPlacement();
 
            RefreshFinalWave();
            RefreshPropList();
            SettlementUI.SetActive(true);
        }
 
        /// <summary>
        /// Occurs when the level is sucessfully completed
        /// </summary>
        protected void Victory()
        {
            EndlessUIStart.instance.Pause();
            OpenEndGameScreen(true);
            if (VictoryAudio != null && AudioSource != null)
                AudioSource.PlayOneShot(VictoryAudio);
        }
 
        /// <summary>
        /// Safely unsubscribes from <see cref="EndlessLevelManager" /> events.
        /// </summary>
        protected void OnDestroy()
        {
            SafelyUnsubscribe();
            if (EndlessGameUI.instanceExists)
                EndlessGameUI.instance.Unpause();
        }
 
        // <summary>
        /// Ensure that <see cref="EndlessLevelManager" /> events are unsubscribed from when necessary
        /// </summary>
        protected void SafelyUnsubscribe()
        {
            LazyLoad();
            levelManager.LevelCompleted -= Victory;
        }
 
        /// <summary>
        /// Ensure <see cref="EndlessLevelManager" /> is not null
        /// </summary>
        protected void LazyLoad()
        {
            if (levelManager == null && EndlessLevelManager.instanceExists)
                levelManager = EndlessLevelManager.instance;
        }
 
        /// <summary>
        /// 刷新最终波次信息
        /// </summary>
        /// <param name="level">关卡等级</param>
        /// <param name="wave">波次</param>
        private void RefreshFinalWave()
        {
            int level = EndlessLevelManager.instance.CurrentLevel;
            int wave = EndlessLevelManager.instance.WaveManager.CurrentWaveIndex;
 
            FinalWaveText.text = $"最终波次:第{level}关,第{wave}波";
        }
 
        private void RefreshPropList()
        {
            EndlessSettlementPropList list = PropListUI.GetComponent<EndlessSettlementPropList>();
            list.RefreshList();
        }
    }
}