Spawning items at a random location
Now, let's tie all of this together to spawn items randomly in the map. Here is a quick overview of the steps that we'll take:
Select a random
tile
from the level data.Check whether this tile is a
floor
tile. If not, go to step 1.Convert the tile location to the absolute position and give it to the item.
The first step is to select a random tile in the level data. Earlier in this chapter, we covered how we'll achieve this:
// Declare the variables we need. int columnIndex(0), rowIndex(0); Tile tileType; // Generate a random index for the row and column. columnIndex = std::rand() % GRID_WIDTH; rowIndex = std::rand() % GRID_HEIGHT; // Get the tile type. tileType = m_level.GetTileType(columnIndex, rowIndex);
We now need to check whether the randomly selected tile is suitable for the spawning of an item. We know that we can do this by checking the type of the tile, but we need to incorporate this into some kind of loop, so that if the randomly selected tile...