2021-02-26 19:01:47 +00:00
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
|
|
public class BaseState
|
|
|
|
|
{
|
|
|
|
|
public List<StateTransition> Transitions { get; } = new List<StateTransition>();
|
|
|
|
|
protected Agent _agent;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public BaseState(Agent agent)
|
|
|
|
|
{
|
|
|
|
|
_agent = agent;
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-30 15:12:41 +02:00
|
|
|
// The walk's state is the call's own: zones (and so their Minions) are built on more than one thread.
|
2021-02-26 19:01:47 +00:00
|
|
|
public void AddTransition(BaseState targetState, Func<bool> condition, Action onTransition = null, bool includeChildren = false, params BaseState[] ignoreStates)
|
|
|
|
|
{
|
|
|
|
|
Transitions.Add(new StateTransition(targetState, condition, onTransition));
|
2026-09-30 15:12:41 +02:00
|
|
|
|
2021-02-26 19:01:47 +00:00
|
|
|
// Traverse the state graph, adding every state that is reachable from this state without traversing over ignored nodes
|
|
|
|
|
if (includeChildren)
|
|
|
|
|
{
|
2026-09-30 15:12:41 +02:00
|
|
|
var visitedStates = new List<BaseState>();
|
|
|
|
|
var ignored = new HashSet<BaseState>();
|
2021-02-26 19:01:47 +00:00
|
|
|
if(ignoreStates!=null)
|
|
|
|
|
foreach (var ignoreState in ignoreStates)
|
2026-09-30 15:12:41 +02:00
|
|
|
ignored.Add(ignoreState);
|
|
|
|
|
var leafStates = new List<BaseState> { this };
|
|
|
|
|
ignored.Add(this);
|
|
|
|
|
ignored.Add(targetState);
|
|
|
|
|
while (leafStates.Count > 0)
|
2021-02-26 19:01:47 +00:00
|
|
|
{
|
2026-09-30 15:12:41 +02:00
|
|
|
var lastVisitedStates = leafStates;
|
|
|
|
|
leafStates = new List<BaseState>();
|
|
|
|
|
foreach (var state in lastVisitedStates)
|
2021-02-26 19:01:47 +00:00
|
|
|
{
|
|
|
|
|
foreach (var transition in state.Transitions)
|
|
|
|
|
{
|
2026-09-30 15:12:41 +02:00
|
|
|
if(!ignored.Contains(transition.TargetState))
|
2021-02-26 19:01:47 +00:00
|
|
|
{
|
2026-09-30 15:12:41 +02:00
|
|
|
visitedStates.Add(transition.TargetState);
|
|
|
|
|
ignored.Add(transition.TargetState);
|
|
|
|
|
leafStates.Add(transition.TargetState);
|
2021-02-26 19:01:47 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-09-30 15:12:41 +02:00
|
|
|
foreach (var state in visitedStates)
|
2021-02-26 19:01:47 +00:00
|
|
|
{
|
|
|
|
|
state.AddTransition(targetState, condition, onTransition);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual void Update(float delta){}
|
|
|
|
|
public virtual void OnEnterState(){}
|
|
|
|
|
public virtual void OnExitState(){}
|
|
|
|
|
}
|