Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Sketch for Entitas-CSharp Behaviour Tree implementation
// Based on http://www.gamasutra.com/blogs/ChrisSimpson/20140717/221339/Behavior_trees_for_AI_How_they_work.php
public enum NodeStatus
{
Success, Failure, Running
}
public interface IBehaviorNode
{
NodeStatus Execute(Entity entity);
}
public sealed class Sequence : IBehaviorNode
{
private IBehaviorNode[] nodes;
public Sequence(params IBehaviorNode[] nodes)
{
this.nodes = nodes;
}
public NodeStatus Execute(Entity entity)
{
var result = NodeStatus.Success;
for (int i = 0; i < nodes.Length; i++)
{
result = nodes[i].Execute(entity);
if (result == NodeStatus.Running || result == NodeStatus.Failure){
return result;
}
}
return result;
}
}
public sealed class Selector : IBehaviorNode
{
private IBehaviorNode[] nodes;
public Selector(params IBehaviorNode[] nodes)
{
this.nodes = nodes;
}
public NodeStatus Execute(Entity entity)
{
var result = NodeStatus.Success;
for (int i = 0; i < nodes.Length; i++)
{
result = nodes[i].Execute(entity);
if (result == NodeStatus.Running || result == NodeStatus.Success){
return result;
}
}
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment