Exemple #1
0
        void Start()
        {
            // Make "diagram 4" from main article
            var grid = new SquareGrid(10, 10);

            for (var x = 1; x < 4; x++)
            {
                for (var y = 7; y < 9; y++)
                {
                    grid.walls.Add(new Location(x, y));
                }
            }
            grid.forests = new HashSet <Location>
            {
                new Location(3, 4), new Location(3, 5),
                new Location(4, 1), new Location(4, 2),
                new Location(4, 3), new Location(4, 4),
                new Location(4, 5), new Location(4, 6),
                new Location(4, 7), new Location(4, 8),
                new Location(5, 1), new Location(5, 2),
                new Location(5, 3), new Location(5, 4),
                new Location(5, 5), new Location(5, 6),
                new Location(5, 7), new Location(5, 8),
                new Location(6, 2), new Location(6, 3),
                new Location(6, 4), new Location(6, 5),
                new Location(6, 6), new Location(6, 7),
                new Location(7, 3), new Location(7, 4),
                new Location(7, 5)
            };

            // Run A*
            var astar = new AStarSearch(grid, new Location(1, 4),
                                        new Location(1, 7));

            DrawGrid(grid, astar);
        }
Exemple #2
0
        void DrawGrid(SquareGrid grid, AStarSearch astar)
        {
            // Print out the cameFrom array
            StringBuilder sb = new StringBuilder();

            for (var y = 0; y < 10; y++)
            {
                for (var x = 0; x < 10; x++)
                {
                    Location id  = new Location(x, y);
                    Location ptr = id;
                    if (!astar.cameFrom.TryGetValue(id, out ptr))
                    {
                        ptr = id;
                    }

                    GameObject obj = Instantiate(cubePrefab);
                    obj.transform.position = new Vector3(x, 0, y);
                    obj.name = "cube" + x + "x" + y;

                    var meshRender = obj.GetComponent <MeshRenderer>();

                    if (grid.walls.Contains(id))
                    {
                        //  sb.Append("##");
                        meshRender.material.color = Color.gray;
                    }
                    if (grid.forests.Contains(id))
                    {
                        meshRender.material.color = Color.cyan;
                    }

                    // else if (ptr.x == x+1) {
                    //  sb.Append("\u2192 ");
                    //  meshRender.material.color = Color.green;
                    // }
                    // else if (ptr.x == x-1) {
                    //  sb.Append("\u2190 ");
                    //  meshRender.material.color = Color.green;
                    // }
                    // else if (ptr.y == y+1) {
                    //  sb.Append("\u2193 ");
                    //  meshRender.material.color = Color.green;
                    // }
                    // else if (ptr.y == y-1) {
                    //  sb.Append("\u2191 ");
                    //  meshRender.material.color = Color.green;
                    // }
                    // else {
                    //  sb.Append("* ");
                    //  meshRender.material.color = Color.red;
                    // }

                    // if (astar.cameFrom.TryGetValue(id, out ptr))
                    // {
                    //     ptr = id;
                    //      meshRender.material.color = Color.green;
                    // }

                    if (x == 1 && y == 4)
                    {
                        meshRender.material.color = Color.yellow;
                    }
                    if (x == 8 && y == 5)
                    {
                        meshRender.material.color = Color.blue;
                    }
                }
                // sb.Append("\n");
            }

            // Debug.Log("-----------------------------");
            // Debug.Log(sb.ToString());
        }