BITKit/Src/Core/StateMachine/AsyncStateMachine.cs

134 lines
4.6 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Cysharp.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
namespace BITKit.StateMachine
{
/// <summary>
/// 动态异步状态机
/// </summary>
/// <typeparam name="T">异步状态</typeparam>
public class AsyncStateMachine<T>:IStateMachine<T> ,IDisposable where T : IStateAsync
{
private enum AsyncState
{
None,
Initializing,
InEntering,
InExiting,
InUpdating,
}
private readonly IServiceCollection _serviceCollection;
public AsyncStateMachine(IServiceCollection serviceCollection)
{
_serviceCollection = serviceCollection;
}
public bool Enabled { get; set; }
public T CurrentState { get; set; }
private T _nextState;
public event Action<T, T> OnStateChanging;
public event Action<T, T> OnStateChanged;
public event Action<T> OnStateRegistered;
public event Action<T> OnStateUnRegistered;
public IDictionary<Type, T> StateDictionary { get; } = new Dictionary<Type, T>();
private AsyncState _currentState;
private Task _currentTask;
private readonly CancellationTokenSource _cancellationTokenSource=new();
public async void Initialize()
{
if (_currentState is not AsyncState.None)
{
throw new InvalidOperationException("状态机可能已初始化");
}
_currentState = AsyncState.Initializing;
foreach (var (type,value) in StateDictionary)
{
await value.InitializeAsync();
}
foreach (var (type,value) in StateDictionary)
{
value.Initialize();
}
_currentState = AsyncState.None;
}
public void UpdateState(float deltaTime)
{
if(_cancellationTokenSource.IsCancellationRequested)return;
switch (_currentState)
{
case AsyncState.None:
if (_nextState is not null)
{
_currentState = AsyncState.InEntering;
_currentTask = CurrentState.OnStateEntryAsync(CurrentState).AsTask();
OnStateChanging?.Invoke(CurrentState,_nextState);
return;
}
_currentState = AsyncState.InUpdating;
if (CurrentState is not null)
{
_currentTask = CurrentState.OnStateUpdateAsync(deltaTime).AsTask();
}
break;
case AsyncState.InExiting:
if (_currentTask.IsCompleted)
{
_currentState =_nextState is not null ? AsyncState.InEntering : AsyncState.None;
CurrentState.OnStateExit(CurrentState,_nextState);
if (_nextState is not null)
{
_currentTask = _nextState.OnStateEntryAsync(_nextState).AsTask();
}
}
break;
case AsyncState.InEntering:
if (_currentTask.IsCompleted)
{
_currentState = AsyncState.None;
_nextState.OnStateEntry(CurrentState);
OnStateChanged?.Invoke(CurrentState,_nextState);
CurrentState = _nextState;
_nextState = default;
}
break;
case AsyncState.InUpdating:
if (_currentTask.IsCompleted)
{
_currentTask = null;
_currentState = AsyncState.None;
}
break;
}
}
public void DisposeState()
{
}
public void TransitionState<State>() where State : T
{
throw new InvalidOperationException("动态异步状态机不支持,请使用TransitionAsync");
}
public void TransitionState(T state)
{
}
public void Dispose()
{
_cancellationTokenSource.Cancel();
_cancellationTokenSource.Dispose();
}
}
}