Time for action - interacting with zombies

  1. In the 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);
    }
    }
    }
    
  2. In the Player class, add a declaration to hold the lives the player has remaining:
    private int livesRemaining = 3;
    
  3. Still in the Player class, add a property to access the livesRemaining value:
    public int LivesRemaining
    {
    get { return livesRemaining; }
    set { livesRemaining = value; }
    }
    
  4. Add the Kill() method to the Player class to allow the player to be killed:
    public void Kill()
    {
    PlayAnimation("die");
    LivesRemaining--;
    velocity.X = 0;
    dead = true;
    }
    
  5. Add the 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;
    }
    
  6. Execute the Gemstone Hunter application and kill some zombies! If you get killed by the zombies, you will need to end the application and restart it to continue playing.

What just happened?

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.225.255.134