Spawning a random item

Currently, when we load our game, set items are spawned. We need to add an element of randomness to do this, and something as simple as a switch statement is all that's needed. Where we can, we always want to add options to create random and procedurally generated content.

To randomly spawn our items, we need to generate a random number between 0 and the number of items we have, and then use that in a switch statement. As stated previously, there isn't one approach to procedural generation, so there will be other methods to doing this.

Let's add in the number generation and switch statements to choose which item to spawn. The updated Game::PopulateLevel function should look as follows:

// Populate the level with items. void ...

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.