Ejemplo n.º 1
0
        // 返回 running 状态的节点
        void VisitChildren(BehaviourTreeRunner runner, BTNodeBase root)
        {
            BTNodeBase tmp = root;
            BTNodeBase child;
            BTNodeBase parent;

            while (tmp != null)
            {
                child = tmp.ChildForVisit;
                if (child == null)
                {
                    if (tmp.State == EBTTaskState.running)
                    {
                        RuntimeNode = tmp;
                        return;
                    }
                    tmp.Cleanup(runner);
                    parent = tmp == mRoot ? null : tmp.ParentNode;
                    if (parent != null)
                    {
                        RuntimeNode = parent;
                        parent.ReturnWithState(runner, tmp.State);
                    }
                    tmp = parent;
                }
                else
                {
                    RuntimeNode = child;
                    child.Visit(runner);
                    tmp = child;
                }
            }
            RuntimeNode = null;
        }
Ejemplo n.º 2
0
 public virtual void SetChild(int index, BTNodeBase node)
 {
     if (node != null)
     {
         node.ParentNode  = this;
         mChildren[index] = node;
     }
 }
Ejemplo n.º 3
0
 public override void SetChild(int index, BTNodeBase node)
 {
     base.SetChild(index, node);
     if (node != null)
     {
         mLoopers[index] = new BehaviourLooper(node);
     }
 }
Ejemplo n.º 4
0
        //public void ResetTreeState()
        //{
        //    ResetTreeStateRecursive(mRoot);
        //    IsComplate = mRoot == null;
        //}

        //void ResetTreeStateRecursive(BTNodeBase root)
        //{
        //    if (root == null)
        //        return;
        //    root.Reset();
        //    for(int i = 0; i < root.ChildLength; i++)
        //    {
        //        ResetTreeStateRecursive(root.ChildAt(i));
        //    }
        //}

        public void Abort(BehaviourTreeRunner btree)
        {
            BTNodeBase node = RuntimeNode;

            while (node != null)
            {
                node.Abort(btree);
                if (node == mRoot)
                {
                    break;
                }
                node = node.ParentNode;
            }
        }
Ejemplo n.º 5
0
            public BTNodeBase Instantiate(BehaviourTreeRunner btree, BehaviourTreeAsset asset)
            {
                BTNodeBase node = null;

                if (m_Type == EBTNodeType.task)
                {
                    node = new BTTask(m_Id, BehaviourLibrary.NewTask(m_Name, m_Id));
                }
                else if (m_Type == EBTNodeType.controller)
                {
                    node = BehaviourLibrary.NewController(m_Name, m_Id);
                }
                if (node != null)
                {
                    node.InitData(btree, m_JsonData);
                    node.InitDecoratorSize(m_Conditions == null ? 0 : m_Conditions.Length, m_Children == null ? 0 : m_Children.Length, m_Services == null ? 0 : m_Services.Length);
                    for (int i = 0; i < node.ConditionLength; i++)
                    {
                        BTData data = asset.GetDataById(m_Conditions[i]);
                        if (data == null)
                        {
                            continue;
                        }
                        node.SetNotFlag(i, data.m_NotFlag);
                        BTConditionBase cond = BehaviourLibrary.NewCondition(data.m_Name, data.m_Id);
                        if (cond != null)
                        {
                            cond.OnInitData(btree, data.m_JsonData);
                            node.SetCondition(i, cond);
                        }
                    }
                    for (int i = 0; i < node.ServiceLength; i++)
                    {
                        BTData data = asset.GetDataById(m_Services[i]);
                        if (data == null)
                        {
                            continue;
                        }
                        BTServiceBase serv = BehaviourLibrary.NewService(data.m_Name, data.m_Id);
                        if (serv != null)
                        {
                            serv.OnInitData(btree, data.m_JsonData);
                            node.SetService(i, serv);
                        }
                    }
                }
                return(node);
            }
Ejemplo n.º 6
0
 private BTNodeBase FindNodeRecursize(int nodeId, BTNodeBase root)
 {
     if (root == null || root.NodeId == nodeId)
     {
         return(root);
     }
     for (int i = 0; i < root.ChildLength; i++)
     {
         BTNodeBase node = FindNodeRecursize(nodeId, root.ChildAt(i));
         if (node != null)
         {
             return(node);
         }
     }
     return(null);
 }
Ejemplo n.º 7
0
        public override void OnInitData(BehaviourTreeRunner btree, string jsonData)
        {
            JObject obj = JsonConvert.DeserializeObject <JObject>(jsonData);

            mAssetPath = obj.Value <string>("asset");

            BehaviourTreeAsset asset = Resources.Load <BehaviourTreeAsset>(mAssetPath);

            if (!asset)
            {
                Debug.LogError("Can't load behaviour asset: " + mAssetPath);
                return;
            }
            BTNodeBase tree = asset.CreateBehaviourTree(btree);

            mLooper = new BehaviourLooper(tree);
        }
Ejemplo n.º 8
0
        BTNodeBase InstBTNode(BehaviourTreeRunner btree, BTData info)
        {
            BTNodeBase node = info.Instantiate(btree, this);

            if (node != null)
            {
                for (int i = 0; i < info.m_Children.Length; i++)
                {
                    BTData child = GetDataById(info.m_Children[i]);
                    if (child != null)
                    {
                        BTNodeBase cnode = InstBTNode(btree, child);
                        node.SetChild(i, cnode);
                    }
                }
            }
            return(node);
        }
Ejemplo n.º 9
0
 public void SetAsset(BehaviourTreeAsset behaviourAsset)
 {
     if (behaviourAsset == mAsset)
     {
         return;
     }
     mAsset = behaviourAsset;
     if (mAsset != null)
     {
         mRootNode = mAsset.CreateBehaviourTree(this);
         mLooper   = new BehaviourLooper(mRootNode);
     }
     else
     {
         mRootNode = null;
         mLooper   = null;
     }
 }
Ejemplo n.º 10
0
        /// <summary>
        /// 构造行为树
        /// </summary>
        /// <param name="runner"></param>
        /// <returns></returns>
        public BTNodeBase CreateBehaviourTree(BehaviourTreeRunner runner)
        {
            BTData root = GetDataById(m_RootNodeId);

            if (root == null)
            {
                return(null);
            }
            try
            {
                BTNodeBase rootNode = InstBTNode(runner, root);
                return(rootNode);
            }
            catch (System.Exception e)
            {
                Debug.LogError(e, this);
                return(null);
            }
        }
Ejemplo n.º 11
0
 public BehaviourLooper(BTNodeBase rootNode)
 {
     mRoot      = rootNode;
     IsComplate = mRoot == null;
 }
Ejemplo n.º 12
0
 public void NotifyBreakCallback(BTNodeBase node)
 {
     BreakNode = node;
     OnBreakCallback(this);
     Debug.Break();
 }