节点基类
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 节点基类
/// </summary>
[System.Serializable]
public abstract class Node {
public delegate NodeSates NodeReturn ();
protected NodeSates m_nodeState;
public NodeSates nodeState {
get { return m_nodeState; }
}
public Node () {
}
public abstract NodeSates Evaluate ();
}
public enum NodeSates
{
SUCCESS,FAILURE,RUNING
}
动作节点
/// <summary>
/// 动作节点
/// </summary>
public class ActionNode : Node {
public delegate NodeSates ActionNodeDelegate ();
private ActionNodeDelegate m_action;
public ActionNode (ActionNodeDelegate action) {
m_action = action;
}
public override NodeSates Evaluate () {
switch (m_action ()) {
case NodeSates.SUCCESS:
m_nodeState = NodeSates.SUCCESS;
return m_nodeState;
case NodeSates.FAILURE:
m_nodeState = NodeSates.FAILURE;
return m_nodeState;
case NodeSates.RUNING:
m_nodeState = NodeSates.RUNING;
return m_nodeState;
default:
m_nodeState = NodeSates.FAILURE;
return m_nodeState;
}
}
}
/// <summary>
/// 反转节点
/// </summary>
public class Inverter : Node {
private Node m_node;
public Node node {
get { return m_node; }
}
public Inverter (Node node) {
m_node = node;
}
public override NodeSates Evaluate () {
switch (m_node.Evaluate ()) {
case NodeSates.FAILURE:
m_nodeState = NodeSates.SUCCESS;
return m_nodeState;
case NodeSates.SUCCESS:
m_nodeState = NodeSates.FAILURE;
return m_nodeState;
case NodeSates.RUNING:
m_nodeState = NodeSates.RUNING;
return m_nodeState;
}
m_nodeState = NodeSates.SUCCESS;
return m_nodeState;
}
}
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 选择类型的节点
/// </summary>
public class Selector:Node
{
protected List<Node> m_nodes=new List<Node>();
public Selector (List<Node> nodes){
m_nodes=nodes;
}
public override NodeSates Evaluate(){
foreach (Node node in m_nodes)
{
switch(node.Evaluate()){
case NodeSates.FAILURE:continue;
case NodeSates.SUCCESS:m_nodeState=NodeSates.SUCCESS;return m_nodeState;
case NodeSates.RUNING:m_nodeState=NodeSates.RUNING;return m_nodeState;
default:
continue;
}
}
m_nodeState=NodeSates.FAILURE;
return m_nodeState;
}
}
using System.Collections.Generic;
/// <summary>
/// 序列节点
/// </summary>
public class Sequence:Node
{
private List<Node> m_nodes=new List<Node>();
public Sequence (List<Node> nodes){
m_nodes=nodes;
}
public override NodeSates Evaluate(){
bool antyChildRuning=false;
foreach (Node node in m_nodes)
{
switch(node.Evaluate()){
case NodeSates .FAILURE:m_nodeState=NodeSates.FAILURE;return m_nodeState;
case NodeSates.SUCCESS:continue;
case NodeSates.RUNING:antyChildRuning=true;continue;
default:
m_nodeState=NodeSates.SUCCESS;
return m_nodeState;
}
}
m_nodeState=antyChildRuning?NodeSates.RUNING:NodeSates.SUCCESS;
return m_nodeState;
}
}