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
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
using System;
using System.Collections;
using System.Collections.Generic;
using KTGMGemClient;
using UnityEngine;
using TowerDefense.UI.HUD;
 
namespace TowerDefense.Level
{
    /// <summary>
    /// PVE 无尽模式一个关卡波次管理器
    /// </summary>
    public class EndlessWaveManager : MonoBehaviour
    {
        /// <summary>
        /// 当前关卡等级的所有波数据
        /// </summary>
        public List<List<EndlessPortConfig>> LevelData { get; set; }
 
        /// <summary>
        /// 波次时间间隔
        /// </summary>
        public float WaveInterval { get; set; }
 
        /// <summary>
        /// 怪物移速缩放
        /// </summary>
        public float SpeedScale { get; set; } = 1.0f;
 
        /// <summary>
        /// 当前波索引
        /// </summary>
        public int CurrentWaveIndex { get; protected set; }
 
        /// <summary>
        /// 当前波数据
        /// </summary>
        protected List<EndlessPortConfig> waveData;
 
        /// <summary>
        /// 实际上就是5条兵线
        /// </summary>
        [Tooltip("Specify list in order")]
        public List<EndlessWave> waves = new List<EndlessWave>();
 
        /// <summary>
        /// 当前WaveManager是否开始Wave.
        /// </summary>
        protected bool isWaveStarted = false;
 
        /// <summary>
        /// Called when all waves are finished
        /// </summary>
        public event Action AllWaveCompleted;
 
        /// <summary>
        /// 当前关卡的总波数
        /// </summary>
        public int TotalWaves { get; protected set; }
 
        /// <summary>
        /// 当前波的赛道总数
        /// </summary>
        public int TotalWaveLines { get; set; }
 
        /// <summary>
        /// 当前波已经完成的赛道数量
        /// </summary>
        public int CompletedWaveLine { get; protected set; }
 
        /// <summary>
        /// 本波次敌人总数量
        /// </summary>
        public int TotalEnemies { get; protected set; }
 
        /// <summary>
        /// 本波次剩余敌人数量
        /// </summary>
        public int RemainEnemies { get; protected set; }
 
        /// <summary>
        /// 当前正在进行的关卡等级
        /// </summary>
        public int Level { get; protected set; }
 
        /// <summary>
        /// Starts the waves
        /// </summary>
        /// <param name="level">关卡等级</param>
        /// 
        public virtual void StartWaves(int level)
        {
            Debug.Log($"--------------------- 开始第 {level} 关 ---------------------");
            Level = level;
            LevelData = EndlessPortData.GetLevelWaveData(level);
            TotalWaves = LevelData.Count;
            CurrentWaveIndex = 0;
            EndlessBossHPManager.instance.InitHP();
            EndlessLevelManager.instance.DecrementEnemies -= DecrementEnemies;
            EndlessLevelManager.instance.DecrementEnemies += DecrementEnemies;
 
            if (LevelData.Count == 0)
                throw new Exception($"当前关卡:{level}, 没有关卡数据");
 
            UpdateWave();
        }
 
        /// <summary>
        /// 敌人数量减少
        /// </summary>
        /// <param name="count"></param>
        public void DecrementEnemies(int count)
        {
            RemainEnemies -= count;
            // 改变血条的进度
            EndlessBossHPManager.instance.SetCurrentProgress(RemainEnemies * 1f / TotalEnemies);
        }
 
        /// <summary>
        /// 获取当前Wave的开始位置
        /// </summary>
        /// <param name="waveline"></param>
        /// <returns></returns>
        public Vector3 GetWaveEndPos(int waveline)
        {
            return waves[waveline].StartingNode.GetNextNode().transform.position;
        }
 
        /// <summary>
        /// 更新波
        /// </summary>
        private void UpdateWave()
        {
            if (EndlessGameUI.instance.state == EndlessGameUI.State.GameOver) return;
 
            if (CurrentWaveIndex > 0)
                EndlessBossHPManager.instance.SwitchHP(CurrentWaveIndex == TotalWaves -1);
            EndlessBossHPManager.instance.UpdateWave(TotalWaves - CurrentWaveIndex);
            EndlessBossHPManager.instance.ShowHP();
            TotalEnemies = EndlessPortData.GetWaveEnemiesCount(Level, CurrentWaveIndex);
            RemainEnemies = TotalEnemies;
 
            waveData = LevelData[CurrentWaveIndex];
            // 直接取第一条就行了
            WaveInterval = waveData[0].Config.cooldown / 1000f;
            TotalWaveLines = waveData.Count;
            CompletedWaveLine = 0;
 
            foreach (EndlessPortConfig data in waveData)
            {
                // 兵线被摧毁了,就不再出兵了,实际上只要不是Wait状态 都应该不能出兵
                if (waves[data.Config.tunel - 1].LineState == EndlessWaveLineState.Destroyed)
                {
                    --TotalWaveLines;
                    continue;
                }
 
                waves[data.Config.tunel - 1].WaveCompleted -= OneWaveCompleted;
                waves[data.Config.tunel - 1].WaveCompleted += OneWaveCompleted;
                waves[data.Config.tunel - 1].StartWave(data);
            }
 
            if (TotalWaveLines != 0)
                isWaveStarted = true;
        }
 
        /// <summary>
        /// 一波结束了
        /// </summary>
        private void OneWaveCompleted()
        {
            ++CompletedWaveLine;
        }
 
        public void Update()
        {
            if (!isWaveStarted) return;
 
            if (CompletedWaveLine >= TotalWaveLines)
            {
                // 判断所有敌人是否全部清除
                WaveLineAgentInsMgr[] waveLineAgentIns = AgentInsManager.instance.GetWaveLineList();
 
                // 判断是否敌人数量为0
                for (int i = 0; i < waveLineAgentIns.Length; ++i)
                {
                    if (waveLineAgentIns[i].getAgentInsNum() != 0)
                        return;
                }
 
                // 更新波索引
                ++CurrentWaveIndex;
                EndlessBuffManager.instance.UpdateBuffList();
 
                if (CurrentWaveIndex >= TotalWaves)
                    // 当前关卡的所有波次全部完成
                    SafelyCallSpawningCompleted();
                else
                {
                    isWaveStarted = false;
                    // 当前波的所有赛道都已经完成出怪
                    StartCoroutine(DelayToUpdateWave());
                }
            }
        }
 
        /// <summary>
        /// 延迟去更新新的一波
        /// </summary>
        /// <returns></returns>
        private IEnumerator DelayToUpdateWave()
        {
            yield return new WaitForSeconds(WaveInterval);
            UpdateWave();
        }
 
        /// <summary>
        /// Calls allWaveCompleted event
        /// </summary>
        protected virtual void SafelyCallSpawningCompleted()
        {
            isWaveStarted = false;
            EndlessBossHPManager.instance.UpdateWave(TotalWaves - CurrentWaveIndex);
 
            if (AllWaveCompleted != null)
                AllWaveCompleted();
        }
    }
}