46 lines
1.4 KiB
C#
46 lines
1.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
|
|
namespace DefaultNamespace.BehaviorTree
|
|
{
|
|
public class Selector : Composite
|
|
{
|
|
private int _currentIndex;
|
|
private Node _currentNode;
|
|
|
|
public Selector(List<Node> children) : base(children)
|
|
{
|
|
}
|
|
|
|
public override Status OnStart()
|
|
{
|
|
if (Children.Count == 0) return Status.Failure;
|
|
|
|
_currentIndex = 0;
|
|
_currentNode = Children[_currentIndex];
|
|
return _currentNode?.OnStart() ?? Status.Failure;
|
|
}
|
|
|
|
public override Status OnUpdate()
|
|
{
|
|
Status status = _currentNode?.OnUpdate() ?? Status.Failure;
|
|
|
|
switch (status)
|
|
{
|
|
case Status.Failure:
|
|
_currentNode?.OnEnd();
|
|
_currentIndex++;
|
|
if (_currentIndex >= Children.Count) return Status.Failure;
|
|
_currentNode = Children[_currentIndex];
|
|
return _currentNode?.OnStart() ?? Status.Failure;
|
|
case Status.Success:
|
|
_currentNode?.OnEnd();
|
|
return Status.Success;
|
|
case Status.Running:
|
|
return Status.Running;
|
|
default:
|
|
throw new ArgumentOutOfRangeException();
|
|
}
|
|
}
|
|
}
|
|
} |