//创建行为树 private void CreateBev(BevTrees BevTrees) { //实体 foreach (string key in BevTrees.EntityTrees.Keys) { EntityReqId reqId = (EntityReqId)Enum.Parse(typeof(EntityReqId), key); //创建树 NodeDataJson nodeJson = BevTrees.EntityTrees[key]; Node rootNode = Node.CreateNodeInstance(nodeJson); Node.CreateNodeRelation(rootNode, nodeJson.ChildNodes); BaseEntityBehavior request = new BaseEntityBehavior(rootNode); EntityBevDict.Add((int)reqId, request); } //世界 foreach (string key in BevTrees.WorldTrees.Keys) { WorldReqId reqId = (WorldReqId)Enum.Parse(typeof(WorldReqId), key); //创建树 NodeDataJson nodeJson = BevTrees.WorldTrees[key]; Node rootNode = Node.CreateNodeInstance(nodeJson); Node.CreateNodeRelation(rootNode, nodeJson.ChildNodes); BaseWorldBehavior request = new BaseWorldBehavior(rootNode); WorldBevDict.Add((int)reqId, request); } }
//创建决策 private void CreateDec(DecTrees DecTrees) { //实体 foreach (string key in DecTrees.EntityTrees.Keys) { EntityDecGroup group = (EntityDecGroup)Enum.Parse(typeof(EntityDecGroup), key); //创建树 NodeDataJson nodeJson = DecTrees.EntityTrees[key]; Node rootNode = Node.CreateNodeInstance(nodeJson); Node.CreateNodeRelation(rootNode, nodeJson.ChildNodes); BaseEntityDecision decision = new BaseEntityDecision(rootNode);; EntityDesDict.Add((int)group, decision); } //世界 foreach (string key in DecTrees.WorldTrees.Keys) { WorldDecGroup group = (WorldDecGroup)Enum.Parse(typeof(WorldDecGroup), key); //创建树 NodeDataJson nodeJson = DecTrees.WorldTrees[key]; Node rootNode = Node.CreateNodeInstance(nodeJson); Node.CreateNodeRelation(rootNode, nodeJson.ChildNodes); BaseWorldDecision decision = new BaseWorldDecision(rootNode); WorldDesDict.Add((int)group, decision); } }
//行为层 private void DrawBevTreeList() { //列表 EDLayout.CreateScrollView(ref leftPos, "box", position.width * 0.3f, position.height, () => { EDTypeField.CreateLableField("********世界行为********", "", position.width * 0.3f, 20); foreach (string group in MBevTrees.WorldTrees.Keys) { NodeDataJson tree = MBevTrees.WorldTrees[group]; EDButton.CreateBtn(group, position.width * 0.28f, 25, () => { SelTreeChange(tree); }); } EDTypeField.CreateLableField("********实体行为********", "", position.width * 0.3f, 20); foreach (string group in MBevTrees.EntityTrees.Keys) { NodeDataJson tree = MBevTrees.EntityTrees[group]; EDButton.CreateBtn(group, position.width * 0.28f, 25, () => { SelTreeChange(tree); }); } }); }
//创建关系 public static void CreateNodeRelation(Node parNode, List <NodeDataJson> childNodes) { if (parNode == null) { return; } if (childNodes == null) { return; } for (int i = 0; i < childNodes.Count; i++) { NodeDataJson childAsset = childNodes[i]; Node childNode = CreateNodeInstance(childAsset); //递归 CreateNodeRelation(childNode, childAsset.ChildNodes); if (childNode != null) { parNode.AddChild(childNode); } } }
//创建节点 public static Node CreateNodeInstance(NodeDataJson node) { if (string.IsNullOrEmpty(node.TypeFullName)) { return(null); } Node rootNode = LCReflect.CreateInstanceByType <Node>(node.TypeFullName); rootNode.Init(node.NodeId, node.Type, node.ChildMaxCnt); if (node.Premise != null) { NodePremise premise = LCReflect.CreateInstanceByType <NodePremise>(node.Premise.TypeFullName); premise.Init(rootNode.GetHashCode(), node.Premise.Type, node.Premise.TrueValue); if (node.Premise.OtherPremise != null) { CreateNodePremise(rootNode.GetHashCode(), premise, node.Premise.OtherPremise); } rootNode.SetPremise(premise); } //属性设置 for (int i = 0; i < node.KeyValues.Count; i++) { NodeKeyValue keyValue = node.KeyValues[i]; object value = LCConvert.StrChangeToObject(keyValue.Value, keyValue.TypeFullName); LCReflect.SetTypeFieldValue(rootNode, keyValue.KeyName, value); } return(rootNode); }
private void CreateNodeEditor(NodeEditor node) { for (int i = 0; i < node.Json.ChildNodes.Count; i++) { NodeDataJson childNode = node.Json.ChildNodes[i]; NodeEditor tmpEditor = new NodeEditor(new Rect(new Vector2((float)childNode.PosX, (float)childNode.PosY), new Vector2(200, 100)), childNode, node, NodeEventCallBack); showNodeEditor.Add(tmpEditor); CreateNodeEditor(tmpEditor); } }
private void SelTreeChange(NodeDataJson tree) { selTree = tree; showNodeEditor.Clear(); //创建显示 NodeEditor nodeEditor = new NodeEditor(new Rect(new Vector2((float)tree.PosX, (float)tree.PosY), new Vector2(200, 100)), tree, null, NodeEventCallBack); showNodeEditor.Add(nodeEditor); CreateNodeEditor(nodeEditor); }
private void DrawNodeLine(NodeDataJson node) { NodeEditor parNode = GetNodeEditorById(node.GetHashCode()); for (int i = 0; i < node.ChildNodes.Count; i++) { NodeEditor childNode = GetNodeEditorById(node.ChildNodes[i].GetHashCode()); EDLine.CreateBezierLine(parNode.mRect.position, childNode.mRect.position, 10); DrawNodeLine(node.ChildNodes[i]); } }
public NodeEditor(Rect rect, NodeDataJson json, NodeEditor parEditor, Action <int, NodeEditor> processCallBack) { Id = json.NodeId; Name = json.Name; mRect = rect; Json = json; ParEditor = parEditor; GetEditorNodeKeyValue(); CallBack = processCallBack; IsRunning = false; }
private static NodeDataJson CreateNodeJson(NodeType nodeType, string fullName, string name = "", Vector2 pos = default) { if (pos == default) { pos = new Vector2(300, 400); } int nodeId = 0; if (ShowDec) { nodeId = MDecTrees.NodeId; MDecTrees.NodeId++; } else { nodeId = MBevTrees.NodeId; MBevTrees.NodeId++; } NodeDataJson node = new NodeDataJson(nodeId, nodeType, fullName, pos.x, pos.y, name);; return(node); }
private static void InitJson() { //决策 DecTrees TempDecTrees = new DecTrees(); TempDecTrees.NodeId = MDecTrees.NodeId; foreach (int group in Enum.GetValues(typeof(EntityDecGroup))) { string strName = Enum.GetName(typeof(EntityDecGroup), group); NodeDataJson oldJson = null; if (MDecTrees.EntityTrees.ContainsKey(strName)) { oldJson = MDecTrees.EntityTrees[strName]; } if (oldJson == null) { oldJson = CreateNodeJson(NodeType.Root, typeof(NodeRoot).FullName, "根节点"); } TempDecTrees.EntityTrees.Add(strName, oldJson); } foreach (int group in Enum.GetValues(typeof(WorldDecGroup))) { string strName = Enum.GetName(typeof(WorldDecGroup), group); NodeDataJson oldJson = null; if (MDecTrees.WorldTrees.ContainsKey(strName)) { oldJson = MDecTrees.WorldTrees[strName]; } if (oldJson == null) { oldJson = CreateNodeJson(NodeType.Root, typeof(NodeRoot).FullName, "根节点"); } TempDecTrees.WorldTrees.Add(strName, oldJson); } MDecTrees = TempDecTrees; //行为 BevTrees TempBevTrees = new BevTrees(); TempBevTrees.NodeId = MBevTrees.NodeId; foreach (int group in Enum.GetValues(typeof(EntityReqId))) { string strName = Enum.GetName(typeof(EntityReqId), group); NodeDataJson oldJson = null; if (MBevTrees.EntityTrees.ContainsKey(strName)) { oldJson = MBevTrees.EntityTrees[strName]; } if (oldJson == null) { oldJson = CreateNodeJson(NodeType.Root, typeof(NodeRoot).FullName, "根节点"); } TempBevTrees.EntityTrees.Add(strName, oldJson); } foreach (int group in Enum.GetValues(typeof(WorldReqId))) { string strName = Enum.GetName(typeof(WorldReqId), group); NodeDataJson oldJson = null; if (MBevTrees.WorldTrees.ContainsKey(strName)) { oldJson = MBevTrees.WorldTrees[strName]; } if (oldJson == null) { oldJson = CreateNodeJson(NodeType.Root, typeof(NodeRoot).FullName, "根节点"); } TempBevTrees.WorldTrees.Add(strName, oldJson); } MBevTrees = TempBevTrees; }
private void ShowCreateNodeMenu() { List <string> showStrs = new List <string>(); //控制节点 List <Type> controlTypes = LCReflect.GetClassByType <NodeControl>(); for (int i = 0; i < controlTypes.Count; i++) { NodeAttribute nodeAttribute = LCReflect.GetTypeAttr <NodeAttribute>(controlTypes[i]); if (nodeAttribute != null) { showStrs.Add("控制节点/" + nodeAttribute.ViewName); } else { showStrs.Add("控制节点/" + controlTypes[i].FullName); } } //行为节点 List <Type> actionTypes = LCReflect.GetClassByType <NodeAction>(); List <Type> actionShowTypes = new List <Type>(); for (int i = 0; i < actionTypes.Count; i++) { NodeAttribute nodeAttribute = LCReflect.GetTypeAttr <NodeAttribute>(actionTypes[i]); if (nodeAttribute == null) { showStrs.Add("基础行为节点/" + nodeAttribute.ViewName); actionShowTypes.Add(actionTypes[i]); } else { if (nodeAttribute.IsCommonAction) { showStrs.Add("基础行为/" + nodeAttribute.ViewName); actionShowTypes.Add(actionTypes[i]); } else { if (ShowDec) { if (nodeAttribute.IsBevNode == false) { showStrs.Add("扩展行为/" + nodeAttribute.ViewName); actionShowTypes.Add(actionTypes[i]); } } else { if (nodeAttribute.IsBevNode) { showStrs.Add("扩展行为/" + nodeAttribute.ViewName); actionShowTypes.Add(actionTypes[i]); } } } } } EDPopMenu.CreatePopMenu(showStrs, (int index) => { Debug.Log("创建节点》》》》》》》》》" + index + " " + controlTypes.Count + " >>" + actionShowTypes.Count); //创建控制 if (index <= controlTypes.Count - 1) { NodeDataJson node = CreateNodeJson(NodeType.Control, controlTypes[index].FullName, showStrs[index], curMousePos); //创建显示 NodeEditor nodeEditor = new NodeEditor(new Rect(new Vector2((float)node.PosX, (float)node.PosY), new Vector2(200, 100)), node, null, NodeEventCallBack); showNodeEditor.Add(nodeEditor); GUI.changed = true; } //创建行为 else { Debug.Log("创建行为节点》》》》》》》》》" + (index - controlTypes.Count)); NodeDataJson node = CreateNodeJson(NodeType.Action, actionShowTypes[index - controlTypes.Count].FullName, showStrs[index], curMousePos); //创建显示 NodeEditor nodeEditor = new NodeEditor(new Rect(new Vector2((float)node.PosX, (float)node.PosY), new Vector2(200, 100)), node, null, NodeEventCallBack); showNodeEditor.Add(nodeEditor); GUI.changed = true; } }); }