This commit is contained in:
CortexCore
2024-11-08 12:52:09 +08:00
parent 4ba741408d
commit 1650126d55
27 changed files with 851 additions and 193 deletions

View File

@@ -0,0 +1,133 @@
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();
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 0c936e10927235d499efcbbf19c45f90
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,85 @@
using System;
using System.Collections.Generic;
using Cysharp.Threading.Tasks;
namespace BITKit.StateMachine
{
public struct EmptyState:IState
{
public bool Enabled { get; set; }
public void Initialize()
{
}
public void OnStateEntry(IState old)
{
}
public void OnStateUpdate(float deltaTime)
{
}
public void OnStateExit(IState old, IState newState)
{
}
}
public interface IState
{
bool Enabled { get; set; }
void Initialize();
void OnStateEntry(IState old);
void OnStateUpdate(float deltaTime);
void OnStateExit(IState old, IState newState);
}
public interface IStateAsync:IState
{
/// <summary>
/// 识别符,用于识别多个相同状态但不同用途的状态机
/// </summary>
int Identifier { get; set; }
UniTask InitializeAsync();
UniTask OnStateEntryAsync(IState old);
UniTask OnStateUpdateAsync(float deltaTime);
UniTask OnStateExitAsync(IState old, IState newState);
}
public interface IStateMachine<T> where T:IState
{
bool Enabled { get; set; }
T CurrentState { get; set; }
event Action<T,T> OnStateChanged;
event Action<T> OnStateRegistered;
event Action<T> OnStateUnRegistered;
IDictionary<Type, T> StateDictionary { get; }
void Initialize();
void UpdateState(float deltaTime);
void DisposeState();
void TransitionState<State>() where State : T;
void TransitionState(T state);
void Register(T newState) => throw new NotImplementedException("未实现的接口");
void UnRegister(T newState) => throw new NotImplementedException("未实现的接口");
void InvokeOnStateRegistered(T state){}
void InvokeOnStateUnRegistered(T state){}
}
public static class StateMachineUtils
{
public static void Register<T>(this IStateMachine<T> stateMachine, T newState) where T : IState
{
if (stateMachine.StateDictionary.ContainsKey(newState.GetType()))
{
throw new ArgumentException($"State {newState.GetType().Name} already registered");
}
stateMachine.StateDictionary.Add(newState.GetType(), newState);
newState.Initialize();
stateMachine.InvokeOnStateRegistered(newState);
}
public static void UnRegister<T>(this IStateMachine<T> stateMachine, T newState) where T : IState
{
if (!stateMachine.StateDictionary.ContainsKey(newState.GetType())) return;
stateMachine.StateDictionary.Remove(newState.GetType());
stateMachine.InvokeOnStateUnRegistered(newState);
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 2c15343777409d940bcf21398f800660
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: