using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyWaveManager : MonoBehaviour
{
public float timeBetweenWaves = 5f;
private float countdown = 2f;
private int waveNumber = 0;
public Transform spawnpoint;
public GameObject enemyObject;
private List<Vector2Int> pathCells;
private GameObject enemyInstance;
int nextPathCellIndex;
bool enemyRunCompleted;
// Start is called before the first frame update
void Start()
{
enemyInstance = Instantiate(enemyObject, new Vector3(0, 0.2f, 5f), Quaternion.identity );
nextPathCellIndex = 1;
enemyRunCompleted = false;
}
// Update is called once per frame
void Update()
{
if (pathCells !=null && pathCells.Count > 1 && !enemyRunCompleted)
{
Vector3 currentPos = enemyInstance.transform.position;
Vector3 nextPos = new Vector3(pathCells[nextPathCellIndex].x, 0.2f, pathCells[nextPathCellIndex].y);
enemyInstance.transform.position = Vector3.MoveTowards(currentPos, nextPos, Time.deltaTime * 2f);
if (Vector3.Distance (currentPos, nextPos) < 0.05f) {
if (nextPathCellIndex >= pathCells.Count - 1)
{
Debug.Log("Reached end");
enemyRunCompleted = true;
} else
{
nextPathCellIndex++;
}
}
}
if (countdown <= 0f)
{
StartCoroutine(SpawnWave());
countdown = timeBetweenWaves;
}
countdown -= Time.deltaTime;
IEnumerator SpawnWave ()
{
waveNumber++;
for (int i = 0; i < waveNumber; i++)
{
SpawEnemy();
yield return new WaitForSeconds(0.5f);
}
}
void SpawEnemy ()
{
enemyInstance = Instantiate(enemyObject, new Vector3(pathCells[0].x, 0.2f, pathCells[0].y), Quaternion.identity );
}
}
public void SetPathCells(List<Vector2Int> pathCells)
{
this.pathCells = pathCells;
}
}