// 添加子节点 public void AddChild(NodeRoot nodeRoot) { int count = nodeChildList.Count; nodeRoot.NodeIndex = count; nodeChildList.Add(nodeRoot); }
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.Run(); if (resultType == ResultType.Fail) { continue; } if (resultType == ResultType.Success) { break; } if (resultType == ResultType.Running) { lastRunningNode = nodeRoot; break; } } return(resultType); }
public static NodeRoot Init(int iId, Dictionary <string, TupleList <NodeType, string, object> > nodesGeneratorDict, Tuple <NodeType, string, object> nodeData = null, int layer = 0 ) { NodeRoot oNode = null; NodeType type; string typeStr; string nodeName; if (nodeData == null) { // 生成根节点 type = NodeType.Select; nodeName = Nodes.BTree_Root_Str; typeStr = Nodes.str[NodeType.Select]; } else { type = nodeData.Item1; nodeName = nodeData.Item2; if (type == NodeType.Select) { typeStr = Nodes.str[NodeType.Select]; } else if (type == NodeType.Sequence) { typeStr = Nodes.str[NodeType.Sequence]; } else { typeStr = Nodes.nodesDict[nodeName]; } } // Console.WriteLine("typeStr: " + typeStr); Assembly assembly = Assembly.GetExecutingAssembly(); object obj = assembly.CreateInstance(typeStr); switch (type) { case NodeType.Action: oNode = obj as NodeAction; break; case NodeType.Condition: oNode = obj as NodeCondition; break; case NodeType.Decorator: oNode = obj as NodeDecorator; break; case NodeType.Parallel: oNode = obj as NodeParallel; break; case NodeType.Random: oNode = obj as NodeRandom; break; case NodeType.Select: oNode = obj as NodeSelect; break; case NodeType.Sequence: oNode = obj as NodeSequence; break; default: oNode = null; break; } if (oNode == null) { Console.WriteLine("Node 找不到目标名 " + nodeName); return(null); } oNode.Name = nodeName; oNode.RoleId = iId; // 根节点打印 if (layer == 0) { Console.WriteLine(" - " + nodeName + " " + layer); } if (nodesGeneratorDict.ContainsKey(nodeName)) { int childLayer = layer + 1; foreach (Tuple <NodeType, string, object> childData in nodesGeneratorDict[nodeName]) { for (int i = 0; i < 4 * childLayer; i++) { Console.Write(" "); } Console.WriteLine(" - " + childData.Item2 + " " + childLayer); oNode.AddChild(Init(iId, nodesGeneratorDict, childData, childLayer)); } } return(oNode); }
public void Init(int iId, Dictionary <string, TupleList <NodeType, string, object> > nodesGeneratorDict) { root = TreeConstructor.Init(iId, nodesGeneratorDict); }