Game Development Reference
In-Depth Information
private void UpdateCollisions()
{
foreach (Enemy enemy in _enemyList)
{
if (enemy.GetBoundingBox().IntersectsWith(_playerCharacter.
GetBoundingBox()))
{
enemy.OnCollision(_playerCharacter);
_playerCharacter.OnCollision(enemy);
}
_bulletManager.UpdateEnemyCollisions(enemy);
}
}
One extra line has been added to the end of the for loop. The BulletManager
is asked to check any collisions between the bullets and the current enemy.
UpdateEnemyCollisions is a new function for the BulletManager ,soit
needs to be implemented.
internal void UpdateEnemyCollisions(Enemy enemy)
{
foreach (Bullet bullet in _bullets)
{
if(bullet.GetBoundingBox().IntersectsWith(enemy.GetBounding-
Box()))
{
bullet.Dead ΒΌ true;
enemy.OnCollision(bullet);
}
}
}
The collision between the bullet and enemy is determined by checking the
intersection between the bounding boxes. If the bullet has hit the enemy, then
the bullet is destroyed and the enemy is notified about the collision.
If a bullet hits an enemy, there are a number of different ways to react. The
enemy could be immediately destroyed and explode, or the enemy could take
damage, requiring a few more shots to be destroyed. Assigning health levels to
the enemy is probably something we'd like in the future, so we may as well do it
Search WWH ::




Custom Search