Exemplo n.º 1
0
        protected override BTResult OnTick(BTContext context)
        {
            if (_activeChildIndex == -1)
            {
                _activeChildIndex = 0;
            }

            for (; _activeChildIndex < m_Children.Count; _activeChildIndex++)
            {
                BTNode activeChild = m_Children[_activeChildIndex];

                BTResult result = activeChild.Tick_(context);
                switch (result.Code)
                {
                case BTResultCode.Running:
                    Running = true;
                    return(BTResult.Running);

                case BTResultCode.Success:
                    activeChild.Reset();
                    continue;

                case BTResultCode.Failed:
                    activeChild.Reset();
                    _activeChildIndex = -1;
                    Running           = false;
                    return(BTResult.Failed);
                }
            }

            _activeChildIndex = -1;
            Running           = false;
            return(BTResult.Success);
        }
Exemplo n.º 2
0
        protected override BTResult OnTick(BTContext context)
        {
            BTResult r = m_Child.Tick_(context);

            if (skipTick)
            {
                return(r);
            }

            Type childType = m_Child.GetType();

            if (memberType == BTMemberType.Field)
            {
                if (_field == null)
                {
                    _field = childType.GetField(MemberName, BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
                }

                if (_field != null)
                {
                    object fv = _field.GetValue(m_Child);
                    if (DataId != null)
                    {
                        context.Database?.SetData(DataId, fv);
                    }
                }
                else
                {
                    skipTick = true;
                }
            }
            else if (memberType == BTMemberType.Property)
            {
                if (_property == null)
                {
                    _property = childType.GetProperty(MemberName, BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
                }
                if (_property != null && _property.CanRead)
                {
                    object pv = _property.GetGetMethod().Invoke(m_Child, null);
                    if (DataId != null)
                    {
                        context.Database?.SetData(DataId, pv);
                    }
                }
                else
                {
                    skipTick = true;
                }
            }

            return(r);
        }
Exemplo n.º 3
0
        protected override bool Check(BTContext context)
        {
            if (context.Database == null)
            {
                return(false);
            }

            if (_rhs == null)
            {
                return(context.Database.CheckDataNull(_dataName));
            }
            return(context.Database.GetData <T>(_dataName).Equals(_rhs));
        }
Exemplo n.º 4
0
 protected override BTResult OnTick(BTContext context)
 {
     BTResult result = m_Child.Tick_(context);
     switch (result.Code)
     {
         case BTResultCode.Running:
             return BTResult.Running;
         case BTResultCode.Success:
             return BTResult.Failed;
         case BTResultCode.Failed:
             return BTResult.Success;
     }
     return BTResult.Failed;
 }
Exemplo n.º 5
0
        protected override BTResult OnTick(BTContext context)
        {
            if (m_Children == null || m_Children.Count == 0)
            {
                return(BTResult.Failed);
            }

            int      index  = SelectChild();
            BTResult result = m_Children[index].Tick_(context);

            //m_Children[index].Reset();
            lastSelectedIndex = index;
            return(result);
        }
Exemplo n.º 6
0
Arquivo: BTTree.cs Projeto: nistck/Jx
        public BTResult Tick(BTContext context)
        {
            context.OnSessionStart();
            BTResult result = BTResult.Running;

            try
            {
                if (Root != null)
                {
                    result = Root.Tick_(context);
                }
            }
            finally {
                context.OnSessionEnd();
            }
            return(result);
        }
Exemplo n.º 7
0
Arquivo: BTNode.cs Projeto: nistck/Jx
        /// <summary>
        /// 检查前置条件
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        private bool CheckConstraints(BTContext context)
        {
            bool result = true;

            m_Constraints.RemoveAll(_x => _x == null);
            for (int i = 0; i < m_Constraints.Count; i++)
            {
                bool b = m_Constraints[i].Execute(context);
                if (!b)
                {
                    result = false;
                    break;
                }
            }

            return(result);
        }
Exemplo n.º 8
0
        protected override BTResult OnTick(BTContext context)
        {
            bool r = Check(context);

            if (inverseResult)
            {
                r = !r;
            }
            if (r)
            {
                return(BTResult.Success);
            }
            else
            {
                return(BTResult.Failed);
            }
        }
Exemplo n.º 9
0
Arquivo: BTTimer.cs Projeto: nistck/Jx
        protected override BTResult OnTick(BTContext context)
        {
            if (_timer == 0)
            {
                _timer = DateTime.Now.Ticks;
            }

            long ts = (DateTime.Now.Ticks - _timer) / 10000;

            if (ts >= interval)
            {
                _timer = DateTime.Now.Ticks;
                BTResult result = m_Child.Tick_(context);
                return(result);
            }
            else
            {
                return(BTResult.Running);
            }
        }
Exemplo n.º 10
0
        protected override BTResult OnTick(BTContext context)
        {
            for (int i = 0; i < m_Children.Count; i++)
            {
                BTNode child = m_Children[i];

                BTResult result = child.Tick_(context);
                switch (result.Code)
                {
                case BTResultCode.Running:
                    if (_activeChildIndex != i && _activeChildIndex != -1)
                    {
                        m_Children[_activeChildIndex].Reset();
                    }
                    _activeChildIndex          = i;
                    _previousSuccessChildIndex = -1;
                    Running = true;
                    return(BTResult.Running);

                case BTResultCode.Success:
                    if (_activeChildIndex != i && _activeChildIndex != -1)
                    {
                        m_Children[_activeChildIndex].Reset();
                    }
                    child.Reset();
                    _activeChildIndex          = -1;
                    _previousSuccessChildIndex = i;
                    Running = false;
                    return(BTResult.Success);

                case BTResultCode.Failed:
                    child.Reset();
                    continue;
                }
            }

            _activeChildIndex          = -1;
            _previousSuccessChildIndex = -1;
            Running = false;
            return(BTResult.Failed);
        }
Exemplo n.º 11
0
        protected override BTResult OnTick(BTContext context)
        {
            string text = Message ?? "";

            switch (level)
            {
            case BTLogLevel.Info:
                BTDebug.Info(text);
                break;

            case BTLogLevel.Warning:
                BTDebug.Warning(text);
                break;

            case BTLogLevel.Error:
                BTDebug.Error(text);
                break;
            }

            return(BTResult.Success.Create(Message == null? "消息为空" : null));
        }
Exemplo n.º 12
0
Arquivo: BTNode.cs Projeto: nistck/Jx
        /// <summary>
        /// 每帧处理函数
        /// </summary>
        /// <param name="context">上下文</param>
        /// <returns></returns>
        internal BTResult Tick_(BTContext context)
        {
            context?.Travel(this);

            BTResult result = BTResult.Running;

            bool constraintsTrue = CheckConstraints(context);

            if (!constraintsTrue)
            {
                result = _MuteResult(BTResult.Failed);
            }
            else
            {
                if (!_sessionOpen)
                {
                    _open(context);
                }

                _enter(context);
                try
                {
                    result = OnTick(context);
                }
                finally
                {
                    _exit(context, result);
                }

                if (result != BTResult.Running)
                {
                    _close(context, result);
                }
            }

            context?.SetNodeResult(this, result);
            return(_MuteResult(result));
        }
Exemplo n.º 13
0
        protected override BTResult OnTick(BTContext context)
        {
            if (count == 0)
            {
                BTResult result = m_Child.Tick_(context);

                if (endOnFailure && result == BTResult.Failed)
                {
                    return(BTResult.Failed);
                }

                Running = true;
                return(BTResult.Running);
            }
            else if (currentCount < count)
            {
                BTResult result = m_Child.Tick_(context);
                currentCount++;

                if (currentCount >= count)
                {
                    if (result == BTResult.Running)
                    {
                        result = BTResult.Success;
                    }
                    return(result);
                }

                if (endOnFailure && result == BTResult.Failed)
                {
                    return(BTResult.Failed);
                }
                Running = true;
                return(BTResult.Running);
            }

            return(BTResult.Failed);
        }
Exemplo n.º 14
0
Arquivo: BTNode.cs Projeto: nistck/Jx
 /// <summary>
 /// Session结束
 /// </summary>
 /// <param name="context"></param>
 /// <param name="result"></param>
 protected virtual void OnClose(BTContext context, BTResult result)
 {
 }
Exemplo n.º 15
0
Arquivo: BTFalse.cs Projeto: nistck/Jx
 protected override bool Evaluate(BTContext context)
 {
     return(false);
 }
Exemplo n.º 16
0
Arquivo: BTNode.cs Projeto: nistck/Jx
 /// <summary>
 /// 初始化
 /// </summary>
 /// <param name="context"></param>
 protected virtual void OnInit(BTContext context)
 {
 }
Exemplo n.º 17
0
Arquivo: BTNode.cs Projeto: nistck/Jx
 /// <summary>
 /// 保存
 /// </summary>
 /// <param name="context"></param>
 protected virtual void OnSave(BTContext context)
 {
 }
Exemplo n.º 18
0
Arquivo: BTNode.cs Projeto: nistck/Jx
 /// <summary>
 /// 执行逻辑
 /// </summary>
 /// <param name="context"></param>
 /// <returns></returns>
 protected virtual BTResult OnTick(BTContext context)
 {
     return(BTResult.Failed);
 }
Exemplo n.º 19
0
Arquivo: BTNode.cs Projeto: nistck/Jx
 /// <summary>
 /// 加载
 /// </summary>
 /// <param name="context"></param>
 protected virtual void OnLoad(BTContext context)
 {
 }
Exemplo n.º 20
0
Arquivo: BTNode.cs Projeto: nistck/Jx
 private void _exit(BTContext context, BTResult result)
 {
     OnExit(context, result);
 }
Exemplo n.º 21
0
Arquivo: BTMute.cs Projeto: nistck/Jx
 protected override BTResult OnTick(BTContext context)
 {
     m_Child.Tick_(context);
     return(result);
 }
Exemplo n.º 22
0
 protected virtual bool Check(BTContext context)
 {
     return(false);
 }
Exemplo n.º 23
0
 protected virtual bool Evaluate(BTContext context)
 {
     return(false);
 }
Exemplo n.º 24
0
Arquivo: BTNode.cs Projeto: nistck/Jx
 private void _enter(BTContext context)
 {
     OnEnter(context);
 }
Exemplo n.º 25
0
Arquivo: BTNode.cs Projeto: nistck/Jx
 private void _close(BTContext context, BTResult result)
 {
     _sessionOpen = false;
     OnClose(context, result);
 }
Exemplo n.º 26
0
Arquivo: BTNode.cs Projeto: nistck/Jx
 private void _open(BTContext context)
 {
     _sessionOpen = true;
     OnOpen(context);
 }
Exemplo n.º 27
0
Arquivo: BTNode.cs Projeto: nistck/Jx
 /// <summary>
 /// 执行之前
 /// </summary>
 /// <param name="context"></param>
 protected virtual void OnEnter(BTContext context)
 {
 }
Exemplo n.º 28
0
Arquivo: BTNode.cs Projeto: nistck/Jx
 /// <summary>
 /// 执行之后
 /// </summary>
 /// <param name="context"></param>
 /// <param name="result"></param>
 protected virtual void OnExit(BTContext context, BTResult result)
 {
 }
Exemplo n.º 29
0
Arquivo: BTNode.cs Projeto: nistck/Jx
 /// <summary>
 /// 开启新Session (在Running时,不会开启新Session)
 /// </summary>
 /// <param name="context"></param>
 protected virtual void OnOpen(BTContext context)
 {
 }
Exemplo n.º 30
0
        protected override BTResult OnTick(BTContext context)
        {
            if (m_Children == null || m_Children.Count == 0)
            {
                return(BTResult.Success);
            }

            BTResult status            = BTResult.Success;
            int      numberOfFailures  = 0;
            int      numberOfSuccesses = 0;
            int      numberOfRunning   = 0;

            for (int i = 0; i < m_Children.Count; i++)
            {
                BTResult childStatus = m_Children[i].Tick_(context);

                if (childStatus == BTResult.Success)
                {
                    numberOfSuccesses++;
                }
                else if (childStatus == BTResult.Failed)
                {
                    numberOfFailures++;
                }
                else if (childStatus == BTResult.Running)
                {
                    numberOfRunning++;
                }
            }


            if ((m_failOnAny && numberOfFailures > 0) || (m_succeedOnAny && numberOfSuccesses > 0))
            {
                if (m_failOnTie)
                {
                    if (m_failOnAny && numberOfFailures > 0)
                    {
                        status = BTResult.Failed;
                    }
                    else if (m_succeedOnAny && numberOfSuccesses > 0)
                    {
                        status = BTResult.Success;
                    }
                }
                else
                {
                    if (m_succeedOnAny && numberOfSuccesses > 0)
                    {
                        status = BTResult.Success;
                    }
                    else if (m_failOnAny && numberOfFailures > 0)
                    {
                        status = BTResult.Failed;
                    }
                }
            }
            else
            {
                if (numberOfSuccesses == m_Children.Count)
                {
                    status = BTResult.Success;
                }
                else if (numberOfFailures == m_Children.Count)
                {
                    status = BTResult.Failed;
                }
                else
                {
                    status = BTResult.Running;
                }
            }

            return(status);
        }