Guest User

Untitled

a guest
May 24th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. public abstract class CompositeNode : Node {
  2. public List<Node> childs;
  3. public override NodeState Run() {
  4. // will be implement on derivered classes
  5. throw new NotImplementedException();
  6. }
  7. }
  8.  
  9. public class Sequence : Composite {
  10. public override BH_Status Run() {
  11. // no child -> then failed (sometime designer forgot to add Leaf nodes)
  12. if(childs.Count == 0) return NodeState.Failure;
  13. // evaluate childs
  14. for(int n = 0, amount = childs.Count; n < amount; n++) {
  15. var state = childs[n].Run();
  16. // break when reach any child that return Failure or Running
  17. if(state != NodeState.Success) return state;
  18. }
  19. // all child are Success, then return Success
  20. return NodeState.Success;
  21. }
  22. }
  23.  
  24. public class Selector : Composite {
  25. public override NodeState Run() {
  26. // no child -> then failed (sometime designer forgot to add Leaf node in tree)
  27. if(childs.Count == 0) return NodeState.Failure;
  28. // evaluate childs
  29. for(int n = 0, amount = childs.Count; n < amount; n++) {
  30. var state = childs[n].Run();
  31. // break when reach the first child with status = Sucess or Running (not Failure)
  32. if(state != NodeState.Failure) return state;
  33. }
  34. // no child success or running, then return failed
  35. return NodeState.Failure;
  36. }
  37. }
Add Comment
Please, Sign In to add comment