public C_DialogueNode GetNextSiblingNode(C_DialogueNode theNode, bool parentSibling = true) { C_DialogueNode returnNode = null; int tempIndex = theNode.parentNode.listOfChildNodes.IndexOf(theNode); if(tempIndex+1 < theNode.parentNode.listOfChildNodes.Count) { // Get next sibling instead returnNode = theNode.parentNode.listOfChildNodes[tempIndex+1]; return returnNode; } else { if(theNode.parentNode.dialogueType == E_DialogueType.NPC || !parentSibling) return returnNode; // Get parent's next sibling return GetNextSiblingNode(theNode.parentNode); } }
public C_DialogueNode GetQuestionNode(C_DialogueNode theNode, int questionNo) { if (theNode.dialogueType == E_DialogueType.Question && theNode.question == questionNo) { return theNode; } else { if(theNode.parentNode != null) return GetQuestionNode(theNode.parentNode,questionNo); else return null; } }
C_DialogueNode SetNodeAttributes(XmlNode node) { C_DialogueNode returnNode = new C_DialogueNode(); foreach(XmlAttribute attribute in node.Attributes) { switch(attribute.LocalName) { case C_Constants.D_A_NAME: returnNode.name = attribute.Value; break; case C_Constants.D_A_CONDITION: returnNode.conditions = attribute.Value; returnNode.SetConditions(); break; case C_Constants.D_A_QUESTION: returnNode.question = Convert.ToInt32(attribute.Value); break; case C_Constants.D_A_SENTENCE: returnNode.sentence = attribute.Value; break; case C_Constants.D_A_CHOICES: returnNode.choices = Convert.ToInt32(attribute.Value); break; default: Debug.LogWarning("Unknown Attribute Name in Dialogue Script > " + node.LocalName); break; } } return returnNode; }
// Use this for initialization void Start() { if(dialogueTree == null) CreateNodeTree (); // This needs to call on each Start foreach(C_DialogueNode chapter in dialogueTree) { if(Convert.ToInt32(chapter.name) == currentChapter.chapterNo) { chapterNode = chapter; foreach(C_DialogueNode scene in chapter.listOfChildNodes) { if(scene.name == currentChapter.scene) { sceneNode = scene; break; } } break; } } }
void CreateNodeTree() { dialogueTree = new List<C_DialogueNode>(); XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(xmlFile.text); XmlNodeList chapterList = xmlDoc.GetElementsByTagName(C_Constants.D_E_CHAPTER); foreach (XmlNode chapterNode in chapterList) { // Add chapter node into dialogueTree C_DialogueNode tempChapterNode = new C_DialogueNode(); tempChapterNode.dialogueType = E_DialogueType.Chapter; tempChapterNode.name = chapterNode.Attributes[C_Constants.D_A_NAME].Value; tempChapterNode.parentNode = null; // Create list of scenes in chapter XmlNodeList sceneList = chapterNode.ChildNodes; foreach(XmlNode sceneNode in sceneList) { if(sceneNode.NodeType != XmlNodeType.Comment) { // Add scene node into dialogueTree C_DialogueNode tempSceneNode = new C_DialogueNode(); tempSceneNode.dialogueType = E_DialogueType.Scene; tempSceneNode.name = sceneNode.Attributes[C_Constants.D_A_NAME].Value; tempSceneNode.parentNode = tempChapterNode; // If have NPCs if(sceneNode.HasChildNodes) { // Create list of npcs in chapter XmlNodeList npcList = sceneNode.ChildNodes; // foreach loop again !!! foreach(XmlNode npcNode in npcList) { if(npcNode.NodeType != XmlNodeType.Comment) { // Add scene node into dialogueTree C_DialogueNode tempNpcNode = new C_DialogueNode(); tempNpcNode.dialogueType = E_DialogueType.NPC; tempNpcNode.name = npcNode.Attributes[C_Constants.D_A_NAME].Value; tempNpcNode.parentNode = tempSceneNode; // nested foreach loop for the dialogue elements // the nested foreach loop needs to know when to break out AddChildNode(npcNode,tempNpcNode); // at end of foreach loop tempSceneNode.listOfChildNodes.Add(tempNpcNode); } } } // at end of foreach loop tempChapterNode.listOfChildNodes.Add(tempSceneNode); } } dialogueTree.Add(tempChapterNode); } }
void AddChildNode( XmlNode parentNode, C_DialogueNode parentDialogueNode) { if (!parentNode.HasChildNodes) return; XmlNodeList childList = parentNode.ChildNodes; foreach(XmlNode childNode in childList) { // Ignore comments, maybe also text if(childNode.NodeType == XmlNodeType.Element) // != XmlNodeType.Comment) { // Add scene node into dialogueTree C_DialogueNode tempChildNode = ConvertToDialogueNode(childNode); tempChildNode.parentNode = parentDialogueNode; // Recursion here ( MAY HAVE A PROBLEM SETTING VALUES, SINCE VALUES ARE SET AFTER RECURSION ) AddChildNode(childNode,tempChildNode); parentDialogueNode.listOfChildNodes.Add(tempChildNode); } } }
public bool SetNodeToNextSibling(C_DialogueNode theNode = null) { if (theNode == null) theNode = currentNode; int tempIndex = theNode.parentNode.listOfChildNodes.IndexOf(theNode); if(tempIndex+1 < theNode.parentNode.listOfChildNodes.Count) { // Get next sibling instead currentNode = theNode.parentNode.listOfChildNodes[tempIndex+1]; return true; } else { if(theNode.parentNode.dialogueType == E_DialogueType.NPC) return false; // Get parent's next sibling return SetNodeToNextSibling(theNode.parentNode); } }
public void SetCurrentNode(string npcName) { foreach(C_DialogueNode npc in sceneNode.listOfChildNodes) { if(npc.name == npcName) { currentNode = npc.listOfChildNodes[0]; return; } } }
public bool MoveToNextNode() { if (currentNode.listOfChildNodes.Count > 0) { // Get first child currentNode = currentNode.listOfChildNodes [0]; return true; } else { // Recursive loop may start here return SetNodeToNextSibling(currentNode); } }