chenxin
2020-11-26 cdadbc5ac879339934eb03644b802cb6b252794d
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
using System;
using System.Collections;
using System.Collections.Generic;
using KTGMGemClient;
using UnityEngine;
using TowerDefense.UI.HUD;
using Core.Utilities;
using Core.Health;
 
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>
        /// <value></value>
        public int SpawnedTotalEnemies { get; protected set; }
 
        /// <summary>
        /// 本波次剩余敌人数量
        /// </summary>
        public int RemainEnemies { get; protected set; }
 
        /// <summary>
        /// 当前正在进行的关卡等级
        /// </summary>
        public int Level { get; protected set; }
 
        /// <summary>
        /// 法阵
        /// </summary>
        public List<MeshRenderer> TunelList;
 
        private string tunelMaterialPath = "UI/Endless/Tunel/tunel_";
 
        private string faZhenPath = "UI/Endless/Tunel/EndlessFaZhen";
 
        private Timer faZhenRefreshTimer;
 
 
        /// <summary>
        /// 管理法阵的对象池
        /// </summary>
        /// <returns></returns>
        private Dictionary<string, List<GameObject>> faZhenDic = new Dictionary<string, List<GameObject>>();
        private void Start()
        {
            faZhenDic = new Dictionary<string, List<GameObject>>();
            HideTunel();
            EventCenter.Ins.Add<float>((int)KTGMGemClient.EventType.EndlessAgentTaskDamage, OnHit);
            EventCenter.Ins.Add<int>((int)KTGMGemClient.EventType.WaveLineFinish, WaveLineFinish);
 
        }
 
        /// <summary>
        /// Agent掉血,不管是中毒还是乱七八糟的掉血,只要掉血就会接收到
        /// </summary>
        /// <param name="value"></param>
        private void OnHit(float value)
        {
            float nowHP = EndlessBossHPManager.instance.CurrentHP - value;
            EndlessBossHPManager.instance.SetCurrentHP(nowHP);
        }
 
        /// <summary>
        /// 隐藏法阵,即怪出生的圈圈
        /// </summary>
        public void HideTunel()
        {
            for (int i = 0; i < TunelList.Count; ++i)
            {
                TunelList[i].gameObject.SetActive(false);
            }
        }
 
        /// <summary>
        /// 显示法阵
        /// </summary>
        public void ShowTunel()
        {
            for (int i = 0; i < TunelList.Count; ++i)
            {
                TunelList[i].gameObject.SetActive(true);
            }
        }
 
        /// <summary>
        /// Starts the waves
        /// </summary>
        /// <param name="level">关卡等级</param>
        /// 
        public virtual void StartWaves(int level)
        {
            SummonFaZhen();
        }
 
        /// <summary>
        /// 关卡开始时首先初始化关卡信息
        /// </summary>
        /// <param name="level"></param>
        public void InitPort(int level)
        {
            InitPortData(level);
            InitWaveData();
            RefreshBoss();
 
            if (!GameConfig.IsNewbie)
            {
                endless_boss bossData = EndlessBossData.GetDataById(waveData[0].Config.resource);
 
                if (bossData != null)
                    EndlessBossSkillManager.instance.Init(bossData.skill);
            }
        }
 
        /// <summary>
        /// 初始化关卡数据
        /// </summary>
        private void InitPortData(int level)
        {
            Debug.Log($"--------------------- 开始第 {level} 关 ---------------------");
            Level = level;
            LevelData = EndlessPortData.GetLevelWaveData(level);
            TotalWaves = LevelData.Count;
            CurrentWaveIndex = 0;
            EndlessBossHPManager.instance.Init();
            EndlessLevelManager.instance.DecrementEnemies -= DecrementEnemies;
            EndlessLevelManager.instance.DecrementEnemies += DecrementEnemies;
 
            if (LevelData.Count == 0)
                throw new Exception($"当前关卡:{level}, 没有关卡数据");
        }
 
        /// <summary>
        /// 召唤法阵
        /// </summary>
        private void SummonFaZhen()
        {
            EventCenter.Ins.Add((int)KTGMGemClient.EventType.EndlessBossSummonEnd, OnEndlessBossSummonEnd);
            EndlessBossCtrl.instance.ChangeState(EndlessBossActionState.Summon);
        }
 
        /// <summary>
        /// boss召唤动作结束
        /// </summary>
        private void OnEndlessBossSummonEnd()
        {
            EventCenter.Ins.Remove((int)KTGMGemClient.EventType.EndlessBossSummonEnd, OnEndlessBossSummonEnd);
            HideTunel();
            RefreshFaZhen();
        }
 
        /// <summary>
        /// 刷新法阵显示
        /// </summary>
        private void RefreshFaZhen()
        {
            float time = 0;
 
            // 刷新法阵的显示
            for (int i = 0; i < waveData.Count; ++i)
            {
                int tunel = waveData[i].Config.tunel;
                int tunelBgId = waveData[i].Config.tunel_bg;
 
                // string key = $"{faZhenPath}{tunelBgId}{tunelBgId}";
                // List<GameObject> allPS;
                // GameObject tmpPSObj = null;
                // if (faZhenDic.ContainsKey(key))
                // {
                //     allPS = faZhenDic[key];
                //     foreach (GameObject item in allPS)
                //     {
                //         if (item.activeSelf)
                //         {
                //             tmpPSObj = item;
                //             break;
                //         }
                //     }
                // }
 
                // if (tmpPSObj == null)
                // {
                //     allPS = new List<GameObject>();
                //     GameObject obj = Resources.Load<GameObject>($"{faZhenPath}{tunelBgId}{tunelBgId}");
                //     GameObject faZhen = Instantiate(obj);
                //     tmpPSObj = faZhen;
                //     allPS.Add(faZhen);
                //     faZhenDic.Add(key, allPS);
                // }
 
                // tmpPSObj.transform.SetParent(TunelList[tunel - 1].gameObject.transform);
                // tmpPSObj.transform.localPosition = new Vector3(0, 1, 0);
                // tmpPSObj.transform.localScale = new Vector3(2, 2, 2);
                // TunelList[tunel - 1].gameObject.SetActive(true);
                TunelList[tunel - 1].material = Resources.Load<Material>($"{tunelMaterialPath}{tunelBgId}");
                GameObject obj = Resources.Load<GameObject>($"{faZhenPath}{tunelBgId}{tunelBgId}");
                GameObject faZhen = Instantiate(obj);
 
                faZhen.transform.SetParent(TunelList[tunel - 1].gameObject.transform);
                faZhen.transform.localPosition = new Vector3(0, 1, 0);
                faZhen.transform.localScale = new Vector3(2, 2, 2);
                TunelList[tunel - 1].gameObject.SetActive(true);
 
                ParticleSystem ps = faZhen.transform.GetChild(0).GetComponent<ParticleSystem>();
                ps.Play();
                time = ps.main.duration * 3;
                Destroy(faZhen, time);
            }
 
            if (faZhenRefreshTimer == null)
                faZhenRefreshTimer = new Timer(time, UpdateWave);
        }
 
        /// <summary>
        /// 延迟关闭法阵
        /// </summary>
        /// <param name="WaveLineId"></param>
        private void WaveLineFinish(int WaveLineId)
        {
            //StartCoroutine(CloseFaZhen(WaveLineId));
        }
 
        IEnumerator CloseFaZhen(int WaveLineId)
        {
            yield return new WaitForSeconds(1.0f);
            Debug.Log("当前列出怪完成,WaveLineId:" + WaveLineId);
 
            TunelList[WaveLineId - 1].gameObject.SetActive(false);
        }
 
        /// <summary>
        /// 敌人数量减少
        /// </summary>
        /// <param name="count"></param>
        public void DecrementEnemies(int count)
        {
            RemainEnemies -= count;
        }
 
        /// <summary>
        /// 获取当前Wave的开始位置
        /// </summary>
        /// <param name="waveline"></param>
        /// <returns></returns>
        public Vector3 GetWaveEndPos(int waveline)
        {
            return waves[waveline].StartingNode.GetNextNode().transform.position;
        }
 
 
        private void RefreshBoss()
        {
            if (CurrentWaveIndex > 0 || TotalWaves == 1)
                EndlessBossHPManager.instance.SwitchHP(CurrentWaveIndex == TotalWaves - 1);
            EndlessBossHPManager.instance.InitHP(EndlessPortData.GetWaveEnemiesTotalHP(Level, CurrentWaveIndex));
            EndlessBossHPManager.instance.SetBossInfo($"第{Level}关 {waveData[0].Config.boss_name}");
            EndlessBossHPManager.instance.UpdateWave(TotalWaves - CurrentWaveIndex);
            EndlessBossHPManager.instance.ShowHP();
            EndlessScoreManager.instance.ShowScore();
            EndlessLevelManager.instance.DropRate = EndlessPortData.GetDropRate(Level, CurrentWaveIndex);
            reward tmpData = EndlessPortData.GetDropReward(Level, CurrentWaveIndex);
            //if(tmpData!=null && tmpData.type == Protobuf.CURRENCY.Arenaexp)//暂时没法判断,需要新加字段
            if (tmpData != null)
                EndlessLevelManager.instance.DropCoin = tmpData != null ? tmpData.count : 0;
 
        }
 
        /// <summary>
        /// 初始化波次数据
        /// </summary>
        private void InitWaveData()
        {
            waveData = LevelData[CurrentWaveIndex];
            // 直接取第一条就行了
            WaveInterval = waveData[0].Config.cooldown / 1000f;
            TotalWaveLines = waveData.Count;
            CompletedWaveLine = 0;
            SpawnedTotalEnemies = 0;
        }
 
        /// <summary>
        /// 更新波
        /// </summary>
        private void UpdateWave()
        {
            if (EndlessGameUI.instance.state == EndlessGameUI.State.GameOver) return;
 
            TotalEnemies = EndlessPortData.GetWaveEnemiesCount(Level, CurrentWaveIndex);
            RemainEnemies = TotalEnemies;
 
            foreach (EndlessPortConfig data in waveData)
            {
                // 兵线被摧毁了,就不再出兵了,实际上只要不是Wait状态 都应该不能出兵
                if (waves[data.Config.tunel - 1].LineState == EndlessWaveLineState.Destroyed)
                {
                    --TotalWaveLines;
                    continue;
                }
 
                int tunelIndex = data.Config.tunel - 1;
                waves[tunelIndex].WaveCompleted -= OneWaveCompleted;
                waves[tunelIndex].WaveCompleted += OneWaveCompleted;
                waves[tunelIndex].SpawnNewAgent -= SpawnNewAgent;
                waves[tunelIndex].SpawnNewAgent += SpawnNewAgent;
                waves[data.Config.tunel - 1].StartWave(data);
            }
 
            if (TotalWaveLines != 0)
            {
                isWaveStarted = true;
                EndlessBossSkillManager.instance.Restart();
                EventCenter.Ins.BroadCast((int)KTGMGemClient.EventType.EndlessBeginWave);
            }
        }
 
        /// <summary>
        /// 有新的敌人生成
        /// </summary>
        private void SpawnNewAgent()
        {
            ++SpawnedTotalEnemies;
 
            if (GameConfig.IsNewbie)
            {
                if (EndlessUIStart.instance.beginDragStep && SpawnedTotalEnemies >= 3)
                {
                    EndlessUIStart.instance.beginDragStep = false;
                    EndlessLevelManager.instance.StopSecondWave();
                }
                else if (EndlessUIStart.instance.guideThirdWaveStep)
                {
                    //Debug.Log($"当前波次是木属性的 总怪物数量:{TotalEnemies}  当前已经生成了:{SpawnedTotalEnemies * 2}");
                    if (TotalEnemies - (SpawnedTotalEnemies * 2) <= 0)
                    {
                        Debug.Log("延迟第三波生成完毕");
                        StartCoroutine(BroadCastCreateThirdWaveDone());
                    }
                }
            }
        }
 
        IEnumerator BroadCastCreateThirdWaveDone()
        {
            yield return new WaitForSeconds(0.5f);
            EndlessUIStart.instance.guideThirdWaveStep = false;
            EventCenter.Ins.BroadCast((int)KTGMGemClient.EventType.CreateThirdWaveDone);
        }
 
        /// <summary>
        /// 暂停所有兵线出兵
        /// </summary>
        public void PauseAllWave()
        {
            foreach (EndlessPortConfig data in waveData)
            {
                waves[data.Config.tunel - 1].PauseWave();
            }
        }
 
        /// <summary>
        /// 恢复所有兵线出兵
        /// </summary>
        public void RestartAllWave()
        {
            foreach (EndlessPortConfig data in waveData)
            {
                waves[data.Config.tunel - 1].RestartWave();
            }
        }
 
        /// <summary>
        /// 一波结束了
        /// </summary>
        private void OneWaveCompleted()
        {
            ++CompletedWaveLine;
        }
 
        public void Update()
        {
            if (faZhenRefreshTimer != null)
            {
                if (faZhenRefreshTimer.Tick(Time.deltaTime))
                    faZhenRefreshTimer = null;
            }
 
            if (!isWaveStarted || EndlessGameUI.instance.state == EndlessGameUI.State.GameOver) 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();
                EndlessBossSkillManager.instance.Reset();
                EventCenter.Ins.BroadCast((int)KTGMGemClient.EventType.EndlessCompletedPort, Level, CurrentWaveIndex);
 
                if (CurrentWaveIndex >= TotalWaves)
                    // 当前关卡的所有波次全部完成
                    SafelyCallSpawningCompleted();
                else
                {
                    isWaveStarted = false;
                    // 当前波的所有赛道都已经完成出怪
                    StartCoroutine(DelayToUpdateWave());
                }
            }
        }
 
        /// <summary>
        /// 延迟去更新新的一波
        /// </summary>
        /// <returns></returns>
        private IEnumerator DelayToUpdateWave()
        {
            yield return new WaitForSeconds(WaveInterval);
            InitWaveData();
            RefreshBoss();
            SummonFaZhen();
        }
 
        /// <summary>
        /// Calls allWaveCompleted event
        /// </summary>
        protected virtual void SafelyCallSpawningCompleted()
        {
            isWaveStarted = false;
            EndlessBossHPManager.instance.UpdateWave(TotalWaves - CurrentWaveIndex);
 
            if (AllWaveCompleted != null)
                AllWaveCompleted();
        }
    }
}