示例#1
0
    public static DijkstraTile[,] generateDijkstraGrid(DijkstraTile[,] grid, Vector2Int gridSize, DijkstraTile target)
    {
        //flood fill out from the end point
        DijkstraTile destination = target;

        destination.setWeight(0);
        grid[destination.getVector2d().x, destination.getVector2d().y].setWeight(0);

        List <DijkstraTile> toVisit = new List <DijkstraTile>();

        toVisit.Add(destination);//check this maybe!!!


        //for each node we need to visit, starting with the pathEnd
        for (int i = 0; i < toVisit.Count; i++)
        {
            List <DijkstraTile> neighbours = straightNeighboursOf(toVisit[i], gridSize);

            //for each neighbour of this node (only straight line neighbours, not diagonals)
            foreach (DijkstraTile neighbour in neighbours)
            {
                //We will only ever visit every node once as we are always visiting nodes in the most efficient order
                if (grid[neighbour.getVector2d().x, neighbour.getVector2d().y].getWeight() == -1)  //if tile has not been visited
                {
                    neighbour.setWeight(toVisit[i].getWeight() + 1);
                    grid[neighbour.getVector2d().x, neighbour.getVector2d().y].setWeight(neighbour.getWeight());
                    toVisit.Add(neighbour);
                }
            }
        }
        return(grid);
    }
    void CreateGrid()
    {
        NodeArray = new DijkstraTile[iGridSizeX, iGridSizeY];                                                                                                   //Declare the array of nodes.
        Vector3 bottomLeft = transform.position - Vector3.right * vGridWorldSize.x / 2 - Vector3.forward * vGridWorldSize.y / 2;                                //Get the real world position of the bottom left of the grid.

        for (int x = 0; x < iGridSizeX; x++)                                                                                                                    //Loop through the array of nodes.
        {
            for (int y = 0; y < iGridSizeY; y++)                                                                                                                //Loop through the array of nodes
            {
                Vector3      worldPoint = bottomLeft + Vector3.right * (x * fNodeDiameter + fNodeRadius) + Vector3.forward * (y * fNodeDiameter + fNodeRadius); //Get the world co ordinates of the bottom left of the graph
                DijkstraTile tile       = new DijkstraTile(new Vector2Int(x, y), worldPoint);

                if (Physics.CheckSphere(worldPoint, fNodeRadius, WallMask))
                {
                    tile.setWeight(int.MaxValue);
                }
                NodeArray[x, y] = tile;//Create a new node in the array.
            }
        }
    }