Added Behavior Graph asset for AI simulations
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user