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

194 lines
5.0 KiB
C#
Raw Normal View History

2024-12-09 23:24:46 +08:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
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))
{
SurvivalPool.Remove(obj);
2024-12-10 00:58:59 +08:00
DeathPool.Add(obj);
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-19 19:02:10 +08:00
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;
// ״̬<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()
{
2024-12-10 06:57:53 +08:00
//this.StartSpawning();
2024-12-09 23:24:46 +08:00
Base.GlobalObj.GetComponent<gameGlobal>().OnGamePlay += this.StartSpawning;
2024-12-13 05:40:51 +08:00
//currentWave = MengyaoInfo.Instance.m_WaveData[0].AffiliatedWaveNumber;
//GenerateQuantity = MengyaoInfo.Instance.m_WaveData[0].EnemyStatistics[0].EnemyAttributes.Count;
////GenerationIntervalOfOne = MengyaoInfo.Instance.m_WaveData[0].EnemyStatistics[0].
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
}
}
}
}
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
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-19 19:02:10 +08:00
2024-12-10 00:58:59 +08:00
}
2024-12-09 23:24:46 +08:00
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-09 23:24:46 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߱<EFBFBD><DFB1><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>յ<EFBFBD><D5B5><EFBFBD><EFBFBD><EFBFBD>
public void ReturnEnemyToPool(GameObject enemy)
{
enemy.SetActive(false); // <20><><EFBFBD>õ<EFBFBD><C3B5>˶<EFBFBD><CBB6><EFBFBD>
EnemyPool.Return(enemy); // <20><><EFBFBD><EFBFBD><EFBFBD>˻<EFBFBD><CBBB>յ<EFBFBD><D5B5><EFBFBD><EFBFBD><EFBFBD>
2024-12-10 00:58:59 +08:00
deadNumber++;
if (deadNumber>= GenerateQuantityMax)
{
2024-12-10 06:57:53 +08:00
gameGlobal.GameVictory();
2024-12-10 00:58:59 +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