Базовая инфраструктура

This commit is contained in:
ThomasCharester committed 2025-10-14 09:47:56 +03:00
1 parent 2e0dc15beb
commit b5e28c7abb
117 files changed
+2068 -2031

No files matched your search

@@ -0,0 +1,68 @@
using System.Collections.Generic;
using UnityEngine;
namespace Code.StateMachine
{
public abstract class AbstractStateMachine<TStateEnum, TStateMachineContext>
: MonoBehaviour
where TStateMachineContext : new()
{
public BaseState<TStateMachineContext> CurrentBaseState { get; private set; }
private BaseState<TStateMachineContext> _previousBaseState;
protected Dictionary<TStateEnum, BaseState<TStateMachineContext>> States = new();
public TStateEnum AllowedStates { get; set; }
public TStateMachineContext StateMachineContext { get; protected set; } = new();
protected virtual void Update()
{
CurrentBaseState.Tick();
}
public abstract void Initialize();
// Костыли 2 шт. для флагового енама в шаблонах
protected abstract bool CheckTransitionAllowed(TStateEnum nextState);
protected abstract void ResetAllowedTransitions();
// TODO Если в EnterState вызывается ExitState, то будет гарантированная поломка
public virtual AbstractStateMachine<TStateEnum, TStateMachineContext> SwitchState(TStateEnum nextState)
{
if (!CheckTransitionAllowed(nextState))
{
Debug.LogError("TransitionNotAllowed");
return null;
}
Debug.Log("Entered " + nextState);
ResetAllowedTransitions();
_previousBaseState = CurrentBaseState;
CurrentBaseState = States[nextState];
if (_previousBaseState != null)
{
_previousBaseState.OnExited += CurrentBaseState.EnterState;
_previousBaseState.OnExited += FinishBaseStateTransition;
_previousBaseState.ExitState();
}
else
{
CurrentBaseState.EnterState();
}
return this;
}
protected virtual void FinishBaseStateTransition()
{
_previousBaseState.OnExited -= CurrentBaseState.EnterState;
_previousBaseState.OnExited -= FinishBaseStateTransition;
_previousBaseState = null;
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 7321778c90464deabe0f2622cf7e1b7b
timeCreated: 1758650857
@@ -0,0 +1,23 @@
namespace Code.StateMachine
{
public abstract class BaseState<TStateMachineContext>
{
public delegate void Entered();
public event Entered OnEntered;
public delegate void Exited();
public event Exited OnExited;
public BaseState(TStateMachineContext context)
{
Context = context;
}
public TStateMachineContext Context { get; protected set; }
public virtual void EnterState() => OnEntered?.Invoke();
public virtual void Tick(){}
public virtual void ExitState() => OnExited?.Invoke();
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 7fac909e55264f26bb546a9a1879c151
timeCreated: 1758561340