Implement enemy spawning and following waypoints

This commit is contained in:
2024-08-12 14:21:11 +02:00
parent 5104b336ca
commit 3f715b2bbc
44 changed files with 1894 additions and 52 deletions

View File

@@ -0,0 +1,83 @@
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);
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: d24ff1cd925ce404c913998a333b4fbd
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: