45 lines
986 B
C#
45 lines
986 B
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public enum NodeState
|
|
{
|
|
Success, Failure, Running
|
|
}
|
|
|
|
public abstract class Node
|
|
{
|
|
protected NodeState state;
|
|
public NodeState State => state;
|
|
public abstract NodeState Evaluate();
|
|
}
|
|
|
|
public class Selector : Node
|
|
{
|
|
protected List<Node> nodes = new List<Node>(); // children nodes
|
|
|
|
public Selector(List<Node> nodes)
|
|
{
|
|
this.nodes = nodes;
|
|
}
|
|
|
|
public override NodeState Evaluate()
|
|
{
|
|
foreach (var node in nodes)
|
|
{
|
|
switch (node.Evaluate())
|
|
{
|
|
case NodeState.Failure:
|
|
continue;
|
|
case NodeState.Success:
|
|
state = NodeState.Success;
|
|
return state;
|
|
case NodeState.Running:
|
|
state = NodeState.Running;
|
|
return state;
|
|
}
|
|
}
|
|
state = NodeState.Failure;
|
|
return state;
|
|
}
|
|
}
|