Cute_demon_attacks/meng_yao/Assets/script/A_Fight/SpawnPool.cs

243 lines
6.3 KiB
C#
Raw Normal View History

2024-12-09 23:24:46 +08:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
2024-12-24 18:02:36 +08:00
using UnityEngine.Rendering;
2024-12-09 23:24:46 +08:00
public class ObJPool<T>
{
public List<T> SurvivalPool = new List<T>();
public List<T> DeathPool = new List<T>();
// <20><>ȡһ<C8A1><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
public T Get()
{
if (DeathPool.Count > 0)
{
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD><D0B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>ת<EFBFBD>Ƶ<EFBFBD><C6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
T obj = DeathPool[0];
DeathPool.RemoveAt(0);
SurvivalPool.Add(obj);
return obj;
}
return default(T); // <20><><EFBFBD><EFBFBD>û<EFBFBD>п<EFBFBD><D0BF>ö<EFBFBD><C3B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>򷵻<EFBFBD>Ĭ<EFBFBD><C4AC>ֵ
}
// <20><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
public void Return(T obj)
{
if (SurvivalPool.Contains(obj))
{
2024-12-25 11:14:21 +08:00
2024-12-09 23:24:46 +08:00
SurvivalPool.Remove(obj);
2024-12-10 00:58:59 +08:00
DeathPool.Add(obj);
2024-12-09 23:24:46 +08:00
}
}
}
2025-01-06 10:13:44 +08:00
2024-12-09 23:24:46 +08:00
public class SpawnPool : Base
{
2024-12-09 23:48:01 +08:00
public static SpawnPool intance;
2024-12-09 23:24:46 +08:00
public ObJPool<GameObject> EnemyPool = new ObJPool<GameObject>();
2024-12-25 11:14:21 +08:00
public List<GameObject> Enemys = new List<GameObject>();
2024-12-09 23:24:46 +08:00
[Header("<22><><EFBFBD><EFBFBD>Ԥ<EFBFBD><D4A4><EFBFBD><EFBFBD>")]
public GameObject Prefab;
[Header("<22><><EFBFBD><EFBFBD>λ<EFBFBD><CEBB>")]
2024-12-10 06:57:53 +08:00
public Transform SpawnLocation;
2024-12-09 23:24:46 +08:00
[Header("һ<><D2BB><EFBFBD><EFBFBD><EFBFBD>ɼ<EFBFBD><C9BC><EFBFBD>")]
public float GenerationInterval;
[Header("һ<><D2BB><EFBFBD><EFBFBD><EFBFBD>ɼ<EFBFBD><C9BC><EFBFBD>")]
public float GenerationIntervalOfOne;
[Header("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>")]
public float GenerateQuantityMax;
[Header("ÿһ<C3BF><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>")]
public float GenerateQuantity;
2024-12-10 00:58:59 +08:00
[HideInInspector]
public int deadNumber = 0;
2024-12-27 22:59:06 +08:00
public int DeadNumber
{
get=> deadNumber;
set
{
deadNumber = value;
if (deadNumber >= GenerateQuantityMax)
{
// gameGlobal.GameVictory();
}
}
}
2024-12-10 00:58:59 +08:00
// ״̬<D7B4><CCAC><EFBFBD><EFBFBD>
private bool IsStartGet = false; // <20>Ƿ<EFBFBD><C7B7><EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>
private bool IsWave = true; // <20><>ǰ<EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɲ<EFBFBD>
private float timerOfOne = 0f; // <20><>¼ÿ<C2BC><C3BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɵ<EFBFBD>ʱ<EFBFBD><CAB1>
private float timerOfOneWave = 0f; // <20><>¼<EFBFBD><C2BC><EFBFBD>벨֮<EBB2A8><D6AE><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>
private int generatedCount = 0; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɵ<EFBFBD><C9B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
private int waveGeneratedCount = 0; // <20><>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɵ<EFBFBD><C9B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2024-12-09 23:24:46 +08:00
private int currentWave = 0; // <20><>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD>
2024-12-09 23:48:01 +08:00
private void Awake()
{
intance = this;
}
2024-12-10 00:58:59 +08:00
2024-12-09 23:24:46 +08:00
void Start()
{
Base.GlobalObj.GetComponent<gameGlobal>().OnGamePlay += this.StartSpawning;
}
2024-12-24 17:40:42 +08:00
public void UpdateNodeList()
{
2024-12-25 14:54:06 +08:00
// <20><>ȡ<EFBFBD><C8A1><EFBFBD>д<EFBFBD><D0B4><EFBFBD> SortingGroup <20><><EFBFBD><EFBFBD><EFBFBD>ĵ<EFBFBD><C4B5>˶<EFBFBD><CBB6><EFBFBD>
2024-12-24 17:40:42 +08:00
List<GameObject> gameObjectsList = new List<GameObject>();
2024-12-25 11:14:21 +08:00
foreach (GameObject go in Enemys)
2024-12-24 17:40:42 +08:00
{
2024-12-24 18:02:36 +08:00
// <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵ<EFBFBD> SortingGroup <20><><EFBFBD><EFBFBD>
2024-12-25 15:39:40 +08:00
if (go!=null)
2024-12-24 17:40:42 +08:00
{
2024-12-25 15:39:40 +08:00
SortingGroup sortingGroup = go.GetComponent<SortingGroup>();
if (sortingGroup != null && go.activeSelf)
{
// <20><><EFBFBD>ӵ<EFBFBD><D3B5>б<EFBFBD><D0B1><EFBFBD>
gameObjectsList.Add(go);
}
2024-12-24 17:40:42 +08:00
}
2024-12-25 15:39:40 +08:00
2024-12-24 17:40:42 +08:00
}
// <20><><EFBFBD><EFBFBD> y <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
gameObjectsList.Sort((a, b) => a.transform.position.y.CompareTo(b.transform.position.y));
2024-12-25 14:54:06 +08:00
// <20><><EFBFBD><EFBFBD> SortingGroup <20><><EFBFBD><EFBFBD> Canvas <20><> sortingOrder
2024-12-24 17:40:42 +08:00
for (int i = 0; i < gameObjectsList.Count; i++)
{
2024-12-24 18:02:36 +08:00
SortingGroup sortingGroup = gameObjectsList[i].GetComponent<SortingGroup>();
if (sortingGroup != null)
2024-12-24 17:40:42 +08:00
{
2024-12-25 14:54:06 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˳<EFBFBD><CBB3>
int order = gameObjectsList.Count - i + 2;
sortingGroup.sortingOrder = order;
// <20><>ȡ<EFBFBD><C8A1> Canvas <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> sortingOrder
Canvas canvas = gameObjectsList[i].GetComponentInChildren<Canvas>();
if (canvas != null)
{
canvas.sortingOrder = order;
}
2024-12-24 17:40:42 +08:00
}
}
}
2024-12-25 15:39:40 +08:00
2024-12-09 23:24:46 +08:00
2024-12-25 14:54:06 +08:00
2024-12-09 23:24:46 +08:00
void Update()
{
2024-12-10 00:58:59 +08:00
if (IsStartGet)
2024-12-09 23:24:46 +08:00
{
2024-12-10 00:58:59 +08:00
if (IsWave)
2024-12-09 23:24:46 +08:00
{
2024-12-10 00:58:59 +08:00
timerOfOne += Time.deltaTime;
if (timerOfOne >= GenerationIntervalOfOne && waveGeneratedCount < GenerateQuantity)
2024-12-09 23:24:46 +08:00
{
2024-12-10 00:58:59 +08:00
GenerateEnemy(); // <20><><EFBFBD>ɵ<EFBFBD><C9B5><EFBFBD>
timerOfOne = 0f; // <20><><EFBFBD>ü<EFBFBD>ʱ<EFBFBD><CAB1>
waveGeneratedCount++; // <20><><EFBFBD>ӵ<EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD>ĵ<EFBFBD><C4B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ѵ<EFBFBD><D1B4><EFBFBD><EFBFBD>ޣ<EFBFBD><DEA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Dz<EFBFBD><C7B2><EFBFBD><EFBFBD><EFBFBD>
if (waveGeneratedCount >= GenerateQuantity)
{
IsWave = false; // <20><>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϣ<EFBFBD><CFA3>л<EFBFBD><D0BB><EFBFBD><EFBFBD>ȴ<EFBFBD><C8B4><EFBFBD>һ<EFBFBD><D2BB>
}
2024-12-09 23:24:46 +08:00
}
}
else
{
2024-12-10 00:58:59 +08:00
timerOfOneWave += Time.deltaTime;
if (timerOfOneWave >= GenerationInterval)
2024-12-09 23:24:46 +08:00
{
2024-12-10 00:58:59 +08:00
// <20><>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϣ<EFBFBD><CFA3>ȴ<EFBFBD><C8B4><EFBFBD>һ<EFBFBD><D2BB>
currentWave++;
if (generatedCount < GenerateQuantityMax)
{
waveGeneratedCount = 0;
timerOfOneWave = 0f; // <20><><EFBFBD>ò<EFBFBD><C3B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>
IsWave = true; // <20><><EFBFBD><EFBFBD><EFBFBD>µ<EFBFBD>һ<EFBFBD><D2BB>
}
else
{
IsStartGet = false; // <20><><EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϣ<EFBFBD>ֹͣ<CDA3><D6B9><EFBFBD><EFBFBD>
}
2024-12-09 23:24:46 +08:00
}
}
}
}
2024-12-24 17:40:42 +08:00
private void FixedUpdate()
{
UpdateNodeList();
}
public void StartSpawning()
2024-12-09 23:24:46 +08:00
{
2024-12-10 06:57:53 +08:00
if (GenerateQuantity>= GenerateQuantityMax)
{
GenerateQuantity = GenerateQuantityMax;
}
2024-12-10 00:58:59 +08:00
// <20><>ʼ<EFBFBD><CABC>״̬<D7B4><CCAC><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>
IsStartGet = true;
waveGeneratedCount = 0;
2024-12-09 23:24:46 +08:00
currentWave = 0;
2024-12-10 00:58:59 +08:00
generatedCount = 0;
timerOfOne = 0f;
timerOfOneWave = 0f;
2024-12-09 23:24:46 +08:00
}
void GenerateEnemy()
{
2024-12-19 19:02:10 +08:00
2024-12-27 22:59:06 +08:00
GameObject enemy = EnemyPool.Get(); // <20><>ȡһ<C8A1><D2BB><EFBFBD><EFBFBD><EFBFBD>˶<EFBFBD><CBB6><EFBFBD>
2024-12-09 23:24:46 +08:00
2024-12-10 00:58:59 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>û<EFBFBD>п<EFBFBD><D0BF>õĶ<C3B5><C4B6>󣬴<EFBFBD><F3A3ACB4><EFBFBD><EFBFBD>µĵ<C2B5><C4B5><EFBFBD>
if (enemy == null)
{
if (Prefab == null)
2024-12-09 23:24:46 +08:00
{
2024-12-10 00:58:59 +08:00
Debug.LogError("Prefab is not assigned.");
return;
2024-12-09 23:24:46 +08:00
}
2024-12-19 19:02:10 +08:00
2024-12-10 06:57:53 +08:00
enemy = Instantiate(Prefab);
2024-12-25 11:14:21 +08:00
//EnemyPool.SurvivalPool.Add(enemy);
Enemys.Add(enemy);
2024-12-10 00:58:59 +08:00
}
2024-12-09 23:24:46 +08:00
2024-12-27 22:59:06 +08:00
//enemy.GetComponent<Role>().ResetAllStatus();
2024-12-10 00:58:59 +08:00
// <20><><EFBFBD>õ<EFBFBD><C3B5>˵<EFBFBD>λ<EFBFBD>ú<EFBFBD><C3BA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
enemy.SetActive(true); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˶<EFBFBD><CBB6><EFBFBD>
enemy.transform.position = SpawnLocation.position;
2024-12-09 23:24:46 +08:00
2024-12-10 00:58:59 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɼ<EFBFBD><C9BC><EFBFBD>
generatedCount++;
2024-12-09 23:24:46 +08:00
}
2024-12-10 00:58:59 +08:00
2024-12-30 12:13:50 +08:00
2024-12-09 23:24:46 +08:00
private void OnDisable()
{
Base.GlobalObj.GetComponent<gameGlobal>().OnGamePlay -= this.StartSpawning;
}
}
2024-12-10 00:58:59 +08:00