Exemplo n.º 1
0
    public void parseDialogueFromXML()
    {
        /* Each line in set are linked sequentically, and may be condiationally linked
         * to any line in any set within actors context. Thus we need to double check linking
         * on lines already parsed and lines not yet parsed to link to both directions */

        bool      firstLine = true;                           // ensures that set starting line is not linked to last line of other set
        ArrayList allLines  = new ArrayList();                // needed for backward linking
        ArrayList conditionalUnlinkedLines = new ArrayList(); // needed for forward linking

        XmlDocument xmlDoc = new XmlDocument();

        xmlDoc.Load("dialogue.xml");          // Peliprojekti/dialogue.xml

        XmlNodeList actors = xmlDoc.GetElementsByTagName("actor");

        foreach (XmlNode actor in actors)
        {
            actorCount++;
            dialogueCount++;

            string        actorName     = actor.Attributes["name"].Value;
            ActorDialogue actorDialogue = new ActorDialogue(actorName);

            XmlNodeList dialogueSetNodes = actor.ChildNodes;
            foreach (XmlNode dialogueSetNode in dialogueSetNodes)             // Actor's all sets
            {
                setCount++;

                firstLine = true;                 // new set started, thus first processed line is marked a first line, set to false in the end

                int         setId       = int.Parse(dialogueSetNode.Attributes["setId"].Value);
                DialogueSet dialogueSet = new DialogueSet(setId);

                XmlNodeList  dialogueLineNodes = dialogueSetNode.ChildNodes;
                DialogueLine previousLine      = null;                  // for sequential linking
                foreach (XmlNode dialogueLineNode in dialogueLineNodes) // Set's all lines
                {
                    lineCount++;

                    int    lineId = int.Parse(dialogueLineNode.Attributes["id"].Value);
                    string text   = dialogueLineNode.InnerText;

                    DialogueLine dialogueLine = new DialogueLine(text, lineId);
                    dialogueLine.setSetId(dialogueSet.getId());                     // line must know it's set for backward linking
                    allLines.Add(dialogueLine);

                    // Linking sequently lines
                    if (previousLine != null && !firstLine)                     // Don't link to previous set
                    {
                        previousLine.setNextLine(dialogueLine);
                    }

                    // look for branching token
                    if (dialogueLineNode.Attributes["branchInfo"] != null)
                    {
                        dialogueLine.setChoice(true);

                        // parse branch info
                        string   branchInfo   = dialogueLineNode.Attributes["branchInfo"].Value;
                        char[]   delimiter    = { '/' };
                        string[] values       = branchInfo.Split(delimiter);
                        int      branchSetId  = int.Parse(values[0]);
                        int      branchLineId = int.Parse(values[1]);

                        if (branchSetId > dialogueSet.getId() || branchLineId > dialogueLine.getId())
                        {
                            // Prepare for forward linking
                            dialogueLine.setBranchLinkingInfo(branchInfo);
                            conditionalUnlinkedLines.Add(dialogueLine);
                        }

                        else
                        {
                            // Perform backward linking
                            foreach (DialogueLine oldLine in allLines)
                            {
                                if (oldLine.getSetId() == branchSetId && dialogueLine.getId() == branchLineId)
                                {
                                    dialogueLine.setAltLine(oldLine);
                                    break;
                                }
                            }
                        }
                    }

                    // Try to forward link pending lines on this line

                    DialogueLine toRemove = null;                     // Remove linked conditionalLine from pending list AFTER iteration to avoid problems
                    foreach (DialogueLine conditionalLine in conditionalUnlinkedLines)
                    {
                        int branchLineId = conditionalLine.getBranchLineId();
                        int branchSetId  = conditionalLine.getBranchSetId();

                        if (branchLineId == dialogueLine.getId() &&
                            branchSetId == dialogueSet.getId())
                        {
                            conditionalLine.setAltLine(dialogueLine);
                            toRemove = conditionalLine;
                        }
                    }

                    // toRemove was linked, remove from forward linking list
                    if (toRemove != null)
                    {
                        conditionalUnlinkedLines.Remove(toRemove);                         // Iteration done, safe to remove
                    }
                    /// ---------------------------------------------------------------

                    previousLine = dialogueLine;
                    dialogueSet.addDialogueLine(dialogueLine);
                    firstLine = false;                     // new lines may be coming in same set, they are not first line
                    //print ("LINE ADDED");
                }

                actorDialogue.addDialogueSet(dialogueSet);
                //print ("SET ADDED");
            }

            //print ("DIALOGUE TO BE ADDED");
            dialogue.Add(actorDialogue.getActorName(), actorDialogue);
            //print ("DIALOGUE ADDED");
        }
    }