Files
gpd-924/AI/Assets/Scripts/BehaviorTree/Sequence.cs
T
david d9fb5ee7bc add unity behavior tree package & examples
add start of own implementation of a behavior tree
2025-04-23 12:33:28 +02:00

42 lines
1.2 KiB
C#

using System;
namespace DefaultNamespace.BehaviorTree
{
public class Sequence : Composite
{
protected int _currentIndex;
protected Node _currentNode;
public override void Start()
{
_currentIndex = 0;
_currentNode = _currentIndex >= _nodes.Count ? null : _nodes[_currentIndex];
_currentNode?.Start();
}
public override Status Update()
{
if (_currentNode == null) return Status.Failure;
Status status = _currentNode.Update();
switch (status)
{
case Status.Success:
_currentNode.End();
_currentIndex++;
if (_currentIndex >= _nodes.Count) return Status.Success;
_currentNode = _nodes[_currentIndex];
_currentNode.Start();
return Status.Running;
case Status.Failure:
_currentNode.End();
return Status.Failure;
case Status.Running:
return Status.Running;
default:
throw new ArgumentOutOfRangeException();
}
}
}
}