Ejemplo n.º 1
0
    public void FindChunkedPath()
    {
        DebugDrawer.Clear();
        var pathGrid   = GridController.Path;
        var memoryGrid = createGraph();
        var graph      = new ChunkedPathfinding.Graph(memoryGrid, 128);

        System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
        sw.Start();
        graph.BuildAll();
        UnityEngine.Debug.Log("ChunkedGraph Building took " + sw.ElapsedMilliseconds + " ms");

        var sizeX = GridController.active.size.x;
        var sizeY = GridController.active.size.y;

        // Set grid weights
        var start = new Vector2Int(-1, -1);
        var end   = new Vector2Int(-1, -1);

        for (var x = 0; x < sizeX; x++)
        {
            for (var y = 0; y < sizeY; y++)
            {
                var pathTile = pathGrid.GetTile(new Vector3Int(x, y, 0));
                if (pathTile != null)
                {
                    if (pathTile == GridController.active.start)
                    {
                        start = new Vector2Int(x, y);
                    }
                    else if (pathTile == GridController.active.end)
                    {
                        end = new Vector2Int(x, y);
                    }
                }
            }
        }

        if (start.x == -1 || end.x == -1)
        {
            Debug.Log("Couldn't find any start or end position");
            return;
        }

        sw.Restart();
        List <ChunkedPathfinding.GraphPathfinder.GraphNode> path;
        var pathfinder = new ChunkedPathfinding.GraphPathfinder(graph);

        path = pathfinder.BidirectionalDijkstra(start.x, start.y, end.x, end.y);
        UnityEngine.Debug.Log("ChunkedGraph - Path" + (path == null ? " not " : " ") + "found in : " + sw.ElapsedMilliseconds + " ms");

        //  graph.DrawCellBorders();
        // graph.DrawOverlayGraph(1);

        graph.DrawGraph();
        // overlayGraph.DrawGraph();

        if (path != null)
        {
            for (var i = 0; i < path.Count; i++)
            {
                int x;
                int y;
                if (i == 0)
                {
                    x = start.x;
                    y = start.y;
                }
                else if (i == path.Count - 1)
                {
                    x = end.x;
                    y = end.y;
                }
                else
                {
                    var pathTile     = path[i];
                    var chunk        = graph.GetChunk(pathTile.ChunkID);
                    int gridPosition = chunk.vertices[pathTile.VertexID].GridPosition;
                    x = gridPosition / graph.sizeY;
                    y = gridPosition % graph.sizeY;
                    if (x == start.x && y == start.y)
                    {
                        continue;
                    }
                    if (x == end.x && y == end.y)
                    {
                        continue;
                    }
                }

                DebugDrawer.DrawCube(new Vector2Int(x, y), Vector2Int.one, Color.blue);
            }
        }
    }