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

/// <summary>
/// Base class for behavior tree actions.
/// </summary>
public abstract class BehaviorTreeAction : AgentAction
{
	private Node _tree;
	private Node _activeNode;

	public override void StartAction()
	{
		OnTreeStart();
		_tree = BuildTree();
	}

	public override Status TickAction()
	{
		if ( _tree == null )
			return Status.Failure;

		var result = _tree.TickInternal();

		if ( result == Node.Status.Running )
			_activeNode = _tree;
		else
			_activeNode = null;

		return result switch
		{
			Node.Status.Success => Status.Success,
			Node.Status.Failure => Status.Failure,
			_ => Status.Running
		};
	}

	public override void StopAction()
	{
		OnTreeStop();
		_tree = null;
	}

	public override ActionDisplayInfo? GetDisplay()
	{
		return _activeNode?.GetDisplay();
	}

	protected abstract Node BuildTree();
	protected virtual void OnTreeStart() { }
	protected virtual void OnTreeStop() { }
}