Files
aibis-dream/Assets/Scripts/SceneManagement/SpawnerKit/BatchSpawner.cs
T
2025-06-13 22:06:14 +08:00

56 lines
1.6 KiB
C#

using System.Collections;
using System.Collections.Generic;
using AibisDream.Framework;
using UnityEngine;
namespace AibisDream
{
public class BatchSpawner : Spawner
{
[SerializeField] private Vector2 batchInterval;
private bool _isSpawning;
protected override ISelector<MobileObjectData> CreateSelector(IEnumerable<MobileObjectData> datas)
{
return new BatchSelector<MobileObjectData>(datas, item => item.weight);
}
protected override void OnUpdate()
{
if (_isSpawning) return;
curInterval += Time.deltaTime;
// 按时间间隔生成物体
if (curInterval >= targetInterval)
{
Spawn();
curInterval = 0;
targetInterval = Random.Range(intervalRange.x, intervalRange.y);
}
}
protected override void Spawn()
{
StartCoroutine(SpawnCoroutine());
}
private IEnumerator SpawnCoroutine()
{
_isSpawning = true;
for (var i = 0; i < selector.Count; i++)
{
// 生成物体
var data = selector.Select();
var mobileObj = factory.CreateMobileObject(data);
mobileObjects.Add(mobileObj);
// 销毁时同样记录
mobileObj.OnDisposed += o => mobileObjects.Remove(o);
var interval = Random.Range(batchInterval.x, batchInterval.y);
yield return new WaitForSeconds(interval);
}
_isSpawning = false;
}
}
}