BITFALL/Assets/Artists/Scripts/Scenes/EvacuateArea.cs

70 lines
1.6 KiB
C#
Raw Normal View History

2023-10-20 19:31:12 +08:00
using System;
2023-08-27 02:58:19 +08:00
using System.Collections;
using System.Collections.Generic;
using System.Linq;
2023-10-20 19:31:12 +08:00
using System.Threading;
using System.Threading.Tasks;
2023-08-27 02:58:19 +08:00
using BITKit;
2023-10-30 01:25:53 +08:00
using BITKit.Entities;
2023-10-20 19:31:12 +08:00
using BITKit.Entities.Player;
using BITKit.Events;
using BITKit.Game;
2023-08-27 02:58:19 +08:00
using BITKit.Sensors;
2023-10-20 19:31:12 +08:00
using Cysharp.Threading.Tasks;
2023-08-27 02:58:19 +08:00
using UnityEngine;
namespace BITFALL.Scenes
{
public class EvacuateArea : MonoBehaviour,IAction
{
[SerializeReference, SubclassSelector] private ISensor sensor;
2023-10-20 19:31:12 +08:00
[SerializeReference,SubclassSelector] private IGameService gameService;
[SerializeField] private Optional<float> stopGameWhenEvacuated = new();
[SerializeField] private UnityEvent onEvacuated;
private CancellationToken _cancellationToken;
private void Start()
{
_cancellationToken = this.GetCancellationTokenOnDestroy();
}
public async void EvacuateScopedPlayer()
2023-08-27 02:58:19 +08:00
{
var players = sensor.Get()
2023-10-20 19:31:12 +08:00
.FirstOrDefault(x=> x.TryGetComponent<IEntityPlayerComponent>(out _));
if(players is null)return;
var player = players.GetComponent<IEntityPlayerComponent>();
Destroy(players.gameObject);
if (!stopGameWhenEvacuated.Allow)
{
onEvacuated?.Invoke();
return;
}
BIT4Log.Log<EvacuateArea>($"玩家已撤离,战局将在{stopGameWhenEvacuated.Value}秒后停止");
try
{
await Task.Delay(TimeSpan.FromSeconds(stopGameWhenEvacuated.Value), _cancellationToken);
}
catch (OperationCanceledException)
2023-08-27 02:58:19 +08:00
{
2023-10-20 19:31:12 +08:00
return;
2023-08-27 02:58:19 +08:00
}
2023-10-20 19:31:12 +08:00
BIT4Log.Log<EvacuateArea>($"玩家已撤离,战局已停止");
gameService.Stop();
onEvacuated?.Invoke();
2023-08-27 02:58:19 +08:00
}
void IAction.Execute() => EvacuateScopedPlayer();
}
}