Game Development Reference
In-Depth Information
numAlive++;
}
}
if (_sinceLastSpawn >= SpawnTime)
{
_sinceLastSpawn -= SpawnTime;
if (numAlive < MaxEnemies)
{
for (auto enemy : _enemies)
{
if (!enemy->GetIsAlive())
{
auto texSize = enemy->GetTextureSize();
auto y = texSize.y +
((float)rand() / (float)RAND_MAX) *
(m_renderTargetSize.Height -
(texSize.y * 2));
enemy->MoveTo(m_renderTargetSize.Width,
y);
enemy->Reset();
break;
}
}
}
}
Here we begin with just a standard update loop for the enemy. However, we don't
bother to update the enemy if it isn't alive. As this game won't have thousands of
enemies we don't need to worry about writing a different system to avoid extra itera-
tions. Now we move onto the spawning part of the code, where the real work is done.
In here we only want to spawn if there has been enough time since the last spawn.
We track this using _sinceLastSpawn , which is updated with the deltaTime
value each update.
Once it exceeds the limit, which is just a constant defined with a value of 5, we can
jump the value back to start counting again and then move on with spawning. In the
Search WWH ::




Custom Search