Files
2026-08-03 22:34:03 +03:00

94 lines
2.4 KiB
C#

using System.Collections;
using Unity.VisualScripting;
using UnityEngine;
public class EnemySpawner : MonoBehaviour
{
public GameObject enemyPrefab;
public float spawnDelay = 2f;
public float timeBetweenWaves = 4f;
public int enemiesPerWave = 3;
private int currentWave = 1;
public float minX = -6f;
public float maxX = 6f;
public float minY = -6f;
public float maxY = 6f; //
Coroutine spawnRoutine;
void Start()
{
Debug.Log("Spawner started");
if(spawnRoutine == null )
{
spawnRoutine = StartCoroutine(SpawnWaves()) ;
}
}
IEnumerator SpawnWaves()
{
while (true)
{
Debug.Log("Wave started: " + currentWave);
for (int i = 0; i < enemiesPerWave; i++)
{
Debug.Log("Spawning emeny");
SpawnEnemy();
//Ждём пока все враги умрут
yield return new WaitUntil(() => GameObject.FindGameObjectsWithTag("Enemy").Length == 0);
yield return new WaitForSeconds(timeBetweenWaves);
}
currentWave++;
// усложнение
enemiesPerWave += 2;
spawnDelay = Mathf.Max(0.3f, spawnDelay - 0.1f);
yield return new WaitForSeconds(timeBetweenWaves);
yield return new WaitUntil(() =>
{
int count = GameObject.FindGameObjectsWithTag("Enemy").Length;
Debug.Log("Enemies left: " + count);
return count == 0;
});
}
}
void SpawnEnemy()
{
float randomX = Random.Range(minX, maxX);
float randomY = Random.Range(minY, maxY);
Vector3 spawnPosition = new Vector3(randomX, randomY, 0f);
GameObject enemy = Instantiate(enemyPrefab, spawnPosition, Quaternion.identity);
}
void OnGUI()
{
int currentEnemyCount = GameObject.FindGameObjectsWithTag("Enemy").Length;
GUI.color = Color.black;
GUI.Box(new Rect(10, 55, 170, 40), "");
GUI.Box(new Rect(10, 100, 230, 40), "");
GUI.color = Color.cyan;
GUI.Label(new Rect(20, 63, 200, 25), "Врагов: " + currentEnemyCount);
GUI.color = Color.yellow;
GUI.Label(new Rect(20, 108, 220, 25), "Волна: " + currentWave);
}
}