Exemple #1
0
        /// <summary>
        /// 添加根节点。
        /// </summary>
        /// <param name="action"></param>
        /// <param name="checker"></param>
        /// <returns></returns>
        public ActionPair AddRoot(ExecuteAction action, CheckActionState checker)
        {
            var root = new ActionPair(action, checker);

            _head.AddDefaultChild(root);
            return(root);
        }
Exemple #2
0
        /// <summary>
        /// 开始执行整个动作树。
        /// </summary>
        public void Start()
        {
            CurrentAction = _head.GetChild(ActionState.Default);
            if (CurrentAction != null && CurrentAction.Action != null)
            {
                // 立即执行第一个动作。
                CurrentAction.Action();
            }

            CurrentState = ActionTreeState.Running;
        }
Exemple #3
0
        /// <summary>
        /// 由上层的MonoBehaviour调用。
        /// </summary>
        public void Update()
        {
            if (CurrentState != ActionTreeState.Running)
            {
                return;
            }

            if (CurrentAction == null)
            {
                // 如果当前没有执行Action,则直接结束所有的动作。
                CompleteActionTree();
                return;
            }

            if (CurrentAction.Checker != null)
            {
                // 存在checker,则检查checker的结果。
                CurrentAction.Checker(CurrentAction.State);
                if (CurrentAction.State.Result != ActionState.Continue)
                {
                    // 不是Contine,则尝试开始下一个动作。
                    var next = CurrentAction.GetChild(CurrentAction.State.Result);
                    CurrentAction = next;

                    if (CurrentAction != null && CurrentAction.Action != null)
                    {
                        CurrentAction.Action();
                    }
                }
            }
            else
            {
                // 不存在checker,则尝试查找默认的动作。
                var next = CurrentAction.GetChild(ActionState.Default);
                CurrentAction = next;

                if (CurrentAction != null && CurrentAction.Action != null)
                {
                    CurrentAction.Action();
                }
            }
        }
Exemple #4
0
        /// <summary>
        /// 添加子节点。
        /// </summary>
        /// <param name="key"></param>
        /// <param name="child"></param>
        /// <returns>返回当前节点,一遍重复添加。</returns>
        public ActionPair AddChild(int key, ActionPair child)
        {
            if (child == null)
            {
                return(this);
            }

            if (Children == null)
            {
                Children = new Dictionary <int, ActionPair>();
            }

            if (Children.ContainsKey(key))
            {
                Children[key] = child;
            }
            else
            {
                Children.Add(key, child);
            }

            return(this);
        }
Exemple #5
0
        /// <summary>
        /// 添加默认的子节点。
        /// </summary>
        /// <param name="child"></param>
        /// <returns></returns>
        public ActionPair AddDefaultChild(ActionPair child)
        {
            if (child == null)
            {
                return(this);
            }

            if (Children == null)
            {
                Children = new Dictionary <int, ActionPair>();
            }

            if (Children.ContainsKey(ActionState.Default))
            {
                Children[ActionState.Default] = child;
            }
            else
            {
                Children.Add(ActionState.Default, child);
            }

            return(this);
        }
Exemple #6
0
 /// <summary>
 /// 重置状态。
 /// </summary>
 public void Reset()
 {
     CurrentAction = _head;
     CurrentState  = ActionTreeState.Idle;
 }