Example #1
0
        /// <summary>
        /// 构建节点
        /// </summary>
        /// <param name="nodeData">节点数据</param>
        public BaseNode CreateNode(NodeData nodeData)
        {
            if (nodeData == null)
            {
                throw new Exception($"BehaviorTreeManager.CreateNode() \n nodeData is null.");
            }

            var proxyData = _proxyManager.GetProxyData(nodeData.ClassType);

            if (proxyData == null)
            {
                throw new Exception($"BehaviorTreeManager.CreateNode({nodeData.ClassType}) \n proxyData is null.");
            }

            BaseNode baseNode = null;

            switch (proxyData.NodeType)
            {
            case NodeType.Action:
                baseNode = new ActionNode {
                    NodeType = NodeType.Action
                };
                break;

            case NodeType.Composite:
                baseNode = new CompositeNode {
                    NodeType = NodeType.Composite
                };
                break;

            case NodeType.Condition:
                baseNode = new ConditionNode {
                    NodeType = NodeType.Condition
                };
                break;

            case NodeType.Decorator:
                baseNode = new DecorateNode {
                    NodeType = NodeType.Condition
                };
                break;
            }

            if (baseNode == null)
            {
                throw new Exception($"CreateNode {proxyData.Name} Failed");
            }

            baseNode.SetData(nodeData);
            baseNode.SetProxyData(proxyData);

            if (baseNode is CompositeNode)
            {
                if (nodeData.Childs == null || nodeData.Childs.Count == 0)
                {
                    throw new Exception($"{proxyData.NodeType} node must have child nodes");
                }

                var compositeNode = baseNode as CompositeNode;
                for (int i = 0; i < nodeData.Childs.Count; i++)
                {
                    var childNodeData = nodeData.Childs[i];
                    var childNode     = CreateNode(childNodeData);
                    compositeNode.AddChild(childNode);
                }
            }

            return(baseNode);
        }