O'Reilly logo

Procedural Content Generation for C++ Game Development by Dale Green

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required