Game Development Reference
In-Depth Information
return b;
}
Here we check if there are any bullets in the stand-by pool and if there aren't any we
create a new one, set the damage value, and load the sprite for it. Once that's done,
we can call respawn to ensure it is "alive" and then add it to the active bullets list.
We'll use this in our Game::Update method to create new bullets based on a firing
timer we implement inside the Player class.
But first let's add the code to update the bullets and manage the cache. Inside up-
date, near the top, add the following code:
std::vector<Bullet*> garbage;
for (auto bullet : _bullets)
{
if (bullet->GetIsAlive())
{
bullet->MoveForward(deltaTime);
}
else
{
_bulletPool.push(bullet);
garbage.push_back(bullet);
}
}
for (auto toRemove : garbage)
_bullets.remove(toRemove);
Here we go through all of the active bullets and check if they are alive. If they are,
we just move them forward using Bullet::MoveForward . If not, we need to add
them to the available pool and also flag them for deletion from the active list using a
temporary vector that indicates the garbage to clean up.
Search WWH ::




Custom Search