Beispiel #1
0
        private NodeBase AnalysisNode(int entityId, long aiFunction, BehaviorTreeData data, int nodeId, IConditionCheck iConditionCheck, Action <int> InvalidSubTreeCallBack)
        {
            NodeValue nodeValue = null;

            if (!data.nodeDic.TryGetValue(nodeId, out nodeValue))
            {
                return(null);
            }

            if (nodeValue.NodeType == (int)NODE_TYPE.SUB_TREE && nodeValue.subTreeValue > 0)
            {
                if ((aiFunction & nodeValue.subTreeValue) <= 0)
                {
                    InvalidSubTreeCallBack?.Invoke(nodeValue.id);
                    return(null);
                }
            }

            UnityEngine.Profiling.Profiler.BeginSample("AnalysisNode CreateNode");
            NodeBase nodeBase = AnalysisNode(nodeValue, iConditionCheck);

            nodeBase.NodeId   = nodeValue.id;
            nodeBase.EntityId = entityId;
            nodeBase.Priority = nodeValue.priority;
            UnityEngine.Profiling.Profiler.EndSample();

            if (nodeValue.NodeType == (int)NODE_TYPE.SUB_TREE && nodeValue.subTreeType == (int)SUB_TREE_TYPE.CONFIG)
            {
                BehaviorTreeData subTreeData = _loadConfigInfoEvent(nodeValue.subTreeConfig);
                if (null != subTreeData)
                {
                    NodeBase      subTreeNode = Analysis(entityId, subTreeData, iConditionCheck, InvalidSubTreeCallBack);
                    NodeComposite composite   = (NodeComposite)(nodeBase);
                    composite.AddNode(subTreeNode);
                    iConditionCheck.AddParameter(subTreeData.parameterList);
                }
            }

            UnityEngine.Profiling.Profiler.BeginSample("AnalysisNode IsLeafNode");
            if (!IsLeafNode(nodeValue.NodeType))
            {
                NodeComposite composite = (NodeComposite)nodeBase;
                for (int i = 0; i < nodeValue.childNodeList.Count; ++i)
                {
                    int      childNodeId = nodeValue.childNodeList[i];
                    NodeBase childNode   = AnalysisNode(entityId, aiFunction, data, childNodeId, iConditionCheck, InvalidSubTreeCallBack);
                    if (null != childNode)
                    {
                        composite.AddNode(childNode);
                    }
                }
            }
            UnityEngine.Profiling.Profiler.EndSample();

            return(nodeBase);
        }
        public NodeBase Analysis(BehaviorTreeData data, IConditionCheck iConditionCheck, Action <NodeAction> ActionNodeCallBack)
        {
            NodeBase rootNode = null;

            if (null == data)
            {
                Debug.LogError("数据无效");
                return(rootNode);
            }

            if (data.rootNodeId < 0)
            {
                Debug.LogError("没有跟节点");
                return(rootNode);
            }

            iConditionCheck.AddParameter(data.parameterList);

            Dictionary <int, NodeBase>    compositeDic = new Dictionary <int, NodeBase>();
            Dictionary <int, NodeBase>    allNodeDic   = new Dictionary <int, NodeBase>();
            Dictionary <int, List <int> > childDic     = new Dictionary <int, List <int> >();

            for (int i = 0; i < data.nodeList.Count; ++i)
            {
                NodeValue nodeValue = data.nodeList[i];
                NodeBase  nodeBase  = AnalysisNode(nodeValue, iConditionCheck);
                nodeBase.NodeId = nodeValue.id;

                if (!IsLeafNode(nodeValue.NodeType))
                {
                    compositeDic[nodeValue.id] = nodeBase;
                    childDic[nodeValue.id]     = nodeValue.childNodeList;

                    if (data.rootNodeId == nodeValue.id)
                    {
                        rootNode = nodeBase;
                    }
                }

                if (null == nodeBase)
                {
                    Debug.LogError("AllNODE:" + nodeValue.id + "     " + (null != nodeBase));
                }

                if (nodeValue.NodeType == (int)NODE_TYPE.ACTION && null != ActionNodeCallBack)
                {
                    ActionNodeCallBack((NodeAction)nodeBase);
                }

                allNodeDic[nodeValue.id] = nodeBase;
            }

            foreach (var kv in compositeDic)
            {
                int           id        = kv.Key;
                NodeComposite composite = (NodeComposite)(kv.Value);

                List <int> childList = childDic[id];
                for (int i = 0; i < childList.Count; ++i)
                {
                    int      nodeId    = childList[i];
                    NodeBase childNode = allNodeDic[nodeId];
                    if (null == childNode)
                    {
                        Debug.LogError("null node :" + nodeId);
                        continue;
                    }
                    composite.AddNode(childNode);
                }
            }

            return(rootNode);
        }