コード例 #1
0
    public void StepDialogue(int choice)
    {
        while (true)
        {
            if (curIndex == -1)
            {
                isBlocking = false;
                return;
            }

            // if there are no more commands, throw an error. last thing in list should be Exit command.
            Command command = commandList[curIndex] as Command;
            if (curIndex < 0 || curIndex >= commandList.Count)
            {
                Debug.LogError(string.Format("Line {0} contains no command in text '{1}'", curIndex, assetLocation));
                curIndex   = -1;
                isBlocking = false;
                return;
            }

            if (command.commandType == "NONE" || command.commandType == "OPTION")
            {
                Debug.Log("NONE");
                curIndex  += 1;
                isBlocking = false;
            }
            else if (command.commandType == "GOTO")
            {
                string gotoLabelName  = command.arguments[0] as string;
                int    gotoLabelIndex = labelIndexDict[gotoLabelName] + 1;
                Debug.Log(string.Format("[GOTO] cur: {0}, next: {1}", curIndex, gotoLabelIndex));
                curIndex   = gotoLabelIndex;
                isBlocking = false;
            }
            else if (command.commandType == "SAY")
            {
                string name     = command.arguments[0] as string;
                string dialogue = command.arguments[1] as string;
                Debug.Log(string.Format(
                              "[SAY] {0}: {1}",
                              name,
                              dialogue
                              ));
                ChatController.Show(name + ": " + dialogue);
                curIndex  += 1;
                isBlocking = true;
                return;
            }
            else if (command.commandType == "CHOOSE")
            {
                if (!isAwaitingInput)                 // no choice made... yet
                {
                    string label    = command.arguments[0] as string;
                    string question = command.arguments[1] as string;
                    Debug.Log(string.Format(
                                  "[CHOOSE] ({0}) {1}; {2}",
                                  label,
                                  question,
                                  choice
                                  ));

                    //accumulate all question options
                    int       optionIndex = curIndex + 1;
                    ArrayList options     = new ArrayList();
                    string    text        = question;
                    while (true)
                    {
                        Command cmdOption = commandList[optionIndex] as Command;
                        if (cmdOption.commandType != "OPTION")
                        {
                            break;
                        }
                        options.Add(cmdOption.arguments[0] as string);
                        optionIndex += 1;
                    }

                    // propose the choice
                    ChatController.Choose(question, options);
                    isBlocking      = true;
                    isAwaitingInput = true;
                    return;
                }
                else
                {
                    string label         = command.arguments[0] as string;
                    string gotoLabelName = label + " " + (choice + 1);

                    int gotoLabelIndex = labelIndexDict[gotoLabelName] + 1;
                    Debug.Log(string.Format("[CHOOSE] cur: {0}, next: {1}", curIndex, gotoLabelIndex));
                    curIndex        = gotoLabelIndex;
                    isBlocking      = false;
                    isAwaitingInput = false;
                    return;
                }
            }
            else if (command.commandType == "EXIT")
            {
                Debug.Log("[EXIT]");
                curIndex   = -1;
                isBlocking = false;
                return;
            }
            else
            {
                Debug.LogError(string.Format(
                                   "Line {0} contains unknown function {1} in text '{2}'",
                                   curIndex,
                                   command.commandType,
                                   assetLocation
                                   ));
            }
        }
    }