Second commit
This commit is contained in:
commit
36840f3ad4
999 files changed
+61364
No files matched your search
@@ -0,0 +1,52 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using Cysharp.Threading.Tasks;
|
||||
|
||||
namespace Code.Infrastructure.StateMachine
|
||||
{
|
||||
public class AbstractStateMachine<TStateEnum, TStateMachineContext>
|
||||
where TStateMachineContext : new()
|
||||
{
|
||||
protected AbstractStateMachine(TStateMachineContext stateMachineContext)
|
||||
{
|
||||
StateMachineContext = stateMachineContext;
|
||||
}
|
||||
|
||||
public TStateEnum CurrentState { get; protected set; }
|
||||
public TStateEnum NextState { get; protected set; }
|
||||
public BaseState<TStateEnum, TStateMachineContext> CurrentBaseState { get; private set; }
|
||||
private BaseState<TStateEnum, TStateMachineContext> _previousBaseState;
|
||||
|
||||
protected Dictionary<TStateEnum, BaseState<TStateEnum, TStateMachineContext>> States = new();
|
||||
protected Dictionary<TStateEnum, Type> StatesTypes = new();
|
||||
protected TStateMachineContext StateMachineContext { get; }
|
||||
|
||||
private readonly SemaphoreSlim _stateLock = new (1, 1);
|
||||
|
||||
public virtual async UniTask SwitchState(TStateEnum nextState)
|
||||
{
|
||||
await _stateLock.WaitAsync();
|
||||
|
||||
try
|
||||
{
|
||||
NextState = nextState;
|
||||
|
||||
if (CurrentBaseState != null) await CurrentBaseState.ExitState();
|
||||
|
||||
var newState = States[nextState];
|
||||
await newState.EnterState();
|
||||
|
||||
CurrentBaseState = newState;
|
||||
CurrentState = nextState;
|
||||
NextState = default;
|
||||
|
||||
// Logger
|
||||
}
|
||||
finally
|
||||
{
|
||||
_stateLock.Release();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b58aba24383545149cd08a2a0511bf67
|
||||
timeCreated: 1785766207
|
||||
@@ -0,0 +1,19 @@
|
||||
using Cysharp.Threading.Tasks;
|
||||
|
||||
namespace Code.Infrastructure.StateMachine
|
||||
{
|
||||
public abstract class BaseState<TEnum, TStateMachineContext>
|
||||
{
|
||||
public abstract TEnum MyState { get; }
|
||||
|
||||
protected BaseState(TStateMachineContext context)
|
||||
{
|
||||
Context = context;
|
||||
}
|
||||
|
||||
protected TStateMachineContext Context { get; }
|
||||
|
||||
public virtual UniTask EnterState() => UniTask.CompletedTask;
|
||||
public virtual UniTask ExitState() => UniTask.CompletedTask;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: dbb4ded54baa4a58b4f7b77f84fd57ed
|
||||
timeCreated: 1785766328
|
||||
Reference in new issue
Block a user