This commit is contained in:
ThomasCharester committed 2026-02-26 11:21:27 +03:00
1 parent a73338916a
commit fbc1cc8d3e
207 files changed
+10327 -2

No files matched your search

@@ -0,0 +1,62 @@
using System;
using System.Collections.Generic;
using Code.Infrastructure.EventBus;
namespace Code.Infrastructure.StateMachine
{
public abstract class AbstractStateMachine<TStateEnum, TStateMachineContext>
where TStateMachineContext : new()
{
protected AbstractStateMachine(TStateMachineContext stateMachineContext )
{
StateMachineContext = stateMachineContext;
}
public TStateEnum CurrentState { 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();
public TStateEnum AllowedStates { get; set; }
protected TStateMachineContext StateMachineContext { get; }
// Костыли 2 шт. для флагового енама в шаблонах
protected abstract bool CheckTransitionAllowed(TStateEnum nextState);
protected abstract void ResetAllowedTransitions();
// TODO Если в EnterState вызывается ExitState, то будет гарантированная поломка
public AbstractStateMachine<TStateEnum, TStateMachineContext> SwitchState(TStateEnum nextState)
{
if (!CheckTransitionAllowed(nextState) && CurrentBaseState != null)
{
return null;
}
ResetAllowedTransitions();
_previousBaseState = CurrentBaseState;
CurrentBaseState = States[nextState];
_previousBaseState?.ExitState();
CurrentBaseState.EnterState();
CurrentState = nextState;
return this;
}
}
public interface IEnterStateHandle<TState> : IGlobalSubscriber
{
void EnterState();
}
public interface IExitStateHandle<TState> : IGlobalSubscriber
{
void ExitState();
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 8f6ce15c93464753a99851fc5d46440f
timeCreated: 1765563731
@@ -0,0 +1,37 @@
namespace Code.Infrastructure.StateMachine
{
public abstract class BaseState<TEnum, TStateMachineContext>
{
public abstract TEnum myState { get; }
protected BaseState(TStateMachineContext context, EventBus.EventBus eventBus)
{
Context = context;
EventBus = eventBus;
}
protected EventBus.EventBus EventBus;
protected TStateMachineContext Context { get; }
protected virtual void _fireEnterState()
{
}
protected virtual void _fireExitState()
{
}
protected abstract void OnEnterState();
protected abstract void OnExitState();
public void EnterState()
{
OnEnterState();
_fireEnterState();
}
public void ExitState()
{
OnExitState();
_fireExitState();
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 0c348c251b644c3c941d2bd91bbb290c
timeCreated: 1765563748