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, we run the risk of spawning items in the walls. The following pseudocode shows how this can be achieved:
for (int i = 0; i < GRID_WIDTH; ++i) { for (int j = 0; j < GRID_HEIGHT; ++j) { m_grid[i][j].markAsSpawnable...