Game Development Reference
In-Depth Information
{
foreach (Enemy enemy in _enemyList)
{
if (enemy.GetBoundingBox().IntersectsWith(_playerCharacter.
GetBoundingBox()))
{
enemy.OnCollision(_playerCharacter);
_playerCharacter.OnCollision(enemy);
}
}
}
public void Update(double elapsedTime)
{
UpdateCollisions();
The collision processing code is called each frame by the level's Update method.
The collisions are determined by iterating through the list of enemies and
checking if their bounding box intersects with the player. The intersection is
worked out using C#'s RectangleF IntersectsWith method. If the
bounding box of the player and enemy do intersect, then OnCollision is
called for the player and the enemy. The Player.OnCollision method
is passed the enemy object. It collides with it and the Enemy.OnCollision is
passed the player object. There is no test for enemies colliding with other
enemies; it's assumed this is no problem if it happens in the game.
The OnCollision class needs to be implemented for both the Enemy and
PlayerCharacter classes. Here is the skeleton method that needs to be added
to the Enemy class.
internal void OnCollision(PlayerCharacter player)
{
// Handle collision with player.
}
Unlike Enemy , the PlayerCharacter class actually has some functionality.
Its implementation is as follows.
internal void OnCollision(Enemy enemy)
{
_dead ΒΌ true;
}
 
Search WWH ::




Custom Search