Ejemplo n.º 1
0
        /// <summary>
        /// Loads a dialogue map into the editor.
        /// </summary>
        private void Load()
        {
            _loading = true;
            Nodes.Clear();
            Connections.Clear();
            var path = EditorUtility.OpenFilePanel("Open Dialogue", "/Resources/", "json");

            if (path == "")
            {
                return;
            }
            string       json = File.ReadAllText(path);
            DialogueSave save = JsonConvert.DeserializeObject <DialogueSave>(json);

            foreach (var node in save.Nodes)
            {
                var tmpNode = new Node(
                    node.Id,
                    node.Rect.position.x,
                    node.Rect.position.y,
                    node.Rect.width,
                    node.Rect.height,
                    OnNodeConnectorClicked,
                    RemoveNode,
                    SetStartingNode,
                    SetEndingNode
                    );
                tmpNode.First           = node.First;
                tmpNode.Last            = node.Last;
                tmpNode.Message         = node.Message;
                tmpNode.Character       = Resources.Load <CharacterInformation>(node.CharacterPath.Replace(".asset", "").Replace("Assets/Resources/", ""));
                tmpNode.Options         = node.Options;
                tmpNode.Outputs         = node.Outputs;
                tmpNode.Input           = node.Input;
                tmpNode.Input.OwnerNode = tmpNode;
                tmpNode.Input.OnClickConnectionPoint = OnNodeConnectorClicked;
                foreach (var output in tmpNode.Outputs)
                {
                    output.OwnerNode = tmpNode;
                    output.OnClickConnectionPoint = OnNodeConnectorClicked;
                }
                Nodes.Add(tmpNode);
            }

            foreach (var connection in save.Connections)
            {
                var inPoint = Nodes.First(n => n.Input.Id == connection.InputPoint.Id).Input;
                foreach (var node in Nodes)
                {
                    foreach (var output in node.Outputs)
                    {
                        if (output.Id != connection.OutputPoint.Id)
                        {
                            continue;
                        }
                        Connections.Add(new Connection(inPoint, output, OnClickRemoveConnection));
                        break;
                    }
                }
            }

            NodeCount = Nodes.Last().Id;
            _loading  = false;
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Saves the editors current map.
        /// </summary>
        private void Save()
        {
            var save = new DialogueSave()
            {
                Connections = Connections,
                Nodes       = Nodes
            };
            var path = EditorUtility.SaveFilePanel(
                "Save Dialogue",
                Application.dataPath + "/Resources/",
                "Diagloue.json",
                "json");

            if (path == "")
            {
                return;
            }
            List <Message> tmpMessages = new List <Message>();

            var           firstNode    = save.Nodes.SingleOrDefault(x => x.First);
            List <Option> firstOptions = new List <Option>();

            for (int i = 0; i < firstNode.Options.Count; i++)
            {
                firstOptions.Add(new Option()
                {
                    Content = firstNode.Options[i],
                    RedirectionMessageID = firstNode.Outputs[i].AttachedNode.Id
                });
            }
            tmpMessages.Add(new Message()
            {
                CharacterPath = firstNode.CharacterPath,
                Content       = firstNode.Message,
                Id            = firstNode.Id,
                Options       = firstOptions,
                Last          = firstNode.Last,
                First         = firstNode.First
            });

            var lastNodes = Nodes.Where(x => x.Last).ToList();

            foreach (var node in save.Nodes)
            {
                if (node == firstNode)
                {
                    continue;
                }
                if (lastNodes.Contains(node))
                {
                    continue;
                }

                List <Option> options = new List <Option>();
                for (int i = 0; i < node.Options.Count; i++)
                {
                    options.Add(new Option()
                    {
                        Content = node.Options[i],
                        RedirectionMessageID = node.Outputs[i].AttachedNode.Id
                    });
                }
                tmpMessages.Add(new Message()
                {
                    CharacterPath = node.CharacterPath,
                    Content       = node.Message,
                    Id            = node.Id,
                    Options       = options,
                    Last          = node.Last,
                    First         = node.First
                });
            }

            foreach (var node in lastNodes)
            {
                List <Option> lastOption = new List <Option>();
                for (int i = 0; i < node.Options.Count; i++)
                {
                    lastOption.Add(new Option()
                    {
                        Content = node.Options[i],
                        RedirectionMessageID = node.Outputs[i].AttachedNode.Id
                    });
                }
                tmpMessages.Add(new Message()
                {
                    CharacterPath = node.CharacterPath,
                    Content       = node.Message,
                    Id            = node.Id,
                    Options       = lastOption,
                    Last          = node.Last,
                    First         = node.First
                });
            }

            save.Messages = tmpMessages;
            var json = JsonConvert.SerializeObject(save);

            File.WriteAllText(path, json);
        }