Example #1
0
    void SetupLevel()
    {
        bool laddarSpawned = false; // bool will control if a ladder has already been spawned
        bool playerSpawned = false;

        cellularAutomata = GameObject.Find("MeshGenerator").GetComponent <CellularAutomata>();                                      // finds the cellular automata script in the scene
        aStarGrid        = GameObject.Find("A*").GetComponent <AStarGrid>();                                                        // finds the AStarGrid

        for (int x = 0; x < cellularAutomata.gridWidth; x++)                                                                        // loops through the grids x
        {
            for (int y = 0; y < cellularAutomata.gridHeight; y++)                                                                   // loops through the grids y
            {
                if (x != 0 && x != cellularAutomata.grid.GetLength(0) - 2 && y != 0 && y != cellularAutomata.grid.GetLength(1) - 2) // if we are within the bounds the array
                {
                    if (cellularAutomata.GetAmountOfNeighbours(x, y, 2) == 0 && aStarGrid.grid[x, y].walkable)                      // using the overloaded methoed of get neighbours to have a larger search area to stop things from spawning on walls and checks if the tile is walkable in the AStarGrid
                    {                                                                                                               //
                        if (GetRandomNumber() <= probabilityToSpawnEnemy)                                                           // generate a random number and if it is within probabilty
                        {
                            Instantiate(enemy, aStarGrid.grid[x, y].worldPosition, Quaternion.identity);                            // spawn an enemy
                        }
                        else if (!laddarSpawned)
                        {
                            laddarSpawned = Instantiate(ladder, aStarGrid.grid[x, y].worldPosition, Quaternion.identity); // if no spawn a ladder and set the ladderspawned varaible to true
                        }
                        else if (!playerSpawned && (x == (cellularAutomata.grid.GetLength(0) / 2) || y == (cellularAutomata.grid.GetLength(1) / 2)))
                        {
                            playerSpawned = Instantiate(player, aStarGrid.grid[x, y].worldPosition, Quaternion.identity);
                        }
                    }
                }
            }
        }
    }