1
This commit is contained in:
@@ -1,8 +1,7 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
@@ -16,111 +15,113 @@ namespace BITKit.StateMachine
|
||||
{
|
||||
private enum AsyncState
|
||||
{
|
||||
None,
|
||||
Initializing,
|
||||
InEntering,
|
||||
InExiting,
|
||||
InUpdating,
|
||||
}
|
||||
private readonly IServiceCollection _serviceCollection;
|
||||
public AsyncStateMachine(IServiceCollection serviceCollection)
|
||||
|
||||
private readonly IServiceProvider _serviceProvider;
|
||||
public AsyncStateMachine(IServiceProvider serviceProvider)
|
||||
{
|
||||
_serviceCollection = serviceCollection;
|
||||
_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> 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()
|
||||
private readonly ValidHandle _isBusy=new();
|
||||
private readonly ConcurrentQueue<(UniTask task,AsyncState state,T value)> _taskQueue=new();
|
||||
public void Initialize()
|
||||
{
|
||||
if (_currentState is not AsyncState.None)
|
||||
foreach (var (_,value) in StateDictionary)
|
||||
{
|
||||
throw new InvalidOperationException("状态机可能已初始化");
|
||||
_taskQueue.Enqueue(new (value.InitializeAsync(),AsyncState.Initializing,value));
|
||||
}
|
||||
|
||||
_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)
|
||||
public async void UpdateState(float deltaTime)
|
||||
{
|
||||
if(_cancellationTokenSource.IsCancellationRequested)return;
|
||||
switch (_currentState)
|
||||
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.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();
|
||||
}
|
||||
}
|
||||
case AsyncState.Initializing:
|
||||
task.value.Initialize();
|
||||
OnStateRegistered?.Invoke(task.value);
|
||||
break;
|
||||
case AsyncState.InEntering:
|
||||
if (_currentTask.IsCompleted)
|
||||
{
|
||||
_currentState = AsyncState.None;
|
||||
_nextState.OnStateEntry(CurrentState);
|
||||
|
||||
OnStateChanged?.Invoke(CurrentState,_nextState);
|
||||
|
||||
CurrentState = _nextState;
|
||||
_nextState = default;
|
||||
}
|
||||
_nextState.OnStateEntry(_nextState);
|
||||
OnStateChanged?.Invoke(CurrentState,_nextState);
|
||||
CurrentState = _nextState;
|
||||
break;
|
||||
case AsyncState.InUpdating:
|
||||
if (_currentTask.IsCompleted)
|
||||
{
|
||||
_currentTask = null;
|
||||
_currentState = AsyncState.None;
|
||||
}
|
||||
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 void TransitionState<State>() where State : T
|
||||
|
||||
public T TransitionState<TState>() where TState : T
|
||||
{
|
||||
throw new InvalidOperationException("动态异步状态机不支持,请使用TransitionAsync");
|
||||
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 void TransitionState(T state)
|
||||
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()
|
||||
|
@@ -56,8 +56,8 @@ namespace BITKit.StateMachine
|
||||
void Initialize();
|
||||
void UpdateState(float deltaTime);
|
||||
void DisposeState();
|
||||
void TransitionState<State>() where State : T;
|
||||
void TransitionState(T state);
|
||||
T TransitionState<TState>() where TState : T;
|
||||
T TransitionState(T state);
|
||||
void Register(T newState) => throw new NotImplementedException("未实现的接口");
|
||||
void UnRegister(T newState) => throw new NotImplementedException("未实现的接口");
|
||||
void InvokeOnStateRegistered(T state){}
|
||||
|
Reference in New Issue
Block a user