96 lines
2.3 KiB
C#
96 lines
2.3 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class EnemySpawnManager : MonoBehaviour
|
|
{
|
|
public bool HasStarted = false;
|
|
public bool IsAutoPlaying = false;
|
|
|
|
private float time = 0f;
|
|
[SerializeField] private LevelDefinition levelDefinition;
|
|
[SerializeField] private EnemyCollection enemyCollection;
|
|
|
|
[SerializeField] private WaypointPath groundPath;
|
|
[SerializeField] private WaypointPath skyPath;
|
|
|
|
[Header("Buttons")]
|
|
|
|
[SerializeField] private Button PlayButton;
|
|
[SerializeField] private Button AutoPlayButton;
|
|
|
|
private Queue<Wave> waveQueue;
|
|
|
|
private void Awake()
|
|
{
|
|
waveQueue = new Queue<Wave>(levelDefinition.Waves);
|
|
|
|
if (PlayButton != null)
|
|
PlayButton.onClick.AddListener(OnPlayButtonClicked);
|
|
if (AutoPlayButton != null)
|
|
AutoPlayButton.onClick.AddListener(ToggleAutoPlayClicked);
|
|
}
|
|
|
|
public void OnPlayButtonClicked()
|
|
{
|
|
HasStarted = true;
|
|
time = waveQueue.Peek().spawnTime;
|
|
PopWave();
|
|
}
|
|
|
|
public void ToggleAutoPlayClicked()
|
|
{
|
|
IsAutoPlaying = !IsAutoPlaying;
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
if (waveQueue.Count <= 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (HasStarted && IsAutoPlaying)
|
|
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);
|
|
}
|
|
}
|
|
}
|