行为树 1节点实现

节点基类

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;
        }
    }
 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值