chenxin
2020-12-03 0bc89b87a59c3f1f394a54c0901868084463cf28
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
using UnityEngine;
using System;
using System.Collections.Generic;
using Core.Utilities;
using KTGMGemClient;
using TowerDefense.Agents;
using TowerDefense.Agents.Data;
using TowerDefense.Nodes;
 
namespace TowerDefense.Level
{
    /// <summary>
    /// A Wave is a TimedBehaviour, that uses the RepeatingTimer to spawn enemies
    /// PVE(无尽模式)使用
    /// </summary>
    public class EndlessWave : TimedBehaviour
    {
        /// <summary>
        /// 当前的Wave可以刷新出来的agent类型.
        /// </summary>
        public List<AgentConfiguration> AgentConfigurationList;
 
        /// <summary>
        /// 兵线id,从左☞右依次为1~5,Inspector面板设置的
        /// </summary>
        public int WaveLineId;
 
        /// <summary>
        /// 本赛道对应的刷怪配置数据
        /// </summary>
        private EndlessPortConfig waveData;
 
        /// <summary>
        /// 已经生成的敌人数量
        /// </summary>
        private int spawnedEnemies;
 
        /// <summary>
        /// The RepeatingTimer used to spawn enemies
        /// </summary>
        protected RepeatingTimer spawnTimer;
 
        /// <summary>
        /// The event that is fired when a Wave is completed
        /// 波次完成事件
        /// </summary>
        public event Action WaveCompleted;
 
        /// <summary>
        /// 有新的敌人生成
        /// </summary>
        public event Action SpawnNewAgent;
 
        /// <summary>
        /// 怪出生开始节点
        /// </summary>
        public Node StartingNode;
 
        /// <summary>
        /// 当前的兵线是否已经停止.
        /// </summary>
        protected bool isWaveStoped = false;
 
        /// <summary>
        /// 怪物刷新速度的缩放.
        /// </summary>
        protected float spawnTimeScale = 1.0f;
 
        /// <summary>
        /// 怪物移动速度的缩放
        /// </summary>
        protected float monsterMoveScale = 1.0f;
 
        /// <summary>
        /// 当前兵线的状态
        /// </summary>
        public EndlessWaveLineState LineState { get; set; } = EndlessWaveLineState.Wait;
 
        /// <summary>
        /// 开始新的一波
        /// </summary>
        public virtual void StartWave(EndlessPortConfig data)
        {
            LineState = EndlessWaveLineState.Spawning;
            waveData = data;
            isWaveStoped = false;
            spawnedEnemies = 0;
 
            SpawnCurrent();
            spawnTimer = new RepeatingTimer(data.Config.interval / 1000f, SpawnCurrent);
            StartTimer(spawnTimer);
        }
 
        /// <summary>
        /// 停止当前的兵线出兵
        /// </summary>
        public void StopWave()
        {
            StopTimer(spawnTimer);
            isWaveStoped = true;
        }
 
        public void PauseWave()
        {
            PauseTimer(spawnTimer);
        }
 
        public void RestartWave()
        {
            if (spawnTimer != null)
                StartTimer(spawnTimer);
        }
 
        protected virtual void SpawnCurrent()
        {
            if (isWaveStoped) return;
 
            if (!TrySetupNextSpawn())
            {
                spawnedEnemies = waveData.Config.amount;
                StopWave();
                SafelyBroadcastWaveCompletedEvent();
            }
            else
            {
                ++spawnedEnemies;
 
                if (SpawnNewAgent != null)
                    SpawnNewAgent();
 
                if (spawnedEnemies >= waveData.Config.amount)
                {
                    StopWave();
                    SafelyBroadcastWaveCompletedEvent();
                }
            }
        }
 
        /// <summary>
        /// 生成agent
        /// </summary>
        /// <returns>Agent</returns>
        protected virtual void SpawnAgent()
        {
            endless_enemy enemyData = waveData.EnemyDataList[waveData.Config.enemy_id > 0 ? 0 : spawnedEnemies];
 
            int index = GetIndexByResId(enemyData.resource);
 
            // 木属性小怪需要同时生成两个,先这么写吧(WTF)
            bool isDouble = enemyData.resource == 103;
 
            Poolable agentPoolable = Poolable.TryGetPoolable<Poolable>(AgentConfigurationList[index].agentPrefab.gameObject);
            Agent newAgent = agentPoolable.GetComponent<Agent>();
            newAgent.EnemyData = enemyData;
            newAgent.transform.position = StartingNode.transform.position;
 
            if (isDouble)
            {
                Vector3 pos = newAgent.transform.position;
                pos.x -= 2f;
                newAgent.transform.position = pos;
            }
 
            newAgent.transform.rotation = StartingNode.transform.rotation;
            newAgent.SetNode(StartingNode, WaveLineId - 1);
            newAgent.opponentAgent = false;
            newAgent.Initialize();
 
            // 最终血量 = 基础血量 * 血量增幅,速度和金币都是一个公式
            float hp = waveData.Config.b_hp * enemyData.hp_rate;
            float speed = waveData.Config.b_speed * enemyData.speed_rate;
            int gold = waveData.Config.b_coin * enemyData.coin_rate;
 
            newAgent.SetAgentData(hp, speed, gold);
            // todo 这里先填1级后面需要修改
            newAgent.healthBar.SetHealthLevel(1);
            // 加入Manager统一管理.
            AgentInsManager.instance.addAgent(newAgent);
 
            if (isDouble)
            {
                Poolable agentPoolable2 = Poolable.TryGetPoolable<Poolable>(AgentConfigurationList[index].agentPrefab.gameObject);
                Agent doubleAgent = agentPoolable2.GetComponent<Agent>();
                doubleAgent.EnemyData = enemyData;
                doubleAgent.transform.position = StartingNode.transform.position;
 
                Vector3 pos = doubleAgent.transform.position;
                pos.x += 2f;
                doubleAgent.transform.position = pos;
 
                doubleAgent.transform.rotation = StartingNode.transform.rotation;
                doubleAgent.SetNode(StartingNode, WaveLineId - 1);
                doubleAgent.opponentAgent = false;
                doubleAgent.Initialize();
 
                doubleAgent.SetAgentData(hp, speed, gold);
                doubleAgent.healthBar.SetHealthLevel(1);
                AgentInsManager.instance.addAgent(doubleAgent);
            }
        }
 
        /// <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>
        /// 尝试去生成下一波怪
        /// </summary>
        /// <returns>true if there is another spawn instruction, false if not</returns>
        protected bool TrySetupNextSpawn()
        {
            if (spawnedEnemies >= waveData.Config.amount) return false;
 
            SpawnAgent();
 
            return true;
        }
 
        /// <summary>
        /// 暂时先这么处理
        /// </summary>
        /// <param name="resId">endless_enemy表的资源id</param>
        /// <returns>所有可选的agent列表的索引</returns>
        private int GetIndexByResId(int resId)
        {
            return resId - 100;
        }
 
        /// <summary>
        /// Launch the WaveCompleted event
        /// </summary>
        protected void SafelyBroadcastWaveCompletedEvent()
        {
            LineState = EndlessWaveLineState.SpawnCompleted;
            if (WaveCompleted != null)
                WaveCompleted();
        }
    }
}