the code is pretty much the same as Rick’s
this is the code for the wave config a ScriptableObject where you put wave information, how many enemies, which ones, and the path
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//è una sorta di contenitore di dati di configurazione, dove chiunque puo attingere
[CreateAssetMenu(menuName = "Enemy Wave Config")]
public class WaveConfig : ScriptableObject
{
[SerializeField] GameObject enemyPrefab;
[SerializeField] GameObject pathPrefab;
[SerializeField] float timeBetweenSpawns = 0.5f;
[SerializeField] float spawnRandomFactor = 0.3f;
[SerializeField] int numberOfEnemies = 5;
[SerializeField] float moveSpeed = 2f;
public GameObject GetEnemyPrefab() { return enemyPrefab; }
public List<Transform> GetWaypoints()
{
var waveWaypoints = new List<Transform>();
foreach (Transform child in pathPrefab.transform) //aggiungi la transform (posizione) di ogni "child" del path che inserisco nel field (i waypoint quindi) in wavewaypoints che è una list di tipo transform (lista di posizioni)
{
waveWaypoints.Add(child);
}
return waveWaypoints;
}
public float GettimeBetweenSpawns() {return timeBetweenSpawns; }
public float GetspawnRandomFactor() { return spawnRandomFactor; }
public int GetnumberOfEnemies() { return numberOfEnemies; }
public float GetmoveSpeed() { return moveSpeed; }
}
this is the enemy spawner one, the one where you put the waves and which permits the loop
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemySpawner : MonoBehaviour
{
[SerializeField] List<WaveConfig> waveConfigs;
[SerializeField] int startingWave = 0;
[SerializeField] bool looping = false;
// Start is called before the first frame update
IEnumerator Start() //HO CAMBIATO tipo a start in IEnumearator per renderlo una COROUTINE!
{
do
{
yield return StartCoroutine(SpawnAllWaves()); //starta la coroutine e torna (yield) solo una volta che è completata, quando hai finito controlla il bool
}
while (looping);
}
private IEnumerator SpawnAllWaves()
{
for(int waveIndex = startingWave; waveIndex < waveConfigs.Count ; waveIndex++)
{
var currentWave = waveConfigs[waveIndex];
yield return StartCoroutine(SpawnAllEnemiesInWave(currentWave)); //finchè non ha finito spawnallenemies
}
}
private IEnumerator SpawnAllEnemiesInWave(WaveConfig waveConfig)
{
for (int enemyCount = 0; enemyCount < waveConfig.GetnumberOfEnemies(); enemyCount++)
{
var newEnemy = Instantiate(
waveConfig.GetEnemyPrefab(),
waveConfig.GetWaypoints()[0].transform.position,
Quaternion.identity);
newEnemy.GetComponent<EnemyPathing>().setWaveConfig(waveConfig);
yield return new WaitForSeconds(waveConfig.GettimeBetweenSpawns());
}
}
}