示例#1
0
        private void AddEdgeIfNeeded(GraphGridNode node, int x, int y)
        {
            if (!node.Passable)
            {
                return;
            }

            if (x < 0 || x >= Width || y < 0 || y >= Height)
            {
                return;
            }

            var otherNode = Map[x, y];

            if (otherNode != null && otherNode.Passable)
            {
                Graph.AddEdge(new GraphEdge(node.Index, otherNode.Index));
            }
        }
示例#2
0
        private void LoadGraph(IReadOnlyCollection <string> lines)
        {
            Width  = CalculateLongestLine(lines);
            Height = lines.Count;

            Graph = new SparseGraph <GraphGridNode, GraphEdge>(false);
            Map   = new GraphGridNode[Width, Height];

            var y = 0;

            foreach (var line in lines)
            {
                var x = 0;
                foreach (var tileChar in line)
                {
                    var node = _nodeFactory.CreateNode(x, y, tileChar.ToString());
                    Graph.AddNode(node);
                    Map[x, y] = node;

                    if (tileChar == '@')
                    {
                        Source = node;
                    }
                    else if (tileChar == 'X')
                    {
                        Destination = node;
                    }

                    x++;
                }

                y++;
            }

            CreateEdges();
        }