コード例 #1
0
    void Start()
    {
        // A*
        greedy = new Greedy(width, height, depth, nodeSize, origin);

        // Create main camera
        Vector3 position = new Vector3(width * -6, height * 18, depth * -6);

        Instantiate(cam, position, cam.transform.rotation);

        grid = greedy.GetGrid();

        // Add walls manually
        greedy.GetNode(2, 2, 2).SetWalkable(!greedy.GetNode(2, 2, 2).isWalkable);

        // Draw walls
        DrawWalls();

        // Create visual nodes
        stepVisual.Setup(grid);

        // Show goal node
        Object.Instantiate(goalObject, grid.GetPosition(goalX, goalY, goalZ) + new Vector3(5, 5, 5), new Quaternion());
        Object.Instantiate(goalObject, grid.GetPosition(startX, startY, startZ) + new Vector3(5, 5, 5), new Quaternion());
    }
コード例 #2
0
    void SetupGreedy()
    {
        greedy = new Greedy(width, height, depth, nodeSize, origin);

        greedyGrid = greedy.GetGrid();

        // Add walls manually
        greedy.GetNode(2, 2, 0).SetWalkable(!greedy.GetNode(2, 2, 0).isWalkable);
        greedy.GetNode(2, 3, 0).SetWalkable(!greedy.GetNode(2, 3, 0).isWalkable);
        greedy.GetNode(2, 4, 0).SetWalkable(!greedy.GetNode(2, 4, 0).isWalkable);
        greedy.GetNode(3, 4, 0).SetWalkable(!greedy.GetNode(3, 4, 0).isWalkable);
        greedy.GetNode(4, 4, 0).SetWalkable(!greedy.GetNode(4, 4, 0).isWalkable);
        greedy.GetNode(5, 4, 0).SetWalkable(!greedy.GetNode(5, 4, 0).isWalkable);
        greedy.GetNode(6, 4, 0).SetWalkable(!greedy.GetNode(6, 4, 0).isWalkable);
        greedy.GetNode(6, 3, 0).SetWalkable(!greedy.GetNode(6, 3, 0).isWalkable);
        greedy.GetNode(6, 2, 0).SetWalkable(!greedy.GetNode(6, 2, 0).isWalkable);
        greedy.GetNode(1, 9, 0).SetWalkable(!greedy.GetNode(1, 9, 0).isWalkable);
        greedy.GetNode(1, 8, 0).SetWalkable(!greedy.GetNode(1, 8, 0).isWalkable);
        greedy.GetNode(3, 7, 0).SetWalkable(!greedy.GetNode(3, 7, 0).isWalkable);
        greedy.GetNode(4, 7, 0).SetWalkable(!greedy.GetNode(4, 7, 0).isWalkable);
        greedy.GetNode(5, 7, 0).SetWalkable(!greedy.GetNode(5, 7, 0).isWalkable);
        greedy.GetNode(6, 7, 0).SetWalkable(!greedy.GetNode(6, 7, 0).isWalkable);
        greedy.GetNode(7, 7, 0).SetWalkable(!greedy.GetNode(7, 7, 0).isWalkable);
        greedy.GetNode(8, 7, 0).SetWalkable(!greedy.GetNode(8, 7, 0).isWalkable);

        // Add weighted tiles manually
        greedy.GetNode(0, 4, 0).SetWeighted(!greedy.GetNode(0, 4, 0).isWeighted);
        greedy.GetNode(1, 4, 0).SetWeighted(!greedy.GetNode(1, 4, 0).isWeighted);

        // Draw walls
        for (int x = 0; x < greedyGrid.GetWidth(); x++)
        {
            for (int y = 0; y < greedyGrid.GetHeight(); y++)
            {
                for (int z = 0; z < greedyGrid.GetDepth(); z++)
                {
                    Node node = greedyGrid.GetGridObject(x, y, z);

                    // If the node is not walkable draw a wall
                    if (!node.isWalkable)
                    {
                        Object.Instantiate(wallObject, greedyGrid.GetPosition(x, y, z) + new Vector3(5, 5, 5), new Quaternion());
                    }

                    if (node.isWeighted)
                    {
                        Object.Instantiate(weightedObject, greedyGrid.GetPosition(x, y, z) + new Vector3(5, 5, 5), new Quaternion());
                    }
                }
            }
        }

        // Create visual nodes
        stepVisual.Setup(greedyGrid);

        // Show goal node
        Object.Instantiate(goalObject, greedyGrid.GetPosition(goalX, goalY, goalZ) + new Vector3(5, 5, 5), new Quaternion());
        Object.Instantiate(goalObject, greedyGrid.GetPosition(startX, startY, startZ) + new Vector3(5, 5, 5), new Quaternion());

        currentAlgorithm = "greedy";
    }