using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MonsterSpawners : MonoBehaviour, IUpdatable
{
[SerializeField] private Vector2 timeToSpawn;
float currentTimer = 0;
[SerializeField] private List<GameObject> enemiesToSpawn;
private List<HealthSystem> spawnedEnemies = new List<HealthSystem>();
private UpdateManager updateManager;
[HideInInspector]
public bool stop = false;
void Start()
{
ProgressController temp = transform.parent.GetComponent<ProgressController>();
if (temp)
temp.AddToUpdatables(gameObject);
else
Debug.LogError("No progress Controller, the spawner won't work");
updateManager = GameObject.Find("UpdateManager").GetComponent<UpdateManager>();
currentTimer = Random.Range(timeToSpawn.x, timeToSpawn.y);
}
public void KillAllSpawnedEnemies()
{
foreach(HealthSystem h in spawnedEnemies)
{
if (h != null) h.Die();
}
}
public void StopSpawn() {
stop= true;
}
public void Updating()
{
if (!stop)
{
currentTimer -= Time.deltaTime;
if (currentTimer <= 0)
{
GameObject t = Instantiate(enemiesToSpawn[Random.Range(0, enemiesToSpawn.Count)], transform.parent);
t.transform.position = transform.position;
IUpdatable[] u = t.GetComponents<IUpdatable>();
spawnedEnemies.Add(t.GetComponent<HealthSystem>());
foreach (IUpdatable i in u)
{
updateManager.AddToList(i);
}
currentTimer = Random.Range(timeToSpawn.x, timeToSpawn.y);
}
}
}
}