47 lines
1.1 KiB
C#
47 lines
1.1 KiB
C#
|
|
using System.Collections.Generic;
|
||
|
|
|
||
|
|
public class Sequence : Node
|
||
|
|
{
|
||
|
|
private List<Node> nodes = new List<Node>();
|
||
|
|
|
||
|
|
public Sequence(List<Node> nodes)
|
||
|
|
{
|
||
|
|
this.nodes = nodes;
|
||
|
|
}
|
||
|
|
|
||
|
|
public override NodeState Evaluate()
|
||
|
|
{
|
||
|
|
var isAnyChildRunning = false;
|
||
|
|
foreach (var node in nodes)
|
||
|
|
{
|
||
|
|
switch (node.Evaluate())
|
||
|
|
{
|
||
|
|
case NodeState.Failure:
|
||
|
|
state = NodeState.Failure;
|
||
|
|
return state;
|
||
|
|
case NodeState.Success:
|
||
|
|
continue;
|
||
|
|
case NodeState.Running:
|
||
|
|
isAnyChildRunning = true;
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
state = isAnyChildRunning ? NodeState.Running : NodeState.Success;
|
||
|
|
return state;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
public class TaskNode : Node
|
||
|
|
{
|
||
|
|
public delegate NodeState TaskDelegate();
|
||
|
|
private TaskDelegate action;
|
||
|
|
|
||
|
|
public TaskNode(TaskDelegate action)
|
||
|
|
{
|
||
|
|
this.action = action;
|
||
|
|
}
|
||
|
|
|
||
|
|
public override NodeState Evaluate()
|
||
|
|
{
|
||
|
|
return action();
|
||
|
|
}
|
||
|
|
}
|