Defining the spawn area

Now we know what obstacles lie ahead, and how the level data is stored, let's take a look at how we can spawn items at random locations in our roguelike object.

Calculating the level bounds

The first step is to calculate the level bounds. Since we're making a 2D roguelike object, described in a 2D array, we need to identify the tiles that are suitable to spawn items on. If this was done for a 3D game, you would also have to take into account the third axis. Though we could just find the top left point of the map and calculate the distance to the bottom right, this would almost certainly cause problems.

We mentioned earlier that it's important that items are spawned within valid level areas. If we take this simple approach, ...

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.