99 lines
3.5 KiB
C#
99 lines
3.5 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class VehicleSpawner : MonoBehaviour
|
|
{
|
|
public List<GameObject> vehiclePrefabs; // 所有可用的车辆预制体
|
|
public Transform[] spawnPoints; // 生成点
|
|
public Vector2 IntervalBetweenWave = new Vector2(6f, 10f); // 生成间隔
|
|
public int minSpawnCount = 1; // 最少生成车辆数
|
|
public int maxSpawnCount = 3; // 最多生成车辆数
|
|
|
|
public Vector2 IntervalBetweenVehicle = new Vector2(1f, 3f); // 车辆生成间隔
|
|
public float destroyAfterSeconds = 10f; // 生成多少秒后销毁
|
|
|
|
void Start()
|
|
{
|
|
StartCoroutine(SpawnCoroutine()); // 进入循环
|
|
}
|
|
|
|
private IEnumerator SpawnCoroutine()
|
|
{
|
|
while (true)
|
|
{
|
|
int spawnCount = Random.Range(minSpawnCount, maxSpawnCount + 1); // 确保包含 maxSpawnCount
|
|
List<GameObject> selectedVehicles = PickUpVehiclesToSpawn(spawnCount); // 选定本轮要生成的车辆
|
|
|
|
foreach (GameObject vehiclePrefab in selectedVehicles)
|
|
{
|
|
SpawnVehicle(vehiclePrefab);
|
|
yield return new WaitForSeconds(Random.Range(IntervalBetweenVehicle.x, IntervalBetweenVehicle.y)); // 车辆生成间隔
|
|
}
|
|
|
|
yield return new WaitForSeconds(Random.Range(IntervalBetweenWave.x, IntervalBetweenWave.y)); // 等待下一轮生成
|
|
}
|
|
}
|
|
|
|
private void SpawnVehicle(GameObject vehiclePrefab)
|
|
{
|
|
Transform spawnPoint = spawnPoints[Random.Range(0, spawnPoints.Length)]; // 随机选择生成点
|
|
GameObject obj = Instantiate(vehiclePrefab, spawnPoint.position, Quaternion.identity, this.transform);
|
|
|
|
// 添加按时间销毁组件
|
|
obj.AddComponent<DestroyByTime>().Initialize(destroyAfterSeconds);
|
|
}
|
|
|
|
private List<GameObject> PickUpVehiclesToSpawn(int num)
|
|
{
|
|
List<GameObject> availableVehicles = GetAvailableVehicles(); // 获取当前可用的车辆
|
|
List<GameObject> selectedVehicles = new List<GameObject>();
|
|
|
|
if (availableVehicles.Count == 0) return selectedVehicles; // 如果没有可用车辆,直接返回空列表
|
|
|
|
int spawnCount = Mathf.Min(num, availableVehicles.Count); // 确保不会超过可用车辆数
|
|
|
|
for (int i = 0; i < spawnCount; i++)
|
|
{
|
|
int randomIndex = Random.Range(0, availableVehicles.Count);
|
|
selectedVehicles.Add(availableVehicles[randomIndex]);
|
|
|
|
// ✅ 立即从列表中移除,确保不会重复选择
|
|
availableVehicles.RemoveAt(randomIndex);
|
|
}
|
|
|
|
return selectedVehicles;
|
|
}
|
|
|
|
private List<GameObject> GetAvailableVehicles()
|
|
{
|
|
List<GameObject> available = new List<GameObject>(vehiclePrefabs); // 复制所有车辆
|
|
GameObject[] existingVehicles = GameObject.FindGameObjectsWithTag("Vehicle"); // 获取当前场景中的车辆
|
|
|
|
// 遍历场景中已经存在的车辆
|
|
foreach (GameObject existing in existingVehicles)
|
|
{
|
|
string existingName = existing.name.Replace("(Clone)", "").Trim();
|
|
available.RemoveAll(c => c.name == existingName);
|
|
}
|
|
|
|
return available;
|
|
}
|
|
}
|
|
|
|
public class DestroyByTime : MonoBehaviour
|
|
{
|
|
private float lifetime;
|
|
|
|
public void Initialize(float seconds)
|
|
{
|
|
lifetime = seconds;
|
|
StartCoroutine(DestroyAfterTime());
|
|
}
|
|
|
|
private IEnumerator DestroyAfterTime()
|
|
{
|
|
yield return new WaitForSeconds(lifetime);
|
|
Destroy(gameObject);
|
|
}
|
|
} |