Example #1
0
    void LoadDialog(string[] questDialog)
    {
        ParseMode parseMode = ParseMode.None;

        DialogBase currentDialogEvent = null;
        characterList = new List<Character>();

        int currentBranch = 0;

        int currentChoiceIndex = 0;
        int maxBranch = currentBranch;

        List<int> branchQueue = new List<int> ();
        List<int> priorityQueue = new List<int> ();
        List<int> finishedQueue = new List<int> (); //for pending branches to be merged later

        bool mergePending = false;
        priorityQueue.Add (currentBranch);

        foreach (string dataLine in questDialog) {
            if (dataLine == "<Chara>"){
                parseMode = ParseMode.Character;
                continue;
            } else if (dataLine == "<Dialog>"){
                parseMode = ParseMode.Dialog;
                continue;
            } else if (dataLine == "<Choice>"){
                parseMode = ParseMode.Choice;
                continue;
            } else if (dataLine == "<ChoiceOptions>"){
                parseMode = ParseMode.ChoiceOptions;
                continue;
            }else if (dataLine == "<End>"){
                break;
            } else if (dataLine == "<ChoiceEnd>"){
                finishedQueue.Insert (0, currentBranch);
                priorityQueue.RemoveAt (0);
                currentBranch = priorityQueue[0];

                if (currentBranch == branchQueue[0])
                    mergePending = true;

                continue;
            }

            DialogBase nextEvent = null;
            switch (parseMode){
            case ParseMode.Character:
            {
                string[] parts = dataLine.Split(new char[] {',',':'});
                characterList.Add(new Character(int.Parse(parts[0]), parts[1]));
            }
                break;

            case ParseMode.Dialog:
            {
                string[] parts = dataLine.Split(new char[] {'^'});

                DialogLine newDialogLine = new DialogLine();
                newDialogLine.eventGroup = currentBranch;

                newDialogLine.characterID = int.Parse(parts[0]);
                newDialogLine.dialogLine = parts[1].Replace("<playername>", PlayerProfile.Get ().playerName).Replace("\\n", System.Environment.NewLine);
                newDialogLine.anims = null;

                if (parts.Length > 2){
                    string[] anims = parts[2].Split (new char[]{','});
                    newDialogLine.anims = new string[anims.Length];
                    for (int i = 0; i < anims.Length; ++i){
                        newDialogLine.anims[i] = anims[i];
                    }

                    if (parts.Length > 3){
                        newDialogLine.isLoopAnim = (parts[3] == "L");
                    }
                }

                nextEvent = newDialogLine;
            }
                break;

            case ParseMode.Choice:
            {
                string[] parts = dataLine.Split(new char[] {'^'});

                DialogChoice newChoice = new DialogChoice();

                int branchCount = int.Parse(parts[1]);
                newChoice.eventGroup = currentBranch;

                newChoice.choiceOptions = new string[branchCount];
                newChoice.choiceCost = new int[branchCount];
                newChoice.choiceReward = new int[branchCount];
                newChoice.choiceEnergyReward = new int[branchCount];
                newChoice.choiceMoneyReward = new int[branchCount];

                parseMode = ParseMode.ChoiceOptions;

                currentChoiceIndex = 0;

        //				newChoice.eventGroup = currentBranch;
        //				newChoice.choiceOptions = new string[parts.Length];
        //				newChoice.choiceCost = new int[parts.Length];
        //				newChoice.choiceReward = new int[parts.Length];
        //
        //				int i = 0;
        //
        //				foreach (string choiceLine in parts){
        //					string[] lineParts = choiceLine.Split(new string[]{"::"}, System.StringSplitOptions.None);
        //					newChoice.choiceCost[i] = int.Parse(lineParts[0].Split (':')[1]);
        //					newChoice.choiceReward[i] = int.Parse(lineParts[2].Split (':')[1]);
        //					newChoice.choiceOptions[i++] = lineParts[1];
        //					maxBranch = maxBranch + i;
        //					priorityQueue.Insert(0, maxBranch);
        //				}
        //
        //				branchQueue.Insert(0, currentBranch); //store last branch node
        //				currentBranch = priorityQueue[0]; //remove first node from pq
        //
        //				parseMode = ParseMode.Dialog;
        //				branchQueue.Add (currentBranch);

                nextEvent = newChoice;
            }
                break;

            case ParseMode.ChoiceOptions:
            {
                DialogChoice currentChoice = currentDialogEvent as DialogChoice;

                string[] parts = dataLine.Split(new char[] {'^'});

                currentChoice.choiceCost[currentChoiceIndex] = int.Parse(parts[0]);
                currentChoice.choiceOptions[currentChoiceIndex] = parts[1];
                currentChoice.choiceReward[currentChoiceIndex] = int.Parse (parts[2]);

                currentChoice.choiceMoneyReward[currentChoiceIndex] = int.Parse (parts[3]);
                currentChoice.choiceEnergyReward[currentChoiceIndex] = int.Parse (parts[4]);

                currentChoiceIndex++;
                maxBranch = maxBranch + currentChoiceIndex;
                priorityQueue.Insert(0, maxBranch);

                if (currentChoiceIndex >= currentChoice.choiceCost.Length){

                    branchQueue.Insert(0, currentBranch); //store last branch node
                    currentBranch = priorityQueue[0]; //remove first node from pq

                    parseMode = ParseMode.Dialog;
                }
                continue;

            }
                break;
            }

            if (currentDialogEvent == null){
                    openingDialogEvent = nextEvent;
                }
            else{
                if (currentDialogEvent.eventGroup == nextEvent.eventGroup)
                    currentDialogEvent.nextEvent = nextEvent;
                else{

                    int lastBranch = branchQueue[0];
                    if (lastBranch != priorityQueue[0]) //branch splitting
                    {
                        currentDialogEvent = openingDialogEvent.FindBranch(lastBranch);
        //						while (currentDialogEvent.eventGroup != lastBranch && currentDialogEvent.eventType != DialogBase.EventType.Choice){
        //							currentDialogEvent = currentDialogEvent.nextEvent;
        //						}
                        DialogChoice branchEvent = (DialogChoice)currentDialogEvent;
                        branchEvent.nextEvents.Add (nextEvent);
                    }else{ //branch merging

                        foreach (int finishedIndex in finishedQueue){
                            currentDialogEvent = openingDialogEvent.FindLeaf(finishedIndex);
                            currentDialogEvent.nextEvent = nextEvent;
                        }
                        finishedQueue.Clear ();
                        branchQueue.RemoveAt(0);
                    }
                }

            }
            currentDialogEvent = nextEvent;
        }

        charactersInScene = characterList.ToArray ();

        currentDialogEvent = openingDialogEvent;
        currentDialogEvent.DumpContents ();
        //		while (currentDialogEvent != null) {
        //			DialogLine dialog = currentDialogEvent as DialogLine;
        //			Debug.Log(dialog.dialogLine + "\n");
        //
        //			currentDialogEvent = currentDialogEvent.nextEvent;
        //		}
    }