135 lines
4.6 KiB
C#
135 lines
4.6 KiB
C#
using System;
|
|
using System.Collections.Concurrent;
|
|
using System.Collections.Generic;
|
|
using System.Threading;
|
|
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
|
|
{
|
|
Initializing,
|
|
InEntering,
|
|
InExiting,
|
|
}
|
|
|
|
private readonly IServiceProvider _serviceProvider;
|
|
public AsyncStateMachine(IServiceProvider serviceProvider)
|
|
{
|
|
_serviceProvider = serviceProvider;
|
|
}
|
|
public AsyncStateMachine()
|
|
{
|
|
_serviceProvider = new ServiceCollection().BuildServiceProvider();
|
|
}
|
|
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 readonly CancellationTokenSource _cancellationTokenSource=new();
|
|
private readonly ValidHandle _isBusy=new();
|
|
private readonly ConcurrentQueue<(UniTask task,AsyncState state,T value)> _taskQueue=new();
|
|
public void Initialize()
|
|
{
|
|
foreach (var (_,value) in StateDictionary)
|
|
{
|
|
_taskQueue.Enqueue(new (value.InitializeAsync(),AsyncState.Initializing,value));
|
|
}
|
|
}
|
|
|
|
public async void UpdateState(float deltaTime)
|
|
{
|
|
if(Enabled is false)return;
|
|
CurrentState?.OnStateUpdate(deltaTime);
|
|
if(_isBusy)return;
|
|
using var _ = _isBusy.GetHandle();
|
|
if (!_taskQueue.TryDequeue(out var task)) return;
|
|
await task.task;
|
|
if(_cancellationTokenSource.IsCancellationRequested|| Enabled is false)return;
|
|
switch (task.state)
|
|
{
|
|
case AsyncState.Initializing:
|
|
task.value.Initialize();
|
|
OnStateRegistered?.Invoke(task.value);
|
|
break;
|
|
case AsyncState.InEntering:
|
|
_nextState.OnStateEntry(_nextState);
|
|
OnStateChanged?.Invoke(CurrentState,_nextState);
|
|
CurrentState = _nextState;
|
|
break;
|
|
case AsyncState.InExiting:
|
|
CurrentState?.OnStateExit(CurrentState,_nextState);
|
|
break;
|
|
}
|
|
|
|
if(Enabled is false)return;
|
|
if (CurrentState is not null)
|
|
{
|
|
await CurrentState.OnStateUpdateAsync(deltaTime);
|
|
}
|
|
|
|
}
|
|
|
|
public void DisposeState()
|
|
{
|
|
_taskQueue.Clear();
|
|
if (CurrentState is not null)
|
|
{
|
|
_taskQueue.Enqueue(new(CurrentState.OnStateExitAsync(CurrentState,null),AsyncState.InExiting,CurrentState));
|
|
}
|
|
}
|
|
|
|
public T TransitionState<TState>() where TState : T
|
|
{
|
|
T nextState;
|
|
foreach (var (type, value) in StateDictionary)
|
|
{
|
|
if (!type.IsAssignableFrom(typeof(TState))) continue;
|
|
nextState = value;
|
|
|
|
TransitionState(nextState);
|
|
return nextState;
|
|
}
|
|
nextState = _serviceProvider.GetRequiredService<TState>();
|
|
_taskQueue.Enqueue(new(nextState.InitializeAsync(),AsyncState.Initializing,nextState));
|
|
TransitionState(nextState);
|
|
|
|
return nextState;
|
|
}
|
|
public T TransitionState(T nextState)
|
|
{
|
|
if(Equals(CurrentState,nextState))return nextState;
|
|
OnStateChanging?.Invoke(CurrentState,nextState);
|
|
if (CurrentState is not null)
|
|
{
|
|
_taskQueue.Enqueue(new(CurrentState.OnStateExitAsync(CurrentState,nextState),AsyncState.InExiting,CurrentState));
|
|
}
|
|
|
|
if (nextState is not null)
|
|
{
|
|
_taskQueue.Enqueue(new(nextState.OnStateEntryAsync(nextState),AsyncState.InEntering,nextState));
|
|
}
|
|
|
|
return nextState;
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
_cancellationTokenSource.Cancel();
|
|
_cancellationTokenSource.Dispose();
|
|
}
|
|
}
|
|
|
|
}
|