示例#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);
    }
示例#2
0
    // Update is called once per frame
    void FixedUpdate()
    {
        DijkstraTile currentTile = worldGrid.NodeFromWorldPoint(agentPosition.position);

        if (this.lastValidTile == null)
        {
            this.lastValidTile = currentTile;
        }
        if (currentTile.getFlowFieldVector().Equals(Vector2Int.zero))
        {
            Vector2Int flowVector = this.lastValidTile.getVector2d() - currentTile.getVector2d();
            Vector3    moveDir    = new Vector3(flowVector.x, 0, flowVector.y).normalized;
            rb.AddForce(moveDir * Time.deltaTime * force, ForceMode.Force);
            //transform.position += moveDir * Time.deltaTime;
        }
        else
        {
            this.lastValidTile = currentTile;
            Vector2Int flowVector = currentTile.getFlowFieldVector();
            Vector3    moveDir    = new Vector3(flowVector.x, 0, flowVector.y).normalized;
            rb.AddForce(moveDir * Time.deltaTime * force, ForceMode.Force);
        }
    }
示例#3
0
    private static List <DijkstraTile> straightNeighboursOf(DijkstraTile tile, Vector2Int gridSize)
    {
        List <DijkstraTile> neighbours = new List <DijkstraTile>();

        if (tile.getVector2d().x > 0)
        {
            neighbours.Add(new DijkstraTile(new Vector2Int(tile.getVector2d().x - 1, tile.getVector2d().y), Vector3.zero));
        }
        if (tile.getVector2d().y > 0)
        {
            neighbours.Add(new DijkstraTile(new Vector2Int(tile.getVector2d().x, tile.getVector2d().y - 1), Vector3.zero));
        }
        if (tile.getVector2d().x < gridSize.x - 1)
        {
            neighbours.Add(new DijkstraTile(new Vector2Int(tile.getVector2d().x + 1, tile.getVector2d().y), Vector3.zero));
        }
        if (tile.getVector2d().y < gridSize.y - 1)
        {
            neighbours.Add(new DijkstraTile(new Vector2Int(tile.getVector2d().x, tile.getVector2d().y + 1), Vector3.zero));
        }
        return(neighbours);
    }