1
This commit is contained in:
@@ -29,7 +29,8 @@ namespace BITKit.StateMachine
|
||||
{
|
||||
_serviceProvider = new ServiceCollection().BuildServiceProvider();
|
||||
}
|
||||
public bool Enabled { get; set; }
|
||||
|
||||
public bool Enabled { get; set; } = true;
|
||||
public T CurrentState { get; set; }
|
||||
private T _nextState;
|
||||
public event Action<T, T> OnStateChanging;
|
||||
@@ -54,7 +55,15 @@ namespace BITKit.StateMachine
|
||||
CurrentState?.OnStateUpdate(deltaTime);
|
||||
if(_isBusy)return;
|
||||
using var _ = _isBusy.GetHandle();
|
||||
if (!_taskQueue.TryDequeue(out var task)) return;
|
||||
if (!_taskQueue.TryDequeue(out var task))
|
||||
{
|
||||
if(Enabled is false)return;
|
||||
if (CurrentState is not null)
|
||||
{
|
||||
await CurrentState.OnStateUpdateAsync(deltaTime);
|
||||
}
|
||||
return;
|
||||
}
|
||||
await task.task;
|
||||
if(_cancellationTokenSource.IsCancellationRequested|| Enabled is false)return;
|
||||
switch (task.state)
|
||||
@@ -64,8 +73,15 @@ namespace BITKit.StateMachine
|
||||
OnStateRegistered?.Invoke(task.value);
|
||||
break;
|
||||
case AsyncState.InEntering:
|
||||
_nextState.OnStateEntry(_nextState);
|
||||
OnStateChanged?.Invoke(CurrentState,_nextState);
|
||||
if (_nextState is not null)
|
||||
{
|
||||
_nextState.OnStateEntry(_nextState);
|
||||
OnStateChanged?.Invoke(CurrentState,_nextState);
|
||||
}
|
||||
else
|
||||
{
|
||||
OnStateChanged?.Invoke(CurrentState,default);
|
||||
}
|
||||
CurrentState = _nextState;
|
||||
break;
|
||||
case AsyncState.InExiting:
|
||||
@@ -73,11 +89,7 @@ namespace BITKit.StateMachine
|
||||
break;
|
||||
}
|
||||
|
||||
if(Enabled is false)return;
|
||||
if (CurrentState is not null)
|
||||
{
|
||||
await CurrentState.OnStateUpdateAsync(deltaTime);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -103,9 +115,7 @@ namespace BITKit.StateMachine
|
||||
}
|
||||
nextState = _serviceProvider.GetRequiredService<TState>();
|
||||
_taskQueue.Enqueue(new(nextState.InitializeAsync(),AsyncState.Initializing,nextState));
|
||||
TransitionState(nextState);
|
||||
|
||||
return nextState;
|
||||
return TransitionState(nextState);;
|
||||
}
|
||||
public T TransitionState(T nextState)
|
||||
{
|
||||
@@ -120,8 +130,7 @@ namespace BITKit.StateMachine
|
||||
{
|
||||
_taskQueue.Enqueue(new(nextState.OnStateEntryAsync(nextState),AsyncState.InEntering,nextState));
|
||||
}
|
||||
|
||||
return nextState;
|
||||
return _nextState= nextState;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
|
Reference in New Issue
Block a user