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