69 lines
1.8 KiB
C#
69 lines
1.8 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using BITFALL.Scene;
|
|
using BITKit;
|
|
using BITKit.Entities;
|
|
using Cysharp.Threading.Tasks;
|
|
using UnityEngine;
|
|
|
|
namespace BITFALL.GameMode
|
|
{
|
|
public class BotQuotaController : MonoBehaviour
|
|
{
|
|
[BITCommand]
|
|
public static void bot_quota(int quota)
|
|
{
|
|
Data.Set(BITConstant.Environment.bot_quota, quota);
|
|
}
|
|
[SerializeField] private Entity[] initialBots;
|
|
[SerializeField] private Entity botPrefab;
|
|
[SerializeReference,SubclassSelector] private ISpawnPointService _spawnPointService;
|
|
private readonly List<Entity> _bots = new();
|
|
private void Start()
|
|
{
|
|
foreach (var x in initialBots)
|
|
{
|
|
_bots.Add(x);
|
|
}
|
|
Data.AddListener<int>(BITConstant.Environment.bot_quota, OnBotQuotaChanged,true);
|
|
destroyCancellationToken.Register(() =>
|
|
{
|
|
Data.RemoveListender<int>(BITConstant.Environment.bot_quota, OnBotQuotaChanged);
|
|
});
|
|
}
|
|
private async void OnBotQuotaChanged(int quota)
|
|
{
|
|
try
|
|
{
|
|
await UniTask.SwitchToMainThread(destroyCancellationToken);
|
|
var currentQuota=_bots.Count;
|
|
BIT4Log.Log<BotQuotaController>($"正在更改机器人配额,当前配额为{currentQuota},目标配额为{quota}");
|
|
while (currentQuota < quota)
|
|
{
|
|
Matrix4x4 spawnPoint = _spawnPointService.RequestSpawnPoint();
|
|
var instance = Instantiate(botPrefab,spawnPoint.GetPosition(),spawnPoint.rotation);
|
|
_bots.Add(instance);
|
|
BIT4Log.Log<BotQuotaController>("已添加bot");
|
|
currentQuota++;
|
|
}
|
|
while (currentQuota > quota)
|
|
{
|
|
var bot = _bots.Last();
|
|
_bots.Remove(bot);
|
|
Destroy(bot.gameObject);
|
|
BIT4Log.Log<BotQuotaController>($"已移除bot:{bot.gameObject.name}");
|
|
currentQuota--;
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
BIT4Log.LogException(e);
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|
|
|