Game Development Reference
In-Depth Information
now. Let's add a
Health
member variable to the enemy and then we can
implement the
OnCollision
function for bullets.
public int Health { get; set; }
public Enemy(TextureManager textureManager)
{
Health
¼
50; // default health value.
//Remaining constructor code omitted
The
Enemy
class already has one
OnCollision
method, but that is for collid-
ing with the
PlayerCharacter
. We will create a new overloaded
OnColli-
sion
method that is only concerned about colliding with bullets. When a bullet
hits the enemy, it will take some damage and lower its health value. If the health
of the enemy drops below 0, then it will be destroyed. If the player shoots
the enemy and causes some damage, there needs to be some visual feedback to
indicate the enemy has taken a hit. A good way to present this feedback is to flash
the enemy yellow for a fraction of a second.
static readonly double HitFlashTime
¼
0.25;
double _hitFlashCountDown
¼
0;
internal void OnCollision(Bullet bullet)
{
// If the ship is already dead then ignore any more bullets.
if (Health == 0)
{
return;
}
Health
¼
Math.Max(0, Health - 25);
_hitFlashCountDown
¼
HitFlashTime; // half
_sprite.SetColor(new Engine.Color(1, 1, 0, 1));
if (Health == 0)
{
OnDestroyed();
}
}
private void OnDestroyed()
{
// Kill the enemy here.
}
Search WWH ::
Custom Search