Beispiel #1
0
    private static Dictionary <string, DialogNode> LoadResource()
    {
        string json = (Resources.Load("dialogs") as TextAsset).text;
        Dictionary <string, object> jsonObj = Json.Deserialize(json) as Dictionary <string, object>;

        Dictionary <long, DialogNode> nodeLookup = new Dictionary <long, DialogNode>();

        List <object> dialogNodes = (List <object>)jsonObj["data"];

        foreach (Dictionary <string, object> node in dialogNodes)
        {
            DialogNode n    = null;
            string     type = (string)node["type"];
            if (type.Equals("text"))
            {
                n = DialogTextNode.Parse(node);
            }
            else if (type.Equals("option"))
            {
                n = DialogOptionNode.Parse(node);
            }

            if (n == null)
            {
                throw new System.Exception("unknown node type " + type);
            }

            nodeLookup[n.id] = n;
        }

        // Construct the tree
        foreach (Dictionary <string, object> node in dialogNodes)
        {
            long id = (long)node["id"];
            if (node.ContainsKey("children") && node["children"] != null)
            {
                List <object> childrenIds = ((List <object>)node["children"]);
                DialogNode    dNode       = nodeLookup[id];
                for (int i = 0; i < childrenIds.Count; i++)
                {
                    dNode.children[i] = nodeLookup[(long)childrenIds[i]];
                }
            }
        }

        Dictionary <string, DialogNode> result = new Dictionary <string, DialogNode>();

        foreach (DialogNode node in nodeLookup.Values)
        {
            if (!result.ContainsKey(node.npc))
            {
                result[node.npc] = node;
            }
            else
            {
                if (node.id < result[node.npc].id)
                {
                    result[node.npc] = node;
                }
            }
        }

        return(result);
    }
Beispiel #2
0
    public IEnumerator Talk(MemoryList memoryList, DialogDisplay dialogBox, OnDialogFinish onDialogFinish)
    {
        bool keepTalking = true;

        while (keepTalking)
        {
            if (node == null)
            {
                keepTalking = false;
            }
            else if (node is DialogTextNode)
            {
                DialogTextNode textNode = (DialogTextNode)node;
                if (textNode.requireItems == null || memoryList.Contains(textNode.requireItems))
                {
                    yield return(dialogBox.DisplayText(textNode.dialogs));

                    if (textNode.addItems != null)
                    {
                        foreach (string item in textNode.addItems)
                        {
                            memoryList.AddItem(item);
                        }
                    }
                    if (node.children != null)
                    {
                        node = node.children[0];
                        if (node.id == 999)
                        {
                            GameObject.FindGameObjectWithTag("GameController").GetComponent <GameManager>().GameEnd();
                            onDialogFinish();
                            yield break;
                        }
                    }
                    else
                    {
                        node = null;
                    }
                }
                else
                {
                    yield return(dialogBox.DisplayText(new string[] { "Hi Ben!" }));
                }
                keepTalking = false;
            }
            else if (node is DialogOptionNode)
            {
                DialogOptionNode optionNode       = (DialogOptionNode)node;
                string[]         availableOptions = optionNode.options
                                                    .Where((option) => !option.selected && (option.requireItems == null || memoryList.Contains(option.requireItems)))
                                                    .Select((option) => option.text).ToArray();

                if (availableOptions.Length > 0)
                {
                    yield return(dialogBox.DisplayOptionList(availableOptions, (selection) => {
                        node = optionNode.Select(selection);
                    }));

                    if (optionNode == node)
                    {
                        keepTalking = false;
                    }
                }
                else
                {
                    yield return(dialogBox.DisplayText(new string[] { "Hi Ben!" }));

                    keepTalking = false;
                }
            }
            yield return(null);
        }
        onDialogFinish();
    }