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:

  1. Select a random tile from the level data.
  2. Check whether this tile is a floor tile. If not, go to step 1.
  3. 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 ...

Get Procedural Content Generation for C++ Game Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.