Hello everyone
I am trying to convert this code:
public class StateTransition
{
public IState From;
public IState To;
public Func<bool> Condition;
public StateTransition(IState from, IState to, Func<bool> condition)
{
From = from;
To = to;
Condition = condition;
}
}
public class StateMachine
{
private List<StateTransition> stateTransitions = new List<StateTransition>();
private List<StateTransition> anyStateTransitions = new List<StateTransition>();
private IState currentState;
public IState CurrentState => currentState;
public event Action<IState> OnStateChanged;
public void AddAnyTransition(IState to, Func<bool> condition)
{
var stateTransition = new StateTransition(null, to, condition);
anyStateTransitions.Add(stateTransition);
}
public void AddTransition(IState from, IState to, Func<bool> condition)
{
var stateTransition = new StateTransition(from, to, condition);
stateTransitions.Add(stateTransition);
}
public void SetState(IState state)
{
if (currentState == state)
{
return;
}
currentState?.OnExit();
currentState = state;
//Debug.Log($"Changed to state {state}");
currentState.OnEnter();
OnStateChanged?.Invoke(currentState);
}
public void Tick()
{
if(MainManager.instance.isPlayerDead || MainManager.instance.didPlayerWin)
{
return;
}
var transition = CheckForTransition();
if(transition!=null)
{
SetState(transition.To);
}
currentState.Tick();
}
private StateTransition CheckForTransition()
{
foreach (var transition in anyStateTransitions)
{
if (transition.Condition())
{
if(transition.To!= currentState)
return transition;
}
}
foreach (var transition in stateTransitions)
{
if (transition.From == currentState && transition.Condition())
{
return transition;
}
}
return null;
}
}
into Unity ECS to optimize the code and so far I have made the following changes:
public struct TickState : IComponentData {}
public struct OnEnterState : IComponentData {}
public struct OnExitState : IComponentData {}
public struct AIStateComponent : IComponentData {
public IState state;
}
public struct CurrentStateComponent : IComponentData {
public IState state;
}
public class StateTransitionSystem : SystemBase {
protected override void OnUpdate() {
Entities
.WithAll<TickState>()
.ForEach((Entity entity, ref CurrentStateComponent currentState, in AIStateComponent) => {
stateComponent.state.Tick();
}).ScheduleParallel();
Entities
.WithAll<OnEnterState>()
.ForEach((Entity entity, ref CurrentStateComponent currentState, in AIStateComponent) => {
stateComponent.state.OnEnter();
}).ScheduleParallel();
Entities
.WithAll<OnExitState>()
.ForEach((Entity entity, ref CurrentStateComponent currentState, in AIStateComponent) => {
stateComponent.state.OnExit();
}).ScheduleParallel();
}
}
So I would like to ask for guidance and help to be sure if I am on the right track regarding the conversion and I still need to convert the following methods from the state machine class: AddAnyTransition, AddTransition, SetState, Tick and CheckForTransition