3DTD/Assets/Scripts/Manager/EnemySpawnManager.cs

69 lines
1.6 KiB
C#

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class EnemySpawnManager : MonoBehaviour
{
private float time = 0f;
[SerializeField] private LevelDefinition levelDefinition;
[SerializeField] private EnemyCollection enemyCollection;
[SerializeField] private WaypointPath groundPath;
[SerializeField] private WaypointPath skyPath;
private Queue<Wave> waveQueue;
private void Awake()
{
waveQueue = new Queue<Wave>(levelDefinition.Waves);
}
void Update()
{
if (waveQueue.Count <= 0)
{
return;
}
time += Time.deltaTime;
if (waveQueue.Peek().spawnTime < time)
{
PopWave();
}
}
void PopWave()
{
Wave spawnWave = waveQueue.Dequeue();
for (int i = 0; i < spawnWave.groups.Length; i++)
{
var group = spawnWave.groups[i];
StartCoroutine(StartSpawnGroup(group));
}
}
IEnumerator StartSpawnGroup(SpawnGroup group)
{
for (int i = 0; i < group.num; i++)
{
EnemyInfo enemyInfo = enemyCollection.Enemies[group.enemyIndex];
GameObject spawned = Instantiate(enemyInfo.prefab);
WaypointEntityData data = new WaypointEntityData(spawned.transform, enemyInfo.moveSpeed);
if (enemyInfo.FlyPath)
{
skyPath.AddObjectToPath(data);
}
else
{
groundPath.AddObjectToPath(data);
}
yield return new WaitForSecondsRealtime(group.spawnInterval);
}
}
}