void Start()
        {
            var uiControllerObject = GameObject.FindWithTag("UIController");

            _dialoguePanel = uiControllerObject.GetComponent <UIController>().DialoguePanel;
            string dialogueJson = Resources.Load <TextAsset>(dialoguePath).text;

            dialogue = DialogueParser.Parse(dialogueJson);
        }
Пример #2
0
        public static DialogueGraph Parse(string json)
        {
            var dialogueJson            = JSON.Parse(json);
            Queue <JSONNode> nodesQueue = new Queue <JSONNode>();

            var dialogueIdNode = _getField(dialogueJson, "dialogue_id");

            _validateType(dialogueIdNode, "dialogue_id");

            var dialogue = new DialogueGraph(dialogueIdNode.Value);

            nodesQueue.Enqueue(dialogueJson);
            while (nodesQueue.Count != 0)
            {
                var currentNode = nodesQueue.Dequeue();

                int id = _getId(currentNode);

                var invitationNode = _getField(currentNode, "invitation");
                var answersNode    = _getField(currentNode, "answers");

                _validateType(invitationNode, "invitation");
                _validateType(answersNode, "answers");

                string    invitation  = invitationNode.Value;
                JSONArray jsonAnswers = answersNode.AsArray;

                DialogueAnswer[] answers = new DialogueAnswer[jsonAnswers.Count];

                for (int i = 0; i < jsonAnswers.Count; i++)
                {
                    JSONNode answer = jsonAnswers[i];
                    _validateType(answer, "answer");

                    var messageNode = _getField(answer, "message");
                    int next        = id; //By default next equals current node id

                    _validateType(messageNode, "message");

                    if (answer.HasKey("next"))
                    {
                        var nextNode = answer["next"];
                        if (nextNode.IsNumber)
                        {
                            next = nextNode.AsInt;
                        }
                        else if (nextNode.IsNull)
                        {
                            //-1 - выход из диалога
                            next = -1; //TODO Наверное лучше использовать тип (int?)
                        }
                        else if (nextNode.IsObject)
                        {
                            next = _getId(nextNode);
                            nodesQueue.Enqueue(nextNode);
                        }
                    }

                    answers[i] = new DialogueAnswer(messageNode.Value, next);

                    if (!answer.HasKey("condition"))
                    {
                        continue;
                    }

                    var conditionNode = answer["condition"];
                    _validateType(conditionNode, "condition");

                    var condition = AnswerConditionParser.Parse(conditionNode);

                    answers[i].SetCondition(condition);
                }
                dialogue.addNode(id, new DialogueNode(invitation, answers));
            }
            return(dialogue);
        }