chenxin
2020-10-21 146e39c5ef6bb29927cb7c00d7708fceab30a724
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
using UnityEngine;
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;
 
        // Start is called before the first frame update
        private void Start()
        {
            LazyLoad();
            levelManager.LevelCompleted += Victory;
        }
 
        /// <summary>
        /// 回到主界面的功能,暂时也使用重新开启新关卡.
        /// </summary>
        public void ReturnToMainMenu()
        {
            // 清空所有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();
            
            gameObject.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;
        }
    }
}