Esempio n. 1
0
    /// <summary>
    /// 依次遍历所有子节点, 如果都返回Fail, 则向父节点返回Fail
    /// 遍历直到返回 Sucess 或者 Running
    /// 如果返回Running, 则需要记住节点, 下一次从这个节点开始执行
    /// </summary>
    /// <returns></returns>
    public override ResultType Execute()
    {
        int index = 0;

        if (lastRunningNode != null)
        {
            index = lastRunningNode.NodeIndex;
        }
        lastRunningNode = null;

        ResultType resultType = ResultType.Fail;

        for (int i = index; i < nodeChildList.Count; i++)
        {
            NodeRoot nodeRoot = nodeChildList[i];
            resultType = nodeRoot.Execute();
            if (resultType == ResultType.Fail)
            {
                continue;
            }

            if (resultType == ResultType.Success)
            {
                break;
            }

            if (resultType == ResultType.Running)
            {
                lastRunningNode = nodeRoot;
                break;
            }
        }

        return(resultType);
    }
Esempio n. 2
0
        /// <summary>
        /// 并行节点同时执行所有节点,直到一个节点返回 Fail 或者全部节点都返回 Success
        /// 才向父节点返回 Fail 或者 Success,并终止执行其他节点
        /// 其他情况向父节点返回 Running
        /// </summary>
        /// <returns></returns>
        public override ResultType Execute()
        {
            ResultType resultType = ResultType.Fail;

            int successCount = 0;

            for (int i = 0; i < nodeChildList.Count; ++i)
            {
                NodeRoot nodeRoot = nodeChildList[i];
                resultType = nodeRoot.Execute();

                if (resultType == ResultType.Fail)
                {
                    break;
                }

                if (resultType == ResultType.Success)
                {
                    ++successCount;
                    continue;
                }

                if (resultType == ResultType.Running)
                {
                    continue;
                }
            }

            if (resultType != ResultType.Fail)
            {
                resultType = (successCount >= nodeChildList.Count) ? ResultType.Success : ResultType.Running;
            }

            return(resultType);
        }
Esempio n. 3
0
        /// <summary>
        /// 修饰节点只有一个子节点,执行子节点直到 执行结果 = untilResultType,将 结果返回给父节点
        /// 如果执行结果 != untilResultType 则返回 Running
        /// </summary>
        /// <returns></returns>
        public override ResultType Execute()
        {
            NodeRoot   nodeRoot   = nodeChildList[0];
            ResultType resultType = nodeRoot.Execute();

            if (resultType != untilResultType)
            {
                return(ResultType.Running);
            }

            return(resultType);
        }
Esempio n. 4
0
    /// <summary>
    /// 随机遍历子节点, 当遇到 Success 时, 返回父节点
    /// 当执行遇到 Fail 时, 尝试执行下一个子节点
    /// 当执行遇到 Running 时, 记录该节点, 下一次继续运行
    /// </summary>
    /// <returns></returns>
    public override ResultType Execute()
    {
        List <int> randomList = GetRandom(nodeChildList.Count);

        int index = -1;

        if (lastRunningNode != null)
        {
            index           = lastRunningNode.NodeIndex;
            lastRunningNode = null;
        }
        ResultType resultType = ResultType.Fail;

        while ((randomList.Count > 0))
        {
            if (index < 0)
            {
                // 从随机数组中最后一个开始取
                index = randomList[randomList.Count - 1];
                randomList.RemoveAt(randomList.Count - 1);
            }
            NodeRoot nodeRoot = nodeChildList[index];
            index      = -1;
            resultType = nodeRoot.Execute();
            if (resultType == ResultType.Fail)
            {
                continue;
            }
            if (resultType == ResultType.Success)
            {
                break;
            }
            if (resultType == ResultType.Running)
            {
                lastRunningNode = nodeRoot;
                break;
            }
        }

        return(resultType);
    }
    /// <summary>
    /// 并行组合节点同时执行所有节点, 直到一个节点返回 Fail 或者全部节点 Success 才返回给父节点Success
    /// 其他情况, 则向父节点返回Running
    /// </summary>
    /// <returns></returns>
    public override ResultType Execute()
    {
        ResultType resultType = ResultType.Fail;

        int successCount = 0;

        for (int i = 0; i < nodeChildList.Count; i++)
        {
            NodeRoot nodeRoot = nodeChildList[i];
            resultType = nodeRoot.Execute();
            if (resultType == ResultType.Fail)
            {
                break;
            }
            if (resultType == ResultType.Running)
            {
                break;
            }

            if (resultType == ResultType.Success)
            {
                successCount++;
                continue;
            }
        }

        if (resultType == ResultType.Fail)
        {
            return(ResultType.Fail);
        }
        if (successCount == nodeChildList.Count)
        {
            return(ResultType.Success);
        }
        return(ResultType.Running);
    }