Exemplo n.º 1
0
    public List <Grid> GetShortestPath(Grid start, Grid goal)
    {
        if (!graph.Passable(goal) || !graph.Passable(start))
        {
            Debug.Log("不能形成路径");
            return(null);
        }
        AstarSearch(start, goal);
        Grid        current = goal;
        List <Grid> path    = new List <Grid>();

        while (current != start)
        {
            path.Add(current);
            if (current == null)
            {
                Debug.Log("current");
            }
            if (came_from == null)
            {
                Debug.Log("came_from");
            }
            current = came_from[current];
        }
        path.Add(start);
        path.Reverse();
        return(path);
    }
Exemplo n.º 2
0
    // Use this for initialization
    void Start()
    {
        //SimplePriorityQueue<string> simPriQueue = new SimplePriorityQueue<string>();
        //simPriQueue.Enqueue("111", 1);
        //simPriQueue.Enqueue("111", 1);
        //simPriQueue.Enqueue("111", 1);
        //while (simPriQueue.Count>0)
        //{
        //    Debug.Log(simPriQueue.Dequeue());
        //}
        Grid[,] Grids = new Grid[weight, length];
        squareGrid    = new SquareGrid(weight, length);
        int count = 0;

        for (int i = 0; i < length; i++)
        {
            for (int j = 0; j < weight; j++)
            {
                count++;
                GameObject go = GameObject.CreatePrimitive(PrimitiveType.Cube);
                go.name = count.ToString();
                go.transform.position    = new Vector3(i, 0, j);
                go.transform.localScale *= 0.7f;
                go.GetComponent <MeshRenderer>().material.color = Color.white;
                Grid grid = go.AddComponent <Grid>();
                grid.x = i; grid.y = j;
                squareGrid.AddEdge(grid);
                if (i == 0 && j == 0)
                {
                    start = grid;
                    grid.gameObject.GetComponent <MeshRenderer>().material.color = Color.black;
                }
                if (i == 15 && j == 15)
                {
                    end = grid;
                    grid.gameObject.GetComponent <MeshRenderer>().material.color = Color.black;
                }
                Grids[i, j]      = grid;
                grid.currentType = GridType.Ground;
            }
        }
        for (int i = 0; i < 100; i++)
        {
            int x = Random.Range(0, 19);
            int y = Random.Range(0, 19);
            if (x == 0 && y == 0)
            {
                continue;
            }
            if (x == 15 && y == 15)
            {
                continue;
            }
            Grid grid = Grids[x, y];
            grid.currentType = GridType.Wall;
            grid.gameObject.GetComponent <MeshRenderer>().material.color = Color.yellow;
            squareGrid.AddWall(grid);
        }
        for (int i = 0; i < 50; i++)
        {
            int x = Random.Range(0, 19);
            int y = Random.Range(0, 19);
            if (x == 0 && y == 0)
            {
                continue;
            }
            if (x == 15 && y == 15)
            {
                continue;
            }
            Grid grid = Grids[x, y];
            grid.currentType = GridType.Forest;
            if (squareGrid.Passable(grid))
            {
                grid.gameObject.GetComponent <MeshRenderer>().material.color = Color.green;
                squareGrid.AddForest(grid);
            }
        }
        astar = new Astar(squareGrid);
    }