public void Debug(AgentDesigner agent) { if (agent == null) { MainForm.Instance.ShowMessage("Agent为空"); return; } m_Nodes.Clear(); VerifyInfo verifyAgent = agent.VerifyAgent(); if (verifyAgent.HasError) { MainForm.Instance.ShowMessage("确保行为树编辑正确后才能调试\n" + verifyAgent.Msg); return; } for (int i = 0; i < agent.Nodes.Count; i++) { NodeDesigner node = agent.Nodes[i]; if (node.StartNode) { m_DebugNode = CreateDebugNode(agent, node); break; } } State = DebugState.Running; ContentUserControl.Instance.OnDebugStart(); MainForm.Instance.ShowInfo("播放成功 时间:" + DateTime.Now); }
public void BindAgents() { m_TreeView.Nodes.Clear(); GroupDic.Clear(); for (int i = 0; i < m_Groups.Count; i++) { Group group = m_Groups[i]; BindGroup(group); } //分组优先 for (int i = 0; i < m_Agents.Count; i++) { AgentDesigner agent = m_Agents[i]; if (!string.IsNullOrEmpty(agent.GroupName)) { BindAgent(agent); } } for (int i = 0; i < m_Agents.Count; i++) { AgentDesigner agent = m_Agents[i]; if (string.IsNullOrEmpty(agent.GroupName)) { BindAgent(agent); } } }
public AgentItem BindAgent(AgentDesigner agent) { AgentItem agentItem = new AgentItem(); agentItem.Agent = agent; if (string.IsNullOrEmpty(agent.GroupName)) { TreeNode treeNode = m_TreeView.Nodes.Add(agent.AgentID); treeNode.Tag = agentItem; agentItem.GroupItem = null; agentItem.TreeNode = treeNode; } else { GroupItem groupItem = FindGroup(agent.GroupName); //添加分组 if (groupItem == null) { groupItem = AddGroup(agent.GroupName); } TreeNode treeNode = groupItem.TreeNode.Nodes.Add(agent.AgentID); treeNode.Tag = agentItem; agentItem.GroupItem = groupItem; agentItem.TreeNode = treeNode; } return(agentItem); }
public EditAgentForm(AgentDesigner agent) { m_Agent = agent; m_AgentContent = XmlUtility.ObjectToString(m_Agent); m_EditAgent = XmlUtility.StringToObject <AgentDesigner>(m_AgentContent); InitializeComponent(); }
public void DeleteGroup(Group group) { foreach (var kv in GroupDic) { if (kv.Value.Group == group) { kv.Value.TreeNode.Remove(); } } if (GroupDic.ContainsKey(group.GroupName)) { GroupDic.Remove(group.GroupName); } m_Groups.Remove(group); for (int i = m_Agents.Count - 1; i >= 0; i--) { AgentDesigner agent = m_Agents[i]; if (agent.GroupName == group.GroupName) { m_Agents.RemoveAt(i); } } }
/// <summary> /// 判断Agent是否存在 /// </summary> /// <param name="agent">agent</param> /// <returns>true:存在</returns> public bool ExistAgent(AgentDesigner agent) { if (agent == null) { throw new Exception("BehaviorTreeData.ExistAgent() error: agent = null"); } for (int i = 0; i < m_Agents.Count; i++) { AgentDesigner tempAgent = m_Agents[i]; if (tempAgent == null) { continue; } if (tempAgent == agent) { return(true); } if (tempAgent.AgentID == agent.AgentID) { return(true); } } return(false); }
/// <summary> /// 粘贴行为树 /// </summary> private void PasteAgent() { try { AgentListContent content = XmlUtility.StringToObject <AgentListContent>(Clipboard.GetText()); for (int i = 0; i < content.DataList.Count; i++) { AgentDesigner agent = content.DataList[i]; string agentID = "NewAgent_" + DateTime.Now.Ticks; do { agentID = "NewAgent_" + DateTime.Now.Ticks; } while (TreeData.ExistAgent(agentID)); agent.AgentID = agentID; TreeViewManager.AddAgent(agent); } ShowInfo("您粘贴了" + content.DataList.Count + "棵行为树!!!"); } catch (Exception ex) { ShowInfo("无法进行粘贴,错误信息:" + ex.Message); ShowMessage("无法进行粘贴,错误信息:" + ex.Message, "警告"); } }
public void RemoveAgent(string agentID) { for (int i = 0; i < m_Agents.Count; i++) { AgentDesigner agent = m_Agents[i]; if (agent.AgentID == agentID) { m_Agents.RemoveAt(i); break; } } AgentItem agentItem = FindAgentItem(agentID); if (agentItem == null) { return; } int index = GetIndex(agentItem); m_TreeView.Nodes.Remove(agentItem.TreeNode); if (index >= 0 && index < m_TreeView.Nodes.Count) { m_TreeView.SelectedNode = m_TreeView.Nodes[index]; } }
/// <summary> /// 检验AgentID /// </summary> /// <returns></returns> public VerifyInfo VerifyAgentID() { //校验ID是否为空 for (int i = 0; i < m_Agents.Count; i++) { AgentDesigner agent = m_Agents[i]; if (string.IsNullOrEmpty(agent.AgentID)) { return(new VerifyInfo("行为树空的AgentID")); } } //检验AgentID是否相同 for (int i = 0; i < m_Agents.Count; i++) { AgentDesigner agent_i = m_Agents[i]; if (agent_i != null) { for (int ii = i + 1; ii < m_Agents.Count; ii++) { AgentDesigner agent_ii = m_Agents[ii]; if (agent_i.AgentID == agent_ii.AgentID) { return(new VerifyInfo(string.Format("行为树存在相同AgentID:{0}", agent_i.AgentID))); } } } } return(VerifyInfo.DefaultVerifyInfo); }
public static BehaviorTreeData.NodeData CreateNode(AgentDesigner agent, NodeDesigner nodeData) { BehaviorTreeData.NodeData data = new BehaviorTreeData.NodeData(); data.ID = nodeData.ID; data.ClassType = nodeData.ClassType; data.Label = nodeData.Label; data.X = (int)nodeData.Rect.x; data.Y = (int)nodeData.Rect.y; for (int i = 0; i < nodeData.Fields.Count; i++) { FieldDesigner field = nodeData.Fields[i]; if (field == null) { continue; } data.Fields.Add(CreateField(field)); } if (nodeData.Transitions.Count > 0) { data.Childs = new List <BehaviorTreeData.NodeData>(nodeData.Transitions.Count); for (int i = 0; i < nodeData.Transitions.Count; i++) { Transition transition = nodeData.Transitions[i]; NodeDesigner childNode = agent.FindByID(transition.ToNodeID); BehaviorTreeData.NodeData childNodeData = CreateNode(agent, childNode); data.Childs.Add(childNodeData); } } return(data); }
//辅助Agent添加节点(粘贴添加) public static void AddNode(AgentDesigner agent, NodeDesigner node) { node.ID = agent.GenNodeID(); if (node.StartNode) { node.StartNode = agent.ExistStartNode() ? false : true; } agent.AddNode(node); if (node.Transitions.Count > 0) { for (int i = 0; i < node.Transitions.Count; i++) { Transition transition = node.Transitions[i]; transition.FromNode = node; transition.FromNodeID = node.ID; NodeDesigner childNode = transition.ToNode; AddNode(agent, childNode); transition.ToNodeID = childNode.ID; } node.Sort(); } }
/// <summary> /// 设置选中的agent /// </summary> /// <param name="agent"></param> public void SetSelectedAgent(AgentDesigner agent) { SelectedAgent = agent; if (m_ContentUserControl != null) { m_ContentUserControl.SetSelectedAgent(SelectedAgent); } }
public AgentItem AddAgent(AgentDesigner agent) { m_Agents.Add(agent); AgentItem agentItem = BindAgent(agent); m_TreeView.SelectedNode = agentItem.TreeNode; return(agentItem); }
public void UpdateAgent(AgentDesigner agent) { AgentItem agentItem = FindAgent(agent); if (agentItem != null) { agentItem.TreeNode.Text = agent.AgentID; } }
//刷新Agent private void UpdateAgent(AgentDesigner agent) { if (agent == null) { return; } TreeViewManager.UpdateAgent(agent); }
/// <summary> /// 修正数据(和模板保持一致) /// </summary> public void AjustData() { for (int i = 0; i < m_Agents.Count; i++) { AgentDesigner agent = m_Agents[i]; if (agent != null) { agent.AjustData(); } } }
/// <summary> /// 移除未定义的节点 /// </summary> public void RemoveUnDefineNode() { for (int i = 0; i < m_Agents.Count; i++) { AgentDesigner agent = m_Agents[i]; if (agent != null) { agent.RemoveUnDefineNode(); } } }
/// <summary> /// 修改节点名字 /// </summary> /// <param name="old">旧的classType</param> /// <param name="newType">新的classType</param> public void UpdateClassType(string old, string newType) { for (int i = 0; i < m_Agents.Count; i++) { AgentDesigner agent = m_Agents[i]; if (agent != null) { agent.UpdateClassType(old, newType); } } }
/// <summary> /// 添加Agent /// </summary> /// <param name="agent">agent</param> /// <returns>true:添加成功</returns> public bool AddAgent(AgentDesigner agent) { if (ExistAgent(agent)) { return(false); } m_Agents.Add(agent); return(true); }
/// <summary> /// 添加行为树Item /// </summary> /// <param name="agent">agent</param> /// <returns>true:添加成功</returns> public bool AddAgentItem(AgentDesigner agent) { if (agent == null) { return(false); } TreeNode treeNode = treeView1.Nodes.Add(agent.AgentID); treeNode.Tag = agent; treeView1.SelectedNode = treeNode; return(true); }
/// <summary> /// 更新Agent内容,但Nodes不能改 /// </summary> /// <param name="agent"></param> public void UpdateAgent(AgentDesigner agent) { if (agent == this) { return; } m_AgentID = agent.AgentID; m_Describe = agent.Describe; m_Fields.Clear(); m_Fields.AddRange(agent.Fields.ToArray()); }
//放到最后 public void AddLast(AgentDesigner agent) { for (int i = 0; i < m_Agents.Count; i++) { AgentDesigner temp = m_Agents[i]; if (temp.AgentID == agent.AgentID) { m_Agents.RemoveAt(i); break; } } m_Agents.Add(agent); }
public static BehaviorTreeData.TreeData CreateTreeData(TreeData treeData) { BehaviorTreeData.TreeData data = new BehaviorTreeData.TreeData(); for (int i = 0; i < treeData.Agents.Count; i++) { AgentDesigner agent = treeData.Agents[i]; if (agent == null) { continue; } data.Agents.Add(CreateAgentData(agent)); } return(data); }
public void UpdateGroup(string oldName, Group group) { foreach (var kv in GroupDic) { if (kv.Value.Group == group) { kv.Value.TreeNode.Text = group.GroupName; } } for (int i = 0; i < m_Agents.Count; i++) { AgentDesigner agent = m_Agents[i]; if (agent.GroupName == oldName) { agent.GroupName = group.GroupName; } } }
/// <summary> /// 删除Agent /// </summary> private void DeleteAgent() { if (treeView1.SelectedNode == null) { return; } if (!(treeView1.SelectedNode.Tag is AgentItem)) { return; } AgentDesigner agent = (treeView1.SelectedNode.Tag as AgentItem).Agent; if (MessageBox.Show(string.Format("确定删除行为树{0}吗?", agent.AgentID), "提示", MessageBoxButtons.OKCancel) == DialogResult.OK) { TreeViewManager.RemoveAgent(agent.AgentID); } }
public void LoadBehaviorTreeData() { //读取行为树数据 TreeData = XmlUtility.Read <TreeData>(GetBehaviorTreeDataPath()); if (TreeData == null) { TreeData = new TreeData(); XmlUtility.Save(GetBehaviorTreeDataPath(), TreeData); } BehaviorTreeDataStringContent = XmlUtility.ObjectToString(TreeData); if (TreeData.Agents.Count > 0) { for (int i = 0; i < TreeData.Agents.Count; i++) { AgentDesigner agent = TreeData.Agents[i]; if (agent != null) { if (agent.Nodes.Count > 0) { for (int ii = 0; ii < agent.Nodes.Count; ii++) { NodeDesigner node = agent.Nodes[ii]; if (node.Transitions.Count > 0) { for (int iii = 0; iii < node.Transitions.Count; iii++) { Transition transition = node.Transitions[iii]; NodeDesigner fromNode = agent.FindNodeByID(transition.FromNodeID); NodeDesigner toNode = agent.FindNodeByID(transition.ToNodeID); transition.Set(toNode, fromNode); } } } } } } } }
/// <summary> /// 获取绑定指定数据的TreeNode /// </summary> /// <param name="obj">Agent</param> /// <returns></returns> public TreeNode GetTreeNode(AgentDesigner agent) { if (agent == null) { return(null); } for (int i = 0; i < treeView1.Nodes.Count; i++) { TreeNode treeNode = treeView1.Nodes[i]; if (treeNode == null) { continue; } if (treeNode.Tag == agent) { return(treeNode); } } return(null); }
//把agent1插入到agent2的前面或者后面 public void InsertAgent(AgentDesigner agent1, AgentDesigner agent2) { int index1 = 0; int index2 = 0; for (int i = 0; i < m_Agents.Count; i++) { AgentDesigner agent = m_Agents[i]; if (agent.AgentID == agent1.AgentID) { index1 = i; } if (agent.AgentID == agent2.AgentID) { index2 = i; } } m_Agents.RemoveAt(index1); m_Agents.Insert(index2, agent1); }
/// <summary> /// 校验行为树数据 /// </summary> /// <returns></returns> public VerifyInfo VerifyBehaviorTree() { VerifyInfo verifyAgentID = VerifyAgentID(); if (verifyAgentID.HasError) { return(verifyAgentID); } for (int i = 0; i < m_Agents.Count; i++) { AgentDesigner agent = m_Agents[i]; if (agent != null) { VerifyInfo verifyAgent = agent.VerifyAgent(); if (verifyAgent.HasError) { return(verifyAgent); } } } return(VerifyInfo.DefaultVerifyInfo); }
//交换Agent public void SwapAgent(AgentDesigner agent1, AgentDesigner agent2) { int index1 = 0; int index2 = 0; for (int i = 0; i < m_Agents.Count; i++) { AgentDesigner agent = m_Agents[i]; if (agent.AgentID == agent1.AgentID) { index1 = i; } if (agent.AgentID == agent2.AgentID) { index2 = i; } } AgentDesigner tempAgentDisigner = m_Agents[index1]; m_Agents[index1] = m_Agents[index2]; m_Agents[index2] = tempAgentDisigner; }