Esempio n. 1
0
    public Interaction(TextAsset file)
    {
        string[] lines = Regex.Split (file.text, @"\r\n|\r|\n");
        dialogueList = new List<InteractionLinkedTreeNode>();
        controlList = new List<InteractionControlPath>();
        name = file.name.Split ('.') [0];
        int lineCounter = 1;

        foreach (string line in lines)
        {
            // ignore empty lines and comments
            if (line.Length == 0)
                continue;
            if (line.StartsWith("#"))
                continue;

            // conditional statements go here
            // If the statement is true, then the line of dialogue directly below
            //  will be the first line of the conversation
            if (line.StartsWith("if "))
            {
                string conditional = line.Substring("if ".Length);
                InteractionControlPath ic = new InteractionControlPath();
                if (conditional.IndexOf("choose ") != -1) {
                    string chooseStr = conditional.Substring(conditional.IndexOf("choose "));
                    string[] chooseParams = chooseStr.Substring("choose ".Length).Split(',');
                    ic.randomStart = Int32.Parse(chooseParams[0]);
                    ic.randomEnd = Int32.Parse(chooseParams[1]);
                    conditional = conditional.Substring(0, conditional.Length - chooseStr.Length);
                }
                ic.statement = conditional;
                ic.lineNumber = lineCounter;
                controlList.Add(ic);
            }
            else if (line.StartsWith ("choose "))
            {
                string[] chooseParams = line.Substring("choose ".Length).Split(',');
                InteractionControlPath ic = new InteractionControlPath();
                ic.randomStart = Int32.Parse(chooseParams[0]);
                ic.randomEnd = Int32.Parse(chooseParams[1]);
                chooseStatement = ic;
            }
            else // otherwise we assume it's a line of dialogue
            {
                InteractionTreeNode linkedNode = new InteractionLinkedTreeNode();
                int lineIndex = 0;
                ParseDialogueLine(line, ref linkedNode, ref lineIndex);
                if (linkedNode == null)
                {
                    throw new LineException("Bad format in line "+lineCounter+" in file "+file.name);
                }
                else
                {
                    while(true)
                    {
                        if (lineIndex >= line.Length || line.IndexOf('[', lineIndex) == -1)
                            break;
                        InteractionTreeNode node = new InteractionTreeNode();
                        ParseDialogueLine(line, ref node, ref lineIndex);
                        if (node == null)
                            throw new LineException("Bad format for line "+lineCounter+" in file "+file.name);
                        else
                            ((InteractionLinkedTreeNode)linkedNode).responses.Add(node);
                    }
                }

                dialogueList.Add((InteractionLinkedTreeNode)linkedNode);
                lineCounter++;
            }
        }
    }
Esempio n. 2
0
    private void ParseDialogueLine(string fullLine, ref InteractionTreeNode node, ref int index)
    {
        int lineStartInd = fullLine.IndexOf('[', index);
        int lineEndInd = fullLine.IndexOf(']', lineStartInd+1);
        if (lineStartInd == -1 || lineEndInd == -1)
        {
            node = null;
            return;
        }

        int eventStartInd = fullLine.IndexOf('{', lineStartInd+1);
        if (!(eventStartInd == -1 || eventStartInd > lineEndInd))
        {
            node.line = fullLine.Substring(lineStartInd+1, (eventStartInd-lineStartInd)-1);

            int eventEndInd = fullLine.IndexOf ('}', eventStartInd+1);
            string eventText = fullLine.Substring(eventStartInd+1, (eventEndInd-eventStartInd)-1);
            string[] eventParams = eventText.Split(new char[]{','}, StringSplitOptions.RemoveEmptyEntries);
            foreach (string eventParam in eventParams)
            {
                string[] eventArgs = eventParam.Split(new char[]{' '}, StringSplitOptions.RemoveEmptyEntries);
                switch(eventArgs[0])
                {
                case "End":
                case "end":
                    node.eventType |= InteractionEvents.End;
                    break;
                case "Line":
                case "line":
                    node.eventType |= InteractionEvents.NextLine;
                    node.nextLine = Int32.Parse(eventArgs[1]);
                    break;
                case "Set":
                case "set":
                    node.eventType |= InteractionEvents.SetConst;
                    node.gameConstant = eventArgs[1];
                    break;
                case "Event":
                case "event":
                    node.eventType |= InteractionEvents.Special;
                    node.gameEvent = eventArgs[1];
                    break;
                }
            }
        }
        else
            node.line = fullLine.Substring(lineStartInd+1, (lineEndInd-lineStartInd)-1);

        index = lineEndInd + 1;
    }