177 lines
6.3 KiB
C#
177 lines
6.3 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Threading;
|
|
using Cysharp.Threading.Tasks;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Random = UnityEngine.Random;
|
|
|
|
namespace BITKit.StateMachine
|
|
{
|
|
/// <summary>
|
|
/// 动态异步状态机
|
|
/// </summary>
|
|
/// <typeparam name="T">异步状态</typeparam>
|
|
public class AsyncStateMachine<T>:IStateMachine<T> ,IDisposable where T : class, IStateAsync
|
|
{
|
|
private readonly IServiceProvider _serviceProvider;
|
|
public AsyncStateMachine(IServiceProvider serviceProvider)
|
|
{
|
|
_serviceProvider = serviceProvider;
|
|
}
|
|
public AsyncStateMachine()
|
|
{
|
|
_serviceProvider = new ServiceCollection().BuildServiceProvider();
|
|
}
|
|
|
|
public bool Enabled { get; set; } = true;
|
|
public T CurrentState { get;private set; }
|
|
public T NextOrCurrentState => _nextTargetState.IfNotAllow(CurrentState);
|
|
private T _nextState;
|
|
public event Action<T, T> OnStateChanging;
|
|
public event Action<T, T> OnStateChanged;
|
|
public event Action<T> OnStateRegistered;
|
|
public IReadOnlyDictionary<int, T> Dictionary => _dictionary;
|
|
public event Action<T> OnStateUnRegistered;
|
|
public IDictionary<Type, T> StateDictionary { get; } = new Dictionary<Type, T>();
|
|
public readonly ValidHandle IsBusy=new();
|
|
private readonly CancellationTokenSource _cancellationTokenSource=new();
|
|
private readonly Dictionary<int, T> _dictionary = new();
|
|
private readonly Optional<T> _nextTargetState = new();
|
|
public async void Initialize()
|
|
{
|
|
await IsBusy;
|
|
if(_cancellationTokenSource.IsCancellationRequested)return;
|
|
using var _ = IsBusy.GetHandle();
|
|
foreach (var (_,value) in StateDictionary)
|
|
{
|
|
await value.InitializeAsync();
|
|
value.Initialize();
|
|
}
|
|
}
|
|
|
|
|
|
public async void UpdateState(float deltaTime)
|
|
{
|
|
if (CurrentState is null) return;
|
|
using var _ = IsBusy.GetHandle();
|
|
await CurrentState.OnStateUpdateAsync(deltaTime);
|
|
CurrentState.OnStateUpdate(deltaTime);
|
|
}
|
|
|
|
|
|
public async void DisposeState()
|
|
{
|
|
await IsBusy;
|
|
if(_cancellationTokenSource.IsCancellationRequested)return;
|
|
if (CurrentState is null) return;
|
|
using var _ = IsBusy.GetHandle();
|
|
await CurrentState.OnStateExitAsync(CurrentState, null);
|
|
if(_cancellationTokenSource.IsCancellationRequested)return;
|
|
CurrentState.OnStateExit(CurrentState, null);
|
|
CurrentState = null;
|
|
}
|
|
|
|
public T TransitionState<TState>() where TState : T
|
|
{
|
|
T nextState;
|
|
foreach (var (type, value) in Dictionary)
|
|
{
|
|
if ((typeof(TState) == value.GetType()) is false) continue;
|
|
|
|
nextState = value;
|
|
|
|
TransitionState(nextState);
|
|
return nextState;
|
|
}
|
|
nextState = _serviceProvider.GetRequiredService<TState>();
|
|
|
|
return TransitionState(nextState);;
|
|
}
|
|
|
|
public async UniTask TransitionStateAsync(T nextState)
|
|
{
|
|
if (nextState is not null)
|
|
{
|
|
if (nextState.Identifier == 0)
|
|
{
|
|
nextState.Identifier = nextState.GetHashCode();
|
|
}
|
|
}
|
|
if (Equals(nextState, CurrentState)) return;
|
|
if(_nextTargetState.Allow && Equals(_nextTargetState.Value,nextState))return;
|
|
if (_nextTargetState.Allow)
|
|
{
|
|
_nextTargetState.Value = nextState;
|
|
return;
|
|
}
|
|
_nextTargetState.SetValueThenAllow(nextState);
|
|
await IsBusy;
|
|
if(_cancellationTokenSource.IsCancellationRequested)return;
|
|
using var _ = IsBusy.GetHandle();
|
|
OnStateChanging?.Invoke(CurrentState,nextState);
|
|
if (_dictionary.TryAdd(nextState.Identifier, nextState))
|
|
{
|
|
await nextState.InitializeAsync();
|
|
if(_cancellationTokenSource.IsCancellationRequested)return;
|
|
nextState.Initialize();
|
|
}
|
|
if (CurrentState is not null)
|
|
{
|
|
CurrentState.Enabled = false;
|
|
await CurrentState.OnStateExitAsync(CurrentState, nextState);
|
|
if(_cancellationTokenSource.IsCancellationRequested)return;
|
|
CurrentState.OnStateExit(CurrentState,nextState);
|
|
}
|
|
|
|
var tempState = CurrentState;
|
|
CurrentState = _nextTargetState.Value;
|
|
_nextTargetState.Clear();
|
|
nextState.Enabled = true;
|
|
await nextState.OnStateEntryAsync(tempState);
|
|
if(_cancellationTokenSource.IsCancellationRequested)return;
|
|
nextState.OnStateEntry(tempState);
|
|
OnStateChanged?.Invoke(tempState,nextState);
|
|
|
|
}
|
|
public T TransitionState(T nextState)
|
|
{
|
|
TransitionStateAsync(nextState).Forget();
|
|
return nextState;
|
|
}
|
|
|
|
public async void UnRegister(T newState)
|
|
{
|
|
if (newState is null) return;
|
|
if (Dictionary.ContainsKey(newState.Identifier) is false) return;
|
|
_dictionary.Remove(newState.Identifier);
|
|
|
|
if (Equals(CurrentState, newState))
|
|
{
|
|
await CurrentState.OnStateExitAsync(CurrentState, null);
|
|
CurrentState.OnStateExit(CurrentState, null);
|
|
if (CurrentState is IAsyncDisposable asyncDisposable)
|
|
{
|
|
await asyncDisposable.DisposeAsync();
|
|
}
|
|
if (CurrentState is IDisposable disposable)
|
|
{
|
|
disposable.Dispose();
|
|
}
|
|
CurrentState = null;
|
|
}
|
|
|
|
OnStateUnRegistered?.Invoke(newState);
|
|
}
|
|
public void Dispose()
|
|
{
|
|
if(_isDisposed)return;
|
|
_cancellationTokenSource.Cancel();
|
|
_cancellationTokenSource.Dispose();
|
|
_isDisposed = true;
|
|
}
|
|
|
|
private bool _isDisposed;
|
|
}
|
|
|
|
}
|