liuzhiwei
2020-12-09 b01151f932f8faa041a8facfb03059147170f40c
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
using System;
using System.Collections.Generic;
using System.Dynamic;
using Core.Extensions;
using KTGMGemClient;
using TowerDefense.Nodes;
using UnityEngine;
using UnityEngine.Analytics;
 
namespace TowerDefense.Level
{
 
    /// <summary>
    /// 用于在路径上生成Boss的数据
    /// </summary>
    public struct BossSpawnData
    {
        public float time;
        public float speed;
        public float hp;
        public int gold;
        public int waveline;
    }
 
    /// <summary>
    /// WaveManager - handles wave initialisation and completion
    /// 需要修改这个类,把编辑生成的Wave修改成有规律的配置Wave,更加自动化,减少配置的工作量。
    /// </summary>
    public class WaveManager : MonoBehaviour
    {
        protected static int BOSS_AGGEN_IDX = 6; // BOSS数据的索引.
 
        // 刷怪的速度缩放和怪物的移动速度数据缩放
        public static float SPAWNMONSTER_TIMESCALE = 1.0f;
        public static float MONSTERMOVE_TIMESCALE = 1.0f;
 
        /// <summary>
        /// 多个开始Node,每一个对应一个EndNode,根据当前位置是否放置塔防而确定是否出兵。
        /// </summary>
        public List<Node> startNodeList;
        /// <summary>
        /// 游戏开始时各兵线是否开启.
        /// </summary>
        public List<bool> waveInitStart;
 
        /// <summary>
        /// WaveMgr内Boss刷新的数据列表.
        /// </summary>
        protected List<BossSpawnData> bossSpawnList;
 
        /// <summary>
        /// 刷怪管理器内普通怪物的血量逻辑。暂时每10秒 + 100.
        /// </summary>
        public float initAgentHealth = 100;
 
        /// <summary>
        /// Current wave being used
        /// </summary>
        protected int m_CurrentIndex;
 
        /// <summary>
        /// Whether the WaveManager starts waves on Awake - defaulted to null since the LevelManager should call function
        /// </summary>
        public bool startWavesOnAwake;
 
        /// <summary>
        /// 初始化所有的Wave,使用不同的StartNode.
        /// </summary>
        [Tooltip("Specify list in order")]
        public List<Wave> waves = new List<Wave>();
 
        /// <summary>
        /// 当前WaveManager是否开始Wave.
        /// </summary>
        protected bool bWaveStarted = false;
 
        /// <summary>
        /// 正方OR反方.
        /// </summary>
        protected bool bOpponent = false;
 
 
        /// <summary>
        /// 是否怪物已加速.
        /// </summary>
        protected bool[] bArrMSpeedUp;
 
        /// <summary>
        /// The total number of waves
        /// </summary>
        public int totalWaves
        {
            get { return waves.Count; }
        }
 
        /// <summary>
        /// Called when all waves are finished
        /// </summary>
        public event Action spawningCompleted;
 
        /// <summary>
        /// 开始更新后的时间和刷怪管理器对应的普通怪血量。
        /// </summary>
        protected float eTimeUpdate = 0;
        protected float commonAgentHealth = 0;
 
        /// <summary>
        /// Starts the waves
        /// </summary>
        public virtual void StartWaves()
        {
            if (waves.Count > 0)
            {
                InitCurrentWave();
                bWaveStarted = true;
 
                eTimeUpdate = 0;
                commonAgentHealth = initAgentHealth;
 
                initBossSpawnData();
            }
            else
            {
                Debug.LogWarning("[LEVEL] No Waves on wave manager. Calling spawningCompleted");
                SafelyCallSpawningCompleted();
            }
        }
 
        /// <summary>
        /// 从配置文件初始化Boss发布的数据。
        /// </summary>
        protected void initBossSpawnData()
        {
            List<boss> listBoss = JsonDataCenter.GetList<boss>();
            bossSpawnList = new List<BossSpawnData>();
 
            for (int ti = 0; ti < listBoss.Count; ti++)
            {
                BossSpawnData tbsd = new BossSpawnData();
                boss tboss = listBoss[ti];
                tbsd.time = tboss.time / 1000;
                tbsd.hp = tboss.hp;
                tbsd.speed = tboss.speed;
                tbsd.waveline = tboss.way - 1;
                tbsd.gold = tboss.gold;
                bossSpawnList.Add(tbsd);
            }
 
            bOpponent = startNodeList[0].bOpponentSide;
 
            // 处理MonsterSpeedUp数据
            SPAWNMONSTER_TIMESCALE = 1.0f;
            MONSTERMOVE_TIMESCALE = 1.0f;
            List<MonsterSpeedUp> listms = JsonDataCenter.monSpeedUp;
            bArrMSpeedUp = new bool[listms.Count];
            for (int ti = 0; ti < bArrMSpeedUp.Length; ti++)
                bArrMSpeedUp[ti] = false;
        }
 
 
        /// <summary>
        /// 处理怪物的SpeedUp数据
        /// </summary>
        protected void UpdateMonsterSpeedUp()
        {
            float gstartTime = UIStart.instance.gameStartTime;
            List<MonsterSpeedUp> listms = JsonDataCenter.monSpeedUp;
            float mbSpeed = MONSTERMOVE_TIMESCALE;
            for (int ti = 0; ti < bArrMSpeedUp.Length; ti++)
            {
                if (bArrMSpeedUp[ti]) continue;
 
                bool bset = false;
                /*if (ti == 0)
                    bset = (gstartTime >= 10);
                else if( ti == 1 )
                    bset = (gstartTime >= 20);
                else */
                bset = gstartTime >= listms[ti].speedTime;
 
                if (bset)
                {
                    SPAWNMONSTER_TIMESCALE = listms[ti].spawnTimeScale;
                    MONSTERMOVE_TIMESCALE = listms[ti].moveSpeedScale;
                    bArrMSpeedUp[ti] = true;
                    if (ti == 0)
                        UIStart.instance.SetFirstSpeedUp();
                    else if (ti == 1)
                        UIStart.instance.SetSecondSpeedUp();
 
                    float fscale = MONSTERMOVE_TIMESCALE / mbSpeed;
                    AgentInsManager.instance.ScaleAgentSpeed(fscale);
                    break;
                }
            }
        }
 
        /// <summary>
        /// 处理WaveMgr内Boss的生成效果
        /// </summary>
        protected void UpdateBossSpawn()
        {
            // 没有Boss可发布了
            if (bossSpawnList.Count <= 0) return;
 
            float gstartTime = UIStart.instance.gameStartTime;
 
            for (int ti = bossSpawnList.Count - 1; ti >= 0; ti--)
            {
                BossSpawnData bd = bossSpawnList[ti];
 
 
                if (!bOpponent)
                {
                    if ((bd.time - gstartTime) <= 30)
                        WaveLineSelMgr.instance.StartCountDownBossWarning(30, bd.waveline);
                    if ((bd.time - gstartTime) < 5)
                        WaveLineSelMgr.instance.waveLineBossWarning(bd.waveline, true);
                }
 
                if (gstartTime < bd.time) continue;
 
 
                // 生成当前的Boss怪:
                waves[bd.waveline].SpawnCommonAgent(BOSS_AGGEN_IDX, bd.hp, bd.speed, bd.gold);
 
                if (!bOpponent)
                {
                    WaveLineSelMgr.instance.waveLineBossWarning(bd.waveline, false);
                    WaveLineSelMgr.instance.StopCountDownBossWarning(bd.waveline);
                }
 
                bossSpawnList.RemoveAt(ti);
            }
 
        }
 
        /// <summary>
        /// 当前刷怪管理器的更新逻辑。
        /// 1:每10秒刷新的普通怪会增加100的血量
        /// </summary>
        public void Update()
        {
            if (!bWaveStarted) return;
 
            UpdateBossSpawn();
 
            // 有一个管理器更新就可以了
            if (!bOpponent)
                UpdateMonsterSpeedUp();
 
            eTimeUpdate += Time.deltaTime;
            while (eTimeUpdate > 9.99f)
            {
                eTimeUpdate -= 9.99f;
                commonAgentHealth += 100f;
                // 设置对应的数据:
                waves[m_CurrentIndex].commonAgentHealth = commonAgentHealth;
            }
 
            // 原来的代码,暂不处理,先搞多条兵线的代码操作:
            // 判断是否时间归零,如果时间归零,则对AgentInsManager做出相应的处理.
            /*            if( AgentInsManager.bBossCreateState )
                        {
                            // 当前Manager内的Wave创建Boss.如果已经创建Boss,则不处理。
                            if( !waves[m_CurrentIndex].bBossCreated)
                            {
                                bool opponent = false;
                                if (startingNode.transform.position.z > 0)
                                    opponent = true;
                                waves[m_CurrentIndex].CreateBossAgent(AgentInsManager.instance.GetCenterPos( opponent ));
                            }
                            else
                            {
                                AgentInsManager.bBossCreateState = false;
                            }
                        }*/
        }
 
        /// <summary>
        /// 从Tower的AttributeId获取到怪物的数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        protected List<int> GetMonsterDataFromAttributeId(int id)
        {
            geminfo tgem = new geminfo();
            if (JsonDataCenter.gemInfoDic.TryGetValue(id, out tgem))
                return tgem.summon;
            else
            {
                Debug.Log("找不到对应ID的Gem:" + id);
                return null;
            }
        }
 
        /// <summary>
        /// 获取当前Wave的开始位置
        /// </summary>
        /// <param name="waveline"></param>
        /// <returns></returns>
        public Vector3 GetWaveEndPos(int waveline)
        {
            return startNodeList[waveline].GetNextNode().transform.position;
        }
 
        /// <summary>
        /// 在当前的WaveManager内插入一个Agent.
        /// </summary>
        /// <param name="health"></param>  参数暂时由内部管理的血量数据接手。\
        /// WORK START: 从这里开始调试出发:
        /*public void SpawnAgent( int waveline,float hp,float speed, int attributeId )
        {
            if (waveline == 1)
                waveline = 1;
            if( bWaveStarted)
            {
                Wave wave = waves[waveline];
                List<int> summon = GetMonsterDataFromAttributeId(attributeId);
                if (summon == null) return;
 
                // 按属性需不在场景内生成小怪.
                monster tm = JsonDataCenter.monsterDic[summon[0]];
                int fid = (int)Math.Floor((attributeId - 101) / 4.0f) + 1;
                hp = tm.hp * hp;
                speed = tm.speed * speed;
                
                *//*if( summon[1] == 1 )
                    wave.SpawnCommonAgent( fid,hp,speed );
                else
                {
                    // 更多的处理
                    for( int ti = 0;ti<summon[1];ti ++)
                    {
                        wave.SpawnCommonAgent(fid,hp, speed);
                    }
                }*//*
            }
        }*/
 
        /// <summary>
        /// Inits the first wave
        /// </summary>
        protected virtual void Awake()
        {
            if (startWavesOnAwake)
            {
                StartWaves();
            }
        }
 
        /// <summary>
        /// Initialize the current wave
        /// </summary>
        protected virtual void InitCurrentWave()
        {
            for (int idx = 0; idx < waves.Count; idx++)
            {
                Wave wave = waves[idx];
                wave.commonAgentHealth = initAgentHealth;
 
                if (waveInitStart[idx])
                    wave.Init(startNodeList[idx], idx);
                else
                    wave.InitNode(startNodeList[idx], idx);
            }
        }
 
        /// <summary>
        /// 开启战场中某一条兵线
        /// </summary>
        /// <param name="x"></param>
        public bool startWaveLine(int x, int attid)
        {
            if (x >= waveInitStart.Count) return false;
            //if (waveInitStart[x]) return false;
            waveInitStart[x] = true;
            waves[x].Init(startNodeList[x], x);
            waves[x].UpdateSpawnInstruction(Wave.GetFidFromAttribute(attid));
 
            return true;
        }
 
        public void prepareWaveline(int x)
        {
            if (x >= waveInitStart.Count) return;
            waves[x].PrepareToSpawnAgent(startNodeList[x], x);
        }
 
        public void UpdateWaveSpawnTime(int x, int attributeId)
        {
            waves[x].UpdateSpawnInstruction(Wave.GetFidFromAttribute(attributeId));
        }
 
        /// <summary>
        /// 停止某一条兵线的推进。
        /// </summary>
        /// <param name="x"></param>
        /// <returns></returns>
        public bool stopWaveLine(int x)
        {
            if (x >= waveInitStart.Count) return false;
            if (!waveInitStart[x]) return false;
 
            waves[x].stopWave();
 
            return true;
        }
 
        /// <summary>
        /// Calls spawningCompleted event
        /// </summary>
        protected virtual void SafelyCallSpawningCompleted()
        {
            if (spawningCompleted != null)
            {
                spawningCompleted();
            }
        }
    }
}