initial commit
This commit is contained in:
98
Assets/Scripts/EnemySpawner.cs
Normal file
98
Assets/Scripts/EnemySpawner.cs
Normal file
@@ -0,0 +1,98 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class EnemySpawner : MonoBehaviour
|
||||
{
|
||||
[Header("Spawn Configuration")]
|
||||
[SerializeField] private GameObject[] enemyPrefabs;
|
||||
[SerializeField] private Transform[] spawnPoints;
|
||||
[SerializeField] private int totalEnemiesToSpawn = 10;
|
||||
[SerializeField] private float timeBetweenSpawns = 2.0f;
|
||||
[SerializeField] private int maxSimultaneousEnemies = 5;
|
||||
|
||||
[Header("Trigger Mode")]
|
||||
[SerializeField] private bool spawnOnStart = true;
|
||||
[SerializeField] private bool triggerOnPlayerEnter = false;
|
||||
|
||||
private int spawnedCount = 0;
|
||||
private bool isSpawning = false;
|
||||
private readonly List<EnemyController> livingEnemies = new List<EnemyController>();
|
||||
|
||||
private void Start()
|
||||
{
|
||||
if (spawnOnStart)
|
||||
{
|
||||
StartSpawning();
|
||||
}
|
||||
}
|
||||
|
||||
private void OnTriggerEnter(Collider other)
|
||||
{
|
||||
if (triggerOnPlayerEnter && !isSpawning && other.CompareTag("Player"))
|
||||
{
|
||||
StartSpawning();
|
||||
}
|
||||
}
|
||||
|
||||
public void StartSpawning()
|
||||
{
|
||||
if (!isSpawning)
|
||||
{
|
||||
isSpawning = true;
|
||||
StartCoroutine(SpawnRoutine());
|
||||
}
|
||||
}
|
||||
|
||||
private IEnumerator SpawnRoutine()
|
||||
{
|
||||
while (spawnedCount < totalEnemiesToSpawn)
|
||||
{
|
||||
// Clean up any destroyed references
|
||||
livingEnemies.RemoveAll(e => e == null || e.CurrentState == EnemyController.State.Dead);
|
||||
|
||||
if (livingEnemies.Count < maxSimultaneousEnemies)
|
||||
{
|
||||
SpawnEnemy();
|
||||
}
|
||||
|
||||
yield return new WaitForSeconds(timeBetweenSpawns);
|
||||
}
|
||||
|
||||
isSpawning = false;
|
||||
}
|
||||
|
||||
private void SpawnEnemy()
|
||||
{
|
||||
if (enemyPrefabs == null || enemyPrefabs.Length == 0) return;
|
||||
if (spawnPoints == null || spawnPoints.Length == 0) return;
|
||||
|
||||
// Choose random prefab and random spawn point
|
||||
GameObject chosenPrefab = enemyPrefabs[Random.Range(0, enemyPrefabs.Length)];
|
||||
Transform chosenPoint = spawnPoints[Random.Range(0, spawnPoints.Length)];
|
||||
|
||||
GameObject enemyObj = Instantiate(chosenPrefab, chosenPoint.position, chosenPoint.rotation);
|
||||
if (enemyObj.TryGetComponent<EnemyController>(out var enemy))
|
||||
{
|
||||
livingEnemies.Add(enemy);
|
||||
AreaClearanceManager.Instance?.RegisterEnemy(enemy);
|
||||
}
|
||||
|
||||
spawnedCount++;
|
||||
}
|
||||
|
||||
private void OnDrawGizmos()
|
||||
{
|
||||
if (spawnPoints == null) return;
|
||||
Gizmos.color = new Color(1f, 0.2f, 0.2f, 0.7f);
|
||||
foreach (var point in spawnPoints)
|
||||
{
|
||||
if (point != null)
|
||||
{
|
||||
Gizmos.DrawWireSphere(point.position, 0.5f);
|
||||
Gizmos.DrawLine(point.position, point.position + point.forward * 1.0f);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user