Update()
method of the LevelManager class, replace the foreach
loop that updates the enemies list entries with the following loop:for (int x = enemies.Count - 1; x >= 0; x--) { enemies[x].Update(gameTime); if (!enemies[x].Dead) { if (player.CollisionRectangle.Intersects( enemies[x].CollisionRectangle)) { if (player.WorldCenter.Y < enemies[x].WorldLocation.Y) { player.Jump(); player.Score += 5; enemies[x].PlayAnimation("die"); enemies[x].Dead = true; ; } else { player.Kill(); } } } else { if (!enemies[x].Enabled) { enemies.RemoveAt(x); } } }
private int livesRemaining = 3;
livesRemaining
value:public int LivesRemaining { get { return livesRemaining; } set { livesRemaining = value; } }
Kill()
method to the Player class to allow the player to be killed:public void Kill() { PlayAnimation("die"); LivesRemaining--; velocity.X = 0; dead = true; }
Revive()
method to the Player class, which we will use to respawn the player after they have died. We will implement this functionality when we build the game state structure around Gemstone Hunter:public void Revive() { PlayAnimation("idle"); dead = false; }
When we determine that the player has collided with an enemy, we check the Y
coordinate of the player's WorldCenter
vector against the Y
coordinate of the enemy's WorldLocation
vector. WorldCenter
points to the middle of the object's display area, while WorldLocation
points to the upper left corner. If the player's center is above the top of the enemy, we grant the player a kill of the zombie, and call player.Jump()
to boost them back up into the air. The enemy's "die" animation is played, and the enemy is marked as dead.
Otherwise, the player's Kill()
method is called, resulting in the player's "die" animation being played, and any current horizontal movement negated. Because the Player class's Update()
method checks to make sure the player is not dead before accepting any input, the player will now be unable to move and lie lifeless on the floor.
18.225.255.134