Пример #1
0
 private AssetBinder(AssetBinder parent)
 {
     mRunner    = parent.mRunner;
     mAssetDeep = parent.mAssetDeep + 1;
     mLooper    = new BehaviourLooper(mRunner);
     mSubAssets = new List <AssetBinder>();
 }
Пример #2
0
 private AssetBinder(BehaviourTreeRunner runner)
 {
     mRunner    = runner;
     mAssetDeep = 1;
     mLooper    = new BehaviourLooper(runner);
     mSubAssets = new List <AssetBinder>();
 }
        public override void OnInitData(BehaviourTreeRunner btree, string jsonData)
        {
            JObject obj = JsonConvert.DeserializeObject <JObject>(jsonData);

            mBlackboardVar = obj.Value <string>("property");
            mBlackboardId  = btree.Blackboard.GetPropertyId(mBlackboardVar);
        }
Пример #4
0
 protected override void OnAbort(BehaviourTreeRunner btree)
 {
     if (mTask != null)
     {
         mTask.OnAbort(btree);
     }
 }
Пример #5
0
        public void Visit(BehaviourTreeRunner btree)
        {
#if UNITY_EDITOR
            if (BreakToggle)
            {
                btree.NotifyBreakCallback(this);
            }
#endif
            mStartTime = Time.time;
            if (IsRunnable(btree))
            {
                for (int i = 0; i < ServiceLength; i++)
                {
                    BTServiceBase serv = mServices[i];
                    if (serv != null)
                    {
                        btree.StartService(serv);
                    }
                }
                mVisited = true;
                OnVisit(btree);
            }
            else
            {
                State = EBTTaskState.faild;
            }
        }
Пример #6
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;
        }
Пример #7
0
 protected override void OnTick(BehaviourTreeRunner behaviourTree, float deltaTime)
 {
     if (mTask != null)
     {
         State = mTask.OnTaskTick(behaviourTree, deltaTime);
     }
 }
Пример #8
0
        public void Update(BehaviourTreeRunner runner, float deltaTime)
        {
            if (mRoot == null)
            {
                IsComplate = true;
                return;
            }
            if (RuntimeNode == null)
            {
                IsComplate = false;
#if UNITY_EDITOR
                runner.NotifyBehaviourTreeBegin();
#endif
                RuntimeNode = mRoot;
                RuntimeNode.Visit(runner);
            }
            else
            {
                RuntimeNode.Tick(runner, Time.deltaTime);
            }
            VisitChildren(runner, RuntimeNode);
            State = mRoot.State;
            if (RuntimeNode == null)
            {
                IsComplate = true;
            }
#if UNITY_EDITOR
            runner.NotifyBehaviourTreeFrame();
            if (IsComplate)
            {
                runner.NotifyBehaviourTreeEnd();
            }
#endif
        }
Пример #9
0
 public void Dispose()
 {
     Abort();
     Parent = null;
     mRoot  = null;
     Runner = null;
 }
Пример #10
0
 public override void OnAbort(BehaviourTreeRunner btree)
 {
     if (mLooper != null && mLooper.RuntimeNode != null)
     {
         mLooper.RuntimeNode.Abort(btree);
     }
 }
Пример #11
0
        public override void OnInitData(BehaviourTreeRunner btree, string jsonData)
        {
            JObject obj = JsonConvert.DeserializeObject <JObject>(jsonData);

            mWaitTime     = obj.Value <float>("timeForWait");
            mAlwaysSucces = obj.Value <bool>("alwaysSuccess");
        }
Пример #12
0
 public override void InitData(BehaviourTreeRunner btree, string jsonData)
 {
     if (mTask != null)
     {
         mTask.OnInitData(btree, jsonData);
     }
 }
Пример #13
0
 public override EBTTaskState OnTaskTick(BehaviourTreeRunner btree, float deltaTime)
 {
     if (mAbort)
     {
         return(mAlwaysSucces ? EBTTaskState.success : EBTTaskState.faild);
     }
     return(btree.TaskTime >= mWaitTime ? EBTTaskState.success : EBTTaskState.running);
 }
Пример #14
0
 protected override void OnVisit(BehaviourTreeRunner behaviourTree)
 {
     if (mTask != null)
     {
         State = mTask.OnTaskStart(behaviourTree);
     }
     else
     {
         State = EBTTaskState.faild;
     }
 }
Пример #15
0
 protected override void OnAbort(BehaviourTreeRunner btree)
 {
     for (int i = 0; i < mLoopers.Length; i++)
     {
         if (mLoopers[i] == null || mLoopers[i].IsComplate)
         {
             continue;
         }
         mLoopers[i].Abort(btree);
     }
 }
Пример #16
0
 public override EBTTaskState OnTaskStart(BehaviourTreeRunner btree)
 {
     if (mLooper == null)
     {
         return(EBTTaskState.faild);
     }
     else
     {
         mLooper.Reset();
         return(EBTTaskState.running);
     }
 }
Пример #17
0
 public void Tick(BehaviourTreeRunner btree, float deltaTime)
 {
     if (!IsOnCondition(btree))
     {
         Abort(btree);
     }
     OnTick(btree, deltaTime);
     if (State != EBTTaskState.running)
     {
         mEndTime = Time.time;
     }
 }
Пример #18
0
 public static AssetBinder GetBinder(BehaviourTreeRunner runner)
 {
     if (runner == null)
     {
         return(null);
     }
     if (runner.mAssetBinder == null || !runner.mAssetBinder.IsAvailable)
     {
         var asset = new AssetBinder(runner);
         runner.mAssetBinder = asset;
     }
     return(runner.mAssetBinder);
 }
Пример #19
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;
            }
        }
Пример #20
0
 protected override void OnReturnWithState(BehaviourTreeRunner btree, EBTTaskState state)
 {
     if (state == EBTTaskState.faild || !IsOnCondition(btree))
     {
         this.State = EBTTaskState.faild;
     }
     else
     {
         mVisitIndex++;
         if (mVisitIndex >= ChildLength)
         {
             this.State = EBTTaskState.success;
         }
     }
 }
Пример #21
0
 protected override void OnVisit(BehaviourTreeRunner behaviourTree)
 {
     State = ChildLength > 0 ? EBTTaskState.running : EBTTaskState.success;
     for (int i = 0; i < mLoopers.Length; i++)
     {
         if (mLoopers[i] != null)
         {
             mLoopers[i].Reset();
         }
     }
     if (mMainTaskIndex < mLoopers.Length && mMainTaskIndex >= 0)
     {
         mMainLooper = mLoopers[mMainTaskIndex];
     }
 }
Пример #22
0
 protected override void OnTick(BehaviourTreeRunner behaviourTree, float deltaTime)
 {
     if (mAbort)
     {
         State = EBTTaskState.faild;
     }
     else
     {
         mVisitIndex++;
         if (mVisitIndex >= ChildLength)
         {
             this.State = EBTTaskState.success;
         }
     }
 }
Пример #23
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);
            }
Пример #24
0
 public void Cleanup(BehaviourTreeRunner btree)
 {
     if (mVisited)
     {
         mVisited = false;
         mEndTime = Time.time;
         for (int i = ServiceLength - 1; i >= 0; i--)
         {
             BTServiceBase serv = mServices[i];
             if (serv != null)
             {
                 btree.StopService(serv);
             }
         }
         OnCleanup(btree);
     }
 }
Пример #25
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);
        }
Пример #26
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);
        }
Пример #27
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);
            }
        }
Пример #28
0
 public override EBTTaskState OnTaskTick(BehaviourTreeRunner btree, float deltaTime)
 {
     if (mLooper != null)
     {
         mLooper.Update(btree, deltaTime);
         if (mLooper.IsComplate)
         {
             return(mLooper.State);
         }
         else
         {
             return(EBTTaskState.running);
         }
     }
     else
     {
         return(EBTTaskState.faild);
     }
 }
Пример #29
0
            public void Dispose()
            {
                Cleanup();
                var tmp = mSubAssets;

                mSubAssets = null;
                for (int i = 0; i < tmp.Count; i++)
                {
                    tmp[i].mParent = null;
                }
                if (mParent != null && mParent.mSubAssets != null)
                {
                    mParent.mSubAssets.Remove(this);
                    mParent = null;
                }
                if (mLooper != null)
                {
                    mLooper.Dispose();
                }
                mLooper = null;
                mRunner = null;
            }
Пример #30
0
        protected override void OnTick(BehaviourTreeRunner behaviourTree, float deltaTime)
        {
            int num = 0;

            for (int i = 0; i < mLoopers.Length; i++)
            {
                if (mLoopers[i] == null || mLoopers[i].IsComplate)
                {
                    continue;
                }
                mLoopers[i].Update(behaviourTree, deltaTime);
                num++;
            }
            if (mMainLooper != null)
            {
                State = mMainLooper.State;
            }
            else
            {
                State = (num == 0) ? EBTTaskState.success : EBTTaskState.running;
            }
        }