Game Development Reference
In-Depth Information
Note
This is similar to the Mark and Sweep garbage collection style. First the items
to be destroyed are marked for deletion during an initial pass, and once that is
complete they are deleted in one go.
Once this update is complete we simply go through all of the items in the temporary
garbage vector and remove them from the active list.
Near the bottom of the same Game::Update method, add the following code:
if (_player->CanFire())
{
auto b = CreateBullet();
b->SetPosition(_player->GetPosition());
}
Here we use a helper method that we're about to define called CanFire to check if
the player can fire a new shot. If they can, we use the CreateBullet helper from
before to generate (or retrieve) a new bullet, which we can then reposition, ready to
move during the next update.
Player::CanFire manages the reload time to allow us to have a delay between
individual shots while we use this continuous firing system—after all, we don't want
30 or 60 shots every single second!
bool Player::CanFire()
{
auto result = _sinceLastShot > ReloadTime;
if (result)
_sinceLastShot = 0;
return result;
}
Search WWH ::




Custom Search