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();
|
}
|
}
|
}
|