Game Development Reference
In-Depth Information
Creating the grid
Let's take some time now to review the grid logic in the game. This grid is created inside
the
createStarGrid
method in
GameLayer.cpp
. What the method does is determ-
ine all possible spots on the screen where we can place the
_star
particle system.
We use a C++ vector list called
_grid
to store the available spots:
std::vector<Point> _grid;
The
createStarGrid
method divides the screen into multiple cells of 32 x 32 pixels,
ignoring the areas too close to the screen borders (
gridFrame
). Then we check the dis-
tance between each cell and the planet sprites stored inside the vector
_planets
. If the
cell is far enough from the planets, we store it inside the
_grid
vector as
Point
.
In the following figure, you can get an idea of the result we're after. We want all the white
cells not overlapping any of the planets.
We output a message to the console with
Log
stating how many cells we end up with:
CCLOG("POSSIBLE STARS: %i", _grid.size());
This
vector
list will be shuffled at each new game, so we end up with a random sequence
of possible positions for our star:
std::random_shuffle(_grid.begin(), _grid.end());