creative-jam-20/Assets/Scripts/SpawnManager.cs

72 lines
1.7 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpawnManager : MonoBehaviour
{
public GameObject[] spawningEnemies;
public Transform[] spawnPoints;
public GameObject[] landingPoints;
public bool running = true;
float elapsedTime;
private int lastSpawn = -1;
// Start is called before the first frame update
void Start()
{
//Eventually put timer in game manager
StartCoroutine("SpawnNewEnemy");
}
private void Update()
{
elapsedTime += Time.deltaTime;
}
IEnumerator SpawnNewEnemy()
{
while (running)
{
GameObject spawned = Instantiate(GetRandomEnemy(), GetRandomSpawn().transform.position, Quaternion.identity);
spawned.GetComponent<Enemy>().SetLandingPoint(GetRandomLandingPoint().transform);
//TODO: replace 2f by function depending on elapsed time, decreasing waiting time over time
yield return new WaitForSeconds(2f);
}
}
GameObject GetRandomEnemy()
{
int index = Random.Range(0, spawningEnemies.Length);
return spawningEnemies[index];
}
GameObject GetRandomLandingPoint()
{
int index = Random.Range(0, landingPoints.Length);
return landingPoints[index];
}
/**
* Get a random spawn point in array.
* You can't get the same spawn point twice
*/
Transform GetRandomSpawn()
{
int index = Random.Range(0, spawnPoints.Length);
if (lastSpawn != -1)
{
while (index == lastSpawn)
{
index = Random.Range(0, spawnPoints.Length);
}
}
lastSpawn = index;
return spawnPoints[index];
}
}