Game Development Reference
In-Depth Information
_upComingEnemies.Add(new EnemyDef("cannon_fodder", new Vector(300, 0,
0), 29));
// Sort enemies so the greater launch time appears first.
_upComingEnemies.Sort(delegate(EnemyDef firstEnemy, EnemyDef
secondEnemy)
{
return firstEnemy.LaunchTime.CompareTo(secondEnemy.LaunchTime);
});
}
The _upcomingEnemies list is a list of enemy definitions sorted by launch
time. The greater the launch time, the higher in the list the definition appears.
Each frame the top item of the list is checked to see if it's ready to launch. Only
the top enemy definition needs to be checked because the list is sorted. If the list
wasn't sorted, then every item in the list would need to be checked to decide
which of the enemy definitions had a launch time greater than the current
gameTime , and therefore needed to be launched next.
This enemy launching is done in the Update loop of the EnemyManager ,
which calls the new method UpdateEnemySpawns .
private void UpdateEnemySpawns(double gameTime)
{
// If no upcoming enemies then there's nothing to spawn.
if (_upComingEnemies.Count == 0)
{
return;
}
EnemyDef lastElement ¼ _upComingEnemies[_upComingEnemies.Count - 1];
if (gameTime < lastElement.LaunchTime)
{
_upComingEnemies.RemoveAt(_upComingEnemies.Count - 1);
_enemies.Add(CreateEnemyFromDef(lastElement));
}
}
private Enemy CreateEnemyFromDef(EnemyDef definition)
{
Enemy enemy ¼ new Enemy(_textureManager, _effectsManager);
enemy.SetPosition(definition.StartPosition);
 
Search WWH ::




Custom Search