Map Generation 1.0

Today I also have been working in the code of map generation, now I only have the procedure to create the tiles no matter if they are correlated to the nearest ones of not, the next step will be to check which tiles can be placed in the correct place and which not.

To achieve that generation I have 3 important variables to take into account which are number of tiles, number of rows and number of columns to create the array which I store in a script with other global variables:

public static int numTiles = 16;
public static int numRows = 4;
public static int numColumns = 4;

In that case  the total number of tiles are 16, so a square shape city of 4 x 4 tiles. I use this variables as global to easily access the value from other scripts and not needed to instantiate the global variables script. In the video shown the city size is 10 x 10 so 100 tiles.

So to create the array I just take into account these two factors and create a 2D array with random numbers from 1 to 8 (the number of tiles I have stored in prefabs). And then I use another function to instantiate the tiles regarding the value in each position of the array. The function that creates all these things looks like this:

public void generateTiles(){
         destroyTiles();
         createTileArray();
         createTiles ();

}

The function named destroyTiles() what it does is to destroy any previous tile that is in the scene, that is to prevent any old tile to appear if a new call of the generateTiles() is called. The destroyTiles() function just search for all the objects in the scene with the tag “Tile” and destroy them using a loop through all of them as it can be seen in this code:

void destroyTiles(){

         //search for any object with the tag “Tile” and destroy it
          if (tiles == null){

                 tiles = GameObject.FindGameObjectsWithTag(“Tile”);

                 foreach (GameObject tile in tiles) {
                                 Destroy(tile);
                 }
           }
            tiles = null;
}

The function FindGameObjectsWithTag(“tag”) it search for any object with the written tag inside and returns and array of objects or null in case it does not find anything. The variable tiles is an array of GameObjects:

private GameObject tiles;

 

Leave a comment