using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class WaveSpawner : MonoBehaviour
{
public Transform enemyPrefab;
private float gametime;
private float initialGametime = -2f;
public int timeBetweenWaves = 1;
public Text waveCountdownText;
private int waveIndex = 0;
private int wavesSpawned = 0;
private float nextWaveSpawnTime;
void Start()
{
gametime = initialGametime;
}
private void Update()
{
GameObject spawnPoint = GameObject.Find("SpawnPoint");
nextWaveSpawnTime = Mathf.Floor(waveIndex) * timeBetweenWaves;
if (gametime > nextWaveSpawnTime)
{
waveIndex++;
if (waveIndex >= wavesSpawned)
{
StartCoroutine(SpawnWave(spawnPoint));
wavesSpawned++;
}
}
gametime += Time.deltaTime;
waveCountdownText.text = Mathf.Round(Mathf.Abs(gametime)).ToString();
}
IEnumerator SpawnWave(GameObject spawnPoint)
{
for (int i = 0; i < waveIndex; i++)
{
SpawnEnemy(spawnPoint);
yield return new WaitForSeconds(0.3f);
}
}
void SpawnEnemy(GameObject spawnPoint)
{
Instantiate(enemyPrefab, spawnPoint.position, spawnPoint.rotation);
}
}