53 lines
1.5 KiB
C#
53 lines
1.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
|
|
namespace BehaviorTree
|
|
{
|
|
public class Selector : Composite
|
|
{
|
|
private int _currentIndex;
|
|
private Node _currentNode;
|
|
|
|
public Selector(List<Node> _nodes)
|
|
{
|
|
nodes = _nodes;
|
|
}
|
|
|
|
public override Status Start()
|
|
{
|
|
if (nodes == null || nodes.Count == 0)
|
|
{
|
|
return Status.Failure;
|
|
}
|
|
|
|
_currentIndex = 0;
|
|
_currentNode = nodes[_currentIndex];
|
|
_currentNode.Start();
|
|
|
|
return Status.Running;
|
|
}
|
|
|
|
public override Status Update()
|
|
{
|
|
Status status = _currentNode?.Update() ?? Status.Failure;
|
|
|
|
switch (status)
|
|
{
|
|
case Status.Failure:
|
|
if (_currentNode?.End() == Status.Failure) return Status.Failure;
|
|
_currentIndex++;
|
|
if (_currentIndex >= nodes.Count) return Status.Failure;
|
|
_currentNode = nodes[_currentIndex];
|
|
if (_currentNode?.Start() == Status.Failure) return Status.Failure;
|
|
return Status.Running;
|
|
case Status.Success:
|
|
_currentNode?.End();
|
|
return Status.Success;
|
|
case Status.Running:
|
|
return Status.Running;
|
|
default:
|
|
throw new ArgumentOutOfRangeException();
|
|
}
|
|
}
|
|
}
|
|
} |