AI/ActionSystem/BehaviorTree/SelectorNode.cs
namespace HC3;

/// <summary>
/// A node that selects the first child node that returns success.
/// </summary>
public class SelectorNode : Node
{
	private readonly List<Node> _children;
	private int _current;

	public SelectorNode( List<Node> children )
	{
		_children = children;
	}

	public override Status Tick()
	{
		while ( _current < _children.Count )
		{
			var result = _children[_current].TickInternal();

			if ( result == Status.Running )
				return Status.Running;

			if ( result == Status.Success )
			{
				_current = 0;
				return Status.Success;
			}

			_current++;
		}

		_current = 0;
		return Status.Failure;
	}

	public override ActionDisplayInfo? GetDisplay()
	{
		if ( _current < _children.Count )
			return _children[_current]?.GetDisplay();

		return null;
	}
}