Game Development Reference
In-Depth Information
The blocks we are currently using in the terrain will be stored inside a
_blocks
vector.
We determine that the minimum width the
_terrain
object must have is
1.5
times the screen width. We'll keep adding blocks until the
_terrain
object
reaches this minimum width. We end by shuffling the
patterns
arrays and
adding the blocks.
2. The
addBlocks
method should look like this:
void Terrain::addBlocks(int currentWidth) {
while (currentWidth < _minTerrainWidth)
{
auto block = _blockPool.at(_blockPoolIndex);
_blockPoolIndex++;
if (_blockPoolIndex == _blockPool.size()) {
_blockPoolIndex = 0;
}
this->initBlock(block);
currentWidth += block->getWidth();
_blocks.pushBack(block);
}
this->distributeBlocks();
}
The logic inside the
while
loop will continue to add blocks until
cur-
rentWidth
of the
_terrain
object reaches
_minTerrainWidth
. Every
new block we retrieve from the pool in order to reach
_minTerrainWidth
gets added to the
_blocks
vector.
3. Blocks are distributed based on their widths:
void Terrain::distributeBlocks() {
int count = (int) _blocks.size();
int i;
for (i = 0; i < count; i++) {
auto block = _blocks.at(i);
if (i != 0) {
auto prev_block = _blocks.at(i - 1);