This commit is contained in:
parent
faf72b05e9
commit
5d19061fab
|
@ -8,22 +8,6 @@ namespace BITKit.Entities
|
||||||
{
|
{
|
||||||
public class Entity : IEntity, IDisposable
|
public class Entity : IEntity, IDisposable
|
||||||
{
|
{
|
||||||
private class EntityServiceProvider : IServiceProvider
|
|
||||||
{
|
|
||||||
public ServiceProvider ServiceProvider;
|
|
||||||
public readonly List<Object> Services = new();
|
|
||||||
public object GetService(Type serviceType)
|
|
||||||
{
|
|
||||||
var value = ServiceProvider.GetService(serviceType);
|
|
||||||
if (value != null)
|
|
||||||
{
|
|
||||||
Services.TryAdd(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public Entity()
|
public Entity()
|
||||||
{
|
{
|
||||||
ServiceCollection.AddSingleton<IEntity>(this);
|
ServiceCollection.AddSingleton<IEntity>(this);
|
||||||
|
@ -36,26 +20,12 @@ namespace BITKit.Entities
|
||||||
public CancellationToken CancellationToken => _cancellationTokenSource.Token;
|
public CancellationToken CancellationToken => _cancellationTokenSource.Token;
|
||||||
private readonly CancellationTokenSource _cancellationTokenSource = new();
|
private readonly CancellationTokenSource _cancellationTokenSource = new();
|
||||||
|
|
||||||
public IServiceProvider ServiceProvider
|
public IServiceProvider ServiceProvider => _serviceProvider ??= ServiceCollection.BuildServiceProvider();
|
||||||
{
|
private ServiceProvider _serviceProvider;
|
||||||
get
|
|
||||||
{
|
|
||||||
if (_serviceProvider is not null)
|
|
||||||
{
|
|
||||||
return _serviceProvider;
|
|
||||||
}
|
|
||||||
|
|
||||||
var value = new EntityServiceProvider()
|
|
||||||
{
|
|
||||||
ServiceProvider = ServiceCollection.BuildServiceProvider()
|
|
||||||
};
|
|
||||||
_serviceProvider = value;
|
|
||||||
return _serviceProvider;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
public IServiceCollection ServiceCollection { get; } = new ServiceCollection();
|
public IServiceCollection ServiceCollection { get; } = new ServiceCollection();
|
||||||
private EntityServiceProvider _serviceProvider;
|
|
||||||
public object[] GetServices()=> _serviceProvider.Services.ToArray();
|
public object[] GetServices() => ServiceCollection.ToArray()
|
||||||
|
.Select(x => _serviceProvider.GetService(x.ServiceType)).ToArray();
|
||||||
public void Inject(object obj)
|
public void Inject(object obj)
|
||||||
{
|
{
|
||||||
foreach (var fieldInfo in obj.GetType().GetFields(ReflectionHelper.Flags))
|
foreach (var fieldInfo in obj.GetType().GetFields(ReflectionHelper.Flags))
|
||||||
|
@ -70,11 +40,7 @@ namespace BITKit.Entities
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
{
|
{
|
||||||
_cancellationTokenSource.Cancel();
|
_cancellationTokenSource.Cancel();
|
||||||
_serviceProvider.ServiceProvider.Dispose();
|
_serviceProvider.Dispose();
|
||||||
foreach (var x in GetServices().OfType<IDisposable>())
|
|
||||||
{
|
|
||||||
x.Dispose();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -29,7 +29,8 @@ namespace BITKit.StateMachine
|
||||||
{
|
{
|
||||||
_serviceProvider = new ServiceCollection().BuildServiceProvider();
|
_serviceProvider = new ServiceCollection().BuildServiceProvider();
|
||||||
}
|
}
|
||||||
public bool Enabled { get; set; }
|
|
||||||
|
public bool Enabled { get; set; } = true;
|
||||||
public T CurrentState { get; set; }
|
public T CurrentState { get; set; }
|
||||||
private T _nextState;
|
private T _nextState;
|
||||||
public event Action<T, T> OnStateChanging;
|
public event Action<T, T> OnStateChanging;
|
||||||
|
@ -54,7 +55,15 @@ namespace BITKit.StateMachine
|
||||||
CurrentState?.OnStateUpdate(deltaTime);
|
CurrentState?.OnStateUpdate(deltaTime);
|
||||||
if(_isBusy)return;
|
if(_isBusy)return;
|
||||||
using var _ = _isBusy.GetHandle();
|
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;
|
await task.task;
|
||||||
if(_cancellationTokenSource.IsCancellationRequested|| Enabled is false)return;
|
if(_cancellationTokenSource.IsCancellationRequested|| Enabled is false)return;
|
||||||
switch (task.state)
|
switch (task.state)
|
||||||
|
@ -64,8 +73,15 @@ namespace BITKit.StateMachine
|
||||||
OnStateRegistered?.Invoke(task.value);
|
OnStateRegistered?.Invoke(task.value);
|
||||||
break;
|
break;
|
||||||
case AsyncState.InEntering:
|
case AsyncState.InEntering:
|
||||||
_nextState.OnStateEntry(_nextState);
|
if (_nextState is not null)
|
||||||
OnStateChanged?.Invoke(CurrentState,_nextState);
|
{
|
||||||
|
_nextState.OnStateEntry(_nextState);
|
||||||
|
OnStateChanged?.Invoke(CurrentState,_nextState);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
OnStateChanged?.Invoke(CurrentState,default);
|
||||||
|
}
|
||||||
CurrentState = _nextState;
|
CurrentState = _nextState;
|
||||||
break;
|
break;
|
||||||
case AsyncState.InExiting:
|
case AsyncState.InExiting:
|
||||||
|
@ -73,11 +89,7 @@ namespace BITKit.StateMachine
|
||||||
break;
|
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>();
|
nextState = _serviceProvider.GetRequiredService<TState>();
|
||||||
_taskQueue.Enqueue(new(nextState.InitializeAsync(),AsyncState.Initializing,nextState));
|
_taskQueue.Enqueue(new(nextState.InitializeAsync(),AsyncState.Initializing,nextState));
|
||||||
TransitionState(nextState);
|
return TransitionState(nextState);;
|
||||||
|
|
||||||
return nextState;
|
|
||||||
}
|
}
|
||||||
public T TransitionState(T nextState)
|
public T TransitionState(T nextState)
|
||||||
{
|
{
|
||||||
|
@ -120,8 +130,7 @@ namespace BITKit.StateMachine
|
||||||
{
|
{
|
||||||
_taskQueue.Enqueue(new(nextState.OnStateEntryAsync(nextState),AsyncState.InEntering,nextState));
|
_taskQueue.Enqueue(new(nextState.OnStateEntryAsync(nextState),AsyncState.InEntering,nextState));
|
||||||
}
|
}
|
||||||
|
return _nextState= nextState;
|
||||||
return nextState;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
|
|
Loading…
Reference in New Issue