Esempio n. 1
0
    public void PopulateNodes()
    {
        _nodesWithLinksFrom = new Dictionary <string, HashSet <string> >();
        _nodesWithLinksTo   = new Dictionary <string, HashSet <string> >();
        if (_lines != null && _lines.Any())
        {
            Debug.Log("Deleting lines");
            foreach (var line in _lines.Values)
            {
                line.Destroy();
                Destroy(line.gameObject);
            }
        }

        if (_nodeButtons != null && _nodeButtons.Any())
        {
            Debug.Log("Deleting buttons");
            foreach (var button in _nodeButtons.Values)
            {
                Destroy(button.gameObject);
            }
        }

        if (_floatingNodeButtons != null && _floatingNodeButtons.Any())
        {
            Debug.Log("Deleting floating buttons");
            foreach (var floatingButton in _floatingNodeButtons.Values)
            {
                Destroy(floatingButton.gameObject);
            }
        }
        _nodeButtons = new Dictionary <string, Button>();
        _lines       = new Dictionary <string, LineHolder>();

        var files     = NodeUtil.GetAvailableNodes();
        var nodes     = new List <Tuple <string, StoryNode> >();
        var nodeNames = new HashSet <string>();

        foreach (var file in files)
        {
            var nName = NodeUtil.StripFileExtension(file);
            nodes.Add(new Tuple <string, StoryNode>(nName, NodeUtil.GetNodeData <StoryNode>(file)));
            if (!nodeNames.Contains(nName))
            {
                nodeNames.Add(nName);
            }
        }

        // Gets the dictionary of nodes with the nodes that link to it
        foreach (var(key, node) in nodes)
        {
            foreach (var nodeLink in node.nodes)
            {
                var link = NodeUtil.StripFileExtension(nodeLink.link);
                if (!nodeNames.Contains(link))
                {
                    continue;
                }

                if (!_nodesWithLinksFrom.ContainsKey(link))
                {
                    _nodesWithLinksFrom.Add(link, new HashSet <string>());
                }

                if (!_nodesWithLinksFrom[link].Contains(key))
                {
                    _nodesWithLinksFrom[link].Add(key);
                }
            }
        }

        // Gets the dictionary of nodes with the nodes that it links to
        foreach (var(key, node) in nodes)
        {
            if (!_nodesWithLinksTo.ContainsKey(key))
            {
                _nodesWithLinksTo.Add(key, new HashSet <string>());
            }

            var linksToDelete = new List <string>();
            foreach (var nodeLink in node.nodes)
            {
                var link = NodeUtil.StripFileExtension(nodeLink.link);
                if (!_nodesWithLinksFrom.ContainsKey(link))
                {
                    linksToDelete.Add(link);
                    continue;
                }

                if (!_nodesWithLinksTo[key].Contains(link))
                {
                    _nodesWithLinksTo[key].Add(link);
                }
            }

            // if there are any non-existing links, then update the node data
            if (linksToDelete.Any())
            {
                var correctedNode = new Node
                {
                    text  = node.text,
                    nodes = node.nodes.Where(n => !linksToDelete.Contains(NodeUtil.StripFileExtension(n.link))).ToArray()
                };

                NodeUtil.WriteNodeData(key, correctedNode);
            }
        }

        _connectedNodes      = new HashSet <string>();
        _floatingNodeButtons = new Dictionary <string, Button>();

        // if there is no entry node then make one
        if (!_nodesWithLinksTo.ContainsKey("entry"))
        {
            _nodesWithLinksTo.Add("entry", new HashSet <string>());
            NodeUtil.WriteNodeData("entry", new Node());
        }

        // This part creates all of the buttons for the tree starting from 'entry'
        AddNode("entry");

        // Construct the floating nodes list
        BuildFloatingNodesList(nodes);

        // This parts sets up the lines between nodes and the indicators for them
        BuildGraphButtonLinks();
    }