add unity behavior package & examples
add behavior tree implementation
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user