1
This commit is contained in:
@@ -25,8 +25,8 @@ namespace BITKit.StateMachine
|
||||
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 Func<T, T, UniTask> OnStateChangeAsync;
|
||||
public event Action<T, T> OnStateChanged;
|
||||
public event Action<T> OnStateRegistered;
|
||||
public IReadOnlyDictionary<int, T> Dictionary => _dictionary;
|
||||
@@ -53,21 +53,18 @@ namespace BITKit.StateMachine
|
||||
{
|
||||
if (CurrentState is null) return;
|
||||
using var _ = IsBusy.GetHandle();
|
||||
await CurrentState.OnStateUpdateAsync(deltaTime);
|
||||
CurrentState.OnStateUpdate(deltaTime);
|
||||
await CurrentState.OnStateUpdateAsync(deltaTime);
|
||||
}
|
||||
|
||||
|
||||
public async void DisposeState()
|
||||
{
|
||||
await IsBusy;
|
||||
if(_cancellationTokenSource.IsCancellationRequested)return;
|
||||
if (_cancellationTokenSource.IsCancellationRequested) return;
|
||||
if (CurrentState is null) return;
|
||||
using var _ = IsBusy.GetHandle();
|
||||
if (CurrentState is not null)
|
||||
{
|
||||
CurrentState.Enabled = false;
|
||||
}
|
||||
CurrentState.Enabled = false;
|
||||
await CurrentState.OnStateExitAsync(CurrentState, null);
|
||||
CurrentState.OnStateExit(CurrentState, null);
|
||||
CurrentState = null;
|
||||
@@ -108,10 +105,15 @@ namespace BITKit.StateMachine
|
||||
}
|
||||
_nextTargetState.SetValueThenAllow(nextState);
|
||||
await IsBusy;
|
||||
|
||||
if(CurrentState==nextState)return;
|
||||
if(_cancellationTokenSource.IsCancellationRequested)return;
|
||||
using var _ = IsBusy.GetHandle();
|
||||
|
||||
OnStateChanging?.Invoke(CurrentState,nextState);
|
||||
if (_dictionary.TryAdd(nextState.Identifier, nextState))
|
||||
await OnStateChangeAsync.UniTaskFunc(CurrentState,nextState);
|
||||
|
||||
if (nextState is not null && _dictionary.TryAdd(nextState.Identifier, nextState))
|
||||
{
|
||||
await nextState.InitializeAsync();
|
||||
if(_cancellationTokenSource.IsCancellationRequested)return;
|
||||
@@ -125,6 +127,11 @@ namespace BITKit.StateMachine
|
||||
CurrentState.OnStateExit(CurrentState,nextState);
|
||||
}
|
||||
|
||||
if (_nextTargetState.Allow && _nextTargetState.Value != nextState)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var tempState = CurrentState;
|
||||
CurrentState = _nextTargetState.Value;
|
||||
_nextTargetState.Clear();
|
||||
|
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using Cysharp.Threading.Tasks;
|
||||
|
||||
@@ -44,11 +45,16 @@ namespace BITKit.StateMachine
|
||||
UniTask OnStateExitAsync(IState old, IState newState);
|
||||
}
|
||||
|
||||
public interface IStateMachine<T> where T:IState
|
||||
public interface IMicroStateMachine<T>
|
||||
{
|
||||
T CurrentState { get; }
|
||||
event Action<T, T> OnStateChanged;
|
||||
T TransitionState<TState>() where TState : T;
|
||||
T TransitionState(T state);
|
||||
}
|
||||
public interface IStateMachine<T>:IMicroStateMachine<T> where T:IState
|
||||
{
|
||||
bool Enabled { get; set; }
|
||||
T CurrentState { get; }
|
||||
event Action<T,T> OnStateChanged;
|
||||
event Action<T> OnStateRegistered;
|
||||
event Action<T> OnStateUnRegistered;
|
||||
|
||||
@@ -56,13 +62,72 @@ namespace BITKit.StateMachine
|
||||
void Initialize();
|
||||
void UpdateState(float deltaTime);
|
||||
void DisposeState();
|
||||
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){}
|
||||
void InvokeOnStateUnRegistered(T state){}
|
||||
}
|
||||
|
||||
public class MicroStateMachine<T> : IMicroStateMachine<T>
|
||||
{
|
||||
public T CurrentState { get; private set; }
|
||||
public event Action<T, T> OnStateChanged;
|
||||
private readonly ConcurrentDictionary<Type,T> _stateDictionary=new();
|
||||
public T TransitionState<TState>() where TState : T
|
||||
{
|
||||
var state = _stateDictionary.GetOrAdd(typeof(TState), Activator.CreateInstance<TState>());
|
||||
return TransitionState(state);
|
||||
}
|
||||
|
||||
public T TransitionState(T state)
|
||||
{
|
||||
_stateDictionary.TryAdd(state.GetType(), state);
|
||||
|
||||
if (Equals(state, CurrentState)) return default;
|
||||
|
||||
var currentState = CurrentState;
|
||||
CurrentState = state;
|
||||
OnStateChanged?.Invoke(currentState, state);
|
||||
return state;
|
||||
}
|
||||
}
|
||||
public abstract class StateAsync:IStateAsync
|
||||
{
|
||||
public virtual bool Enabled { get; set; }
|
||||
public virtual void Initialize()
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void OnStateEntry(IState old)
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void OnStateUpdate(float deltaTime)
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void OnStateExit(IState old, IState newState)
|
||||
{
|
||||
}
|
||||
|
||||
public virtual int Identifier { get; set; }
|
||||
public virtual UniTask InitializeAsync()
|
||||
{
|
||||
return UniTask.CompletedTask;
|
||||
}
|
||||
|
||||
public virtual UniTask OnStateEntryAsync(IState old)
|
||||
{ return UniTask.CompletedTask;
|
||||
}
|
||||
|
||||
public virtual UniTask OnStateUpdateAsync(float deltaTime)
|
||||
{ return UniTask.CompletedTask;
|
||||
}
|
||||
|
||||
public virtual UniTask OnStateExitAsync(IState old, IState newState)
|
||||
{ return UniTask.CompletedTask;
|
||||
}
|
||||
}
|
||||
public static class StateMachineUtils
|
||||
{
|
||||
public static void Register<T>(this IStateMachine<T> stateMachine, T newState) where T : IState
|
||||
|
Reference in New Issue
Block a user