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

122 lines
3.2 KiB
C#
Raw Normal View History

2024-04-21 05:59:29 +02:00
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;
2024-04-21 05:59:29 +02:00
public class EnemySpawnManager : MonoBehaviour
{
2024-04-21 11:10:18 +02:00
public int WaveNum = 0;
public bool HasStarted = false;
public bool IsAutoPlaying = false;
2024-04-21 05:59:29 +02:00
private float time = 0f;
[SerializeField] private LevelDefinition levelDefinition;
[SerializeField] private EnemyCollection enemyCollection;
[SerializeField] private WaypointPath groundPath;
[SerializeField] private WaypointPath skyPath;
[Header("Buttons")]
2024-04-21 06:15:42 +02:00
[SerializeField] private Button PlayButton;
[SerializeField] private Button AutoPlayButton;
2024-04-21 05:59:29 +02:00
private Queue<Wave> waveQueue;
2024-04-21 07:31:09 +02:00
private List<GameObject> SpawnedEnemies = new();
2024-04-21 05:59:29 +02:00
2024-04-21 11:10:18 +02:00
public static EnemySpawnManager Instance;
2024-04-21 05:59:29 +02:00
private void Awake()
{
2024-04-21 11:10:18 +02:00
if (Instance != null)
Destroy(Instance);
Instance = this;
2024-04-21 05:59:29 +02:00
waveQueue = new Queue<Wave>(levelDefinition.Waves);
2024-04-21 06:15:42 +02:00
if (PlayButton != null)
PlayButton.onClick.AddListener(OnPlayButtonClicked);
if (AutoPlayButton != null)
AutoPlayButton.onClick.AddListener(ToggleAutoPlayClicked);
}
2024-04-21 06:15:42 +02:00
public void OnPlayButtonClicked()
{
2024-04-21 14:28:25 +02:00
if (GameManager.Instance.CurrentNumEnemies != 0) return;
HasStarted = true;
PopWave();
}
public void ToggleAutoPlayClicked()
{
IsAutoPlaying = !IsAutoPlaying;
2024-04-21 05:59:29 +02:00
}
void Update()
{
if (HasStarted && IsAutoPlaying)
time += Time.deltaTime;
2024-04-21 05:59:29 +02:00
2024-04-21 14:28:25 +02:00
if (waveQueue.Count > 0 && (GameManager.Instance.CurrentNumEnemies == 0 && IsAutoPlaying))
2024-04-21 05:59:29 +02:00
{
PopWave();
}
2024-04-21 07:31:09 +02:00
// Removes all null from list
for (int i = 0; i < SpawnedEnemies.Count; i++)
{
if (SpawnedEnemies[i] == null)
SpawnedEnemies.Remove(SpawnedEnemies[i]);
}
// Updates the state of a round
GameManager.Instance.CurrentNumEnemies = SpawnedEnemies.Count;
2024-04-21 05:59:29 +02:00
}
void PopWave()
{
2024-04-21 14:05:58 +02:00
if (waveQueue.Count <= 0)
return;
2024-04-21 06:45:20 +02:00
time = 0;
2024-04-21 11:10:18 +02:00
WaveNum++;
2024-04-21 06:45:20 +02:00
2024-04-21 05:59:29 +02:00
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);
2024-04-21 14:04:42 +02:00
WaypointEntityData data = new WaypointEntityData(spawned.transform, enemyInfo.moveSpeed, enemyInfo.feetOffset, enemyInfo.damage);
2024-04-21 05:59:29 +02:00
if (enemyInfo.FlyPath)
{
spawned.transform.position = skyPath.Waypoints.First().position;
2024-04-21 05:59:29 +02:00
skyPath.AddObjectToPath(data);
}
else
{
spawned.transform.position = groundPath.Waypoints.First().position;
2024-04-21 05:59:29 +02:00
groundPath.AddObjectToPath(data);
}
2024-04-21 07:31:09 +02:00
SpawnedEnemies.Add(spawned);
2024-04-21 05:59:29 +02:00
yield return new WaitForSecondsRealtime(group.spawnInterval);
}
}
}