Files
dungeons/Assets/Scripts/Managers/WaveManager.cs

82 lines
1.9 KiB
C#

using Sirenix.OdinInspector;
using Sirenix.Utilities;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.Events;
public class WaveManager : MonoBehaviour
{
[SerializeField]
private Level level;
[SerializeField]
[ReadOnly]
private int currentWaveIndex = -1;
private Wave currentWave {
get { return level.waves[currentWaveIndex]; }
}
[SerializeField]
[ReadOnly]
private bool waveInProgress = false;
private bool isLastWave {
get { return currentWaveIndex == level.waves.Length - 1; }
}
public UnityEvent<SpawnInformation> spawnEnemy;
[SerializeField]
[ReadOnly]
private WaypointsGroup[] waypointGroups;
[DisableInEditorMode]
[Button]
private void StartNextWave()
{
if (waypointGroups.IsNullOrEmpty())
{
Debug.LogWarning("No waypoint groups set in wave manager");
return;
}
if (waveInProgress) return;
if (isLastWave) return;
currentWaveIndex++;
waveInProgress = true;
StartCoroutine(SpawnWave());
}
private void Start()
{
waypointGroups = GetComponentsInChildren<WaypointsGroup>();
StartNextWave();
}
private IEnumerator SpawnWave()
{
foreach (var spawn in currentWave.groups)
{
for (var i = 0; i < spawn.count; i++)
{
spawnEnemy.Invoke(new(spawn.prefab, transform.position, (enemy) =>
{
var agent = enemy.GetComponent<NavMeshComponent>();
agent.MoveTo(waypointGroups.First());
}));
yield return new WaitForSeconds(spawn.timeToNext);
}
yield return new WaitForSeconds(currentWave.timeToNextGroup);
}
}
}