Last active
September 5, 2022 11:55
-
-
Save mzaks/3817c0e1a4e700aee4deb2607fa21eef to your computer and use it in GitHub Desktop.
Sketch for Entitas-CSharp Behaviour Tree implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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