Game Development Reference
In-Depth Information
Damage , GetIsAlive , SetIsAlive , and Reset all manage the health status of
the ship and allow for other objects to damage the ship and eventually destroy it.
So we have the enemies and player well defined, but this new Enemy class isn't ac-
tually in use yet. Return to the Game class and add the following private variables:
std::vector<Enemy*> _enemies;
float _sinceLastSpawn;
Once those are in, we need to add the following to LoadContent :
for (auto i = 0; i < MaxEnemies; ++i)
{
auto e = new Enemy();
e->Load();
e->SetIsAlive(false);
_enemies.push_back(e);
}
This lets us create a pool of enemies that we can spawn in the game-based on a
spawn timer. A pool is a pre-defined collection that allows us to re-use objects. This
has the benefit of removing constant allocations and de-allocations, which is often
slow. We can adjust some of the values, such as the total number of enemies based
on our needs, but for now let's just fix that at 5, which we can do with a simple con-
stant in Game.cpp .
The spawning itself happens inside the Update method within Game , shown as fol-
lows:
_sinceLastSpawn += deltaTime;
int numAlive = 0;
for (auto enemy : _enemies)
{
if (enemy->GetIsAlive())
{
enemy->Update(deltaTime);
Search WWH ::




Custom Search