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
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 WaveChanged;
 
        /// <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;
        }
 
        /// <summary>
        /// Handles spawning the current agent and sets up the next agent for spawning
        /// 在场景内孵化出来一个Boss,这个核心函数最后被规则性的数据接管
        /// </summary>
        protected virtual void SpawnCurrent()
        {
            if (isWaveStoped) return;
 
            if (!TrySetupNextSpawn())
            {
                spawnedEnemies = waveData.Config.amount;
                StopWave();
                SafelyBroadcastWaveCompletedEvent();
            }
            else
                ++spawnedEnemies;
        }
 
        /// <summary>
        /// 生成agent
        /// </summary>
        /// <returns>Agent</returns>
        protected virtual Agent SpawnAgent()
        {
            int index = GetIndexByResId(waveData.EnemyData.resource);
 
            Poolable agentPoolable = Poolable.TryGetPoolable<Poolable>(AgentConfigurationList[index].agentPrefab.gameObject);
            Agent newAgent = agentPoolable.GetComponent<Agent>();
 
            newAgent.transform.position = StartingNode.transform.position;
            newAgent.transform.rotation = StartingNode.transform.rotation;
            newAgent.SetNode(StartingNode, WaveLineId - 1);
            newAgent.opponentAgent = false;
            newAgent.Initialize();
 
            // 最终血量 = 基础血量 * 血量增幅,速度和金币都是一个公式
            float hp = waveData.Config.b_hp * waveData.EnemyData.hp_rate;
            float speed = waveData.Config.b_speed * waveData.EnemyData.speed_rate;
            int gold = waveData.Config.b_coin * waveData.EnemyData.coin_rate;
 
            newAgent.SetAgentData(hp, speed, gold);
            // todo 这里先填1级后面需要修改
            newAgent.healthBar.SetHealthLevel(1);
            // 加入Manager统一管理.
            AgentInsManager.instance.addAgent(newAgent);
 
            return newAgent;
        }
 
        /// <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();
        }
    }
}