242 lines
6.3 KiB
C#
242 lines
6.3 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.Rendering;
|
|
|
|
public class ObJPool<T>
|
|
{
|
|
public List<T> SurvivalPool = new List<T>();
|
|
public List<T> DeathPool = new List<T>();
|
|
|
|
// 获取一个对象
|
|
public T Get()
|
|
{
|
|
if (DeathPool.Count > 0)
|
|
{
|
|
// 如果死亡池中有对象,则从死亡池中取出并转移到生存池
|
|
T obj = DeathPool[0];
|
|
DeathPool.RemoveAt(0);
|
|
SurvivalPool.Add(obj);
|
|
return obj;
|
|
}
|
|
return default(T); // 如果没有可用对象,则返回默认值
|
|
}
|
|
|
|
// 回收一个对象
|
|
public void Return(T obj)
|
|
{
|
|
if (SurvivalPool.Contains(obj))
|
|
{
|
|
|
|
SurvivalPool.Remove(obj);
|
|
DeathPool.Add(obj);
|
|
}
|
|
}
|
|
}
|
|
|
|
public class SpawnPool : Base
|
|
{
|
|
public static SpawnPool intance;
|
|
|
|
public ObJPool<GameObject> EnemyPool = new ObJPool<GameObject>();
|
|
public List<GameObject> Enemys = new List<GameObject>();
|
|
|
|
[Header("生成预制体")]
|
|
public GameObject Prefab;
|
|
|
|
[Header("生成位置")]
|
|
public Transform SpawnLocation;
|
|
|
|
[Header("一波生成间隔")]
|
|
public float GenerationInterval;
|
|
|
|
[Header("一次生成间隔")]
|
|
public float GenerationIntervalOfOne;
|
|
|
|
[Header("生成总数量")]
|
|
public float GenerateQuantityMax;
|
|
|
|
[Header("每一波生成数量")]
|
|
public float GenerateQuantity;
|
|
|
|
[HideInInspector]
|
|
public int deadNumber = 0;
|
|
public int DeadNumber
|
|
{
|
|
get=> deadNumber;
|
|
set
|
|
{
|
|
deadNumber = value;
|
|
if (deadNumber >= GenerateQuantityMax)
|
|
{
|
|
// gameGlobal.GameVictory();
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
// 状态变量
|
|
private bool IsStartGet = false; // 是否开始生成
|
|
private bool IsWave = true; // 当前是否在生成波
|
|
private float timerOfOne = 0f; // 记录每个敌人生成的时间
|
|
private float timerOfOneWave = 0f; // 记录波与波之间的时间
|
|
private int generatedCount = 0; // 总已生成敌人数量
|
|
private int waveGeneratedCount = 0; // 当前波已生成敌人数量
|
|
private int currentWave = 0; // 当前波数
|
|
|
|
|
|
private void Awake()
|
|
{
|
|
intance = this;
|
|
}
|
|
|
|
void Start()
|
|
{
|
|
Base.GlobalObj.GetComponent<gameGlobal>().OnGamePlay += this.StartSpawning;
|
|
}
|
|
public void UpdateNodeList()
|
|
{
|
|
// 获取所有带有 SortingGroup 组件的敌人对象
|
|
List<GameObject> gameObjectsList = new List<GameObject>();
|
|
|
|
foreach (GameObject go in Enemys)
|
|
{
|
|
// 获取该物体上的 SortingGroup 组件
|
|
if (go!=null)
|
|
{
|
|
SortingGroup sortingGroup = go.GetComponent<SortingGroup>();
|
|
if (sortingGroup != null && go.activeSelf)
|
|
{
|
|
// 添加到列表中
|
|
gameObjectsList.Add(go);
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
// 按照 y 轴排序
|
|
gameObjectsList.Sort((a, b) => a.transform.position.y.CompareTo(b.transform.position.y));
|
|
|
|
// 设置 SortingGroup 和子 Canvas 的 sortingOrder
|
|
for (int i = 0; i < gameObjectsList.Count; i++)
|
|
{
|
|
SortingGroup sortingGroup = gameObjectsList[i].GetComponent<SortingGroup>();
|
|
if (sortingGroup != null)
|
|
{
|
|
// 计算排序顺序
|
|
int order = gameObjectsList.Count - i + 2;
|
|
sortingGroup.sortingOrder = order;
|
|
|
|
// 获取子 Canvas 并设置其 sortingOrder
|
|
Canvas canvas = gameObjectsList[i].GetComponentInChildren<Canvas>();
|
|
if (canvas != null)
|
|
{
|
|
canvas.sortingOrder = order;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
void Update()
|
|
{
|
|
if (IsStartGet)
|
|
{
|
|
if (IsWave)
|
|
{
|
|
timerOfOne += Time.deltaTime;
|
|
if (timerOfOne >= GenerationIntervalOfOne && waveGeneratedCount < GenerateQuantity)
|
|
{
|
|
GenerateEnemy(); // 生成敌人
|
|
timerOfOne = 0f; // 重置计时器
|
|
waveGeneratedCount++; // 增加当前波的生成数量
|
|
|
|
// 如果当前波的敌人数量已达上限,则标记波结束
|
|
if (waveGeneratedCount >= GenerateQuantity)
|
|
{
|
|
IsWave = false; // 当前波生成完毕,切换到等待下一波
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
timerOfOneWave += Time.deltaTime;
|
|
if (timerOfOneWave >= GenerationInterval)
|
|
{
|
|
// 当前波生成完毕,等待下一波
|
|
currentWave++;
|
|
if (generatedCount < GenerateQuantityMax)
|
|
{
|
|
waveGeneratedCount = 0;
|
|
timerOfOneWave = 0f; // 重置波间隔计时器
|
|
IsWave = true; // 启动新的一波
|
|
}
|
|
else
|
|
{
|
|
IsStartGet = false; // 所有敌人生成完毕,停止生成
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private void FixedUpdate()
|
|
{
|
|
UpdateNodeList();
|
|
}
|
|
public void StartSpawning()
|
|
{
|
|
if (GenerateQuantity>= GenerateQuantityMax)
|
|
{
|
|
GenerateQuantity = GenerateQuantityMax;
|
|
}
|
|
// 初始化状态,开始生成
|
|
IsStartGet = true;
|
|
waveGeneratedCount = 0;
|
|
currentWave = 0;
|
|
generatedCount = 0;
|
|
timerOfOne = 0f;
|
|
timerOfOneWave = 0f;
|
|
}
|
|
|
|
void GenerateEnemy()
|
|
{
|
|
|
|
GameObject enemy = EnemyPool.Get(); // 获取一个敌人对象
|
|
|
|
// 如果池子中没有可用的对象,创建新的敌人
|
|
if (enemy == null)
|
|
{
|
|
if (Prefab == null)
|
|
{
|
|
Debug.LogError("Prefab is not assigned.");
|
|
return;
|
|
}
|
|
|
|
enemy = Instantiate(Prefab);
|
|
//EnemyPool.SurvivalPool.Add(enemy);
|
|
Enemys.Add(enemy);
|
|
}
|
|
|
|
//enemy.GetComponent<Role>().ResetAllStatus();
|
|
// 设置敌人的位置和其他属性
|
|
enemy.SetActive(true); // 激活敌人对象
|
|
enemy.transform.position = SpawnLocation.position;
|
|
|
|
// 更新生成计数
|
|
generatedCount++;
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnDisable()
|
|
{
|
|
Base.GlobalObj.GetComponent<gameGlobal>().OnGamePlay -= this.StartSpawning;
|
|
}
|
|
}
|
|
|
|
|
|
|