Leveling up

The next code we will add enables the player to level-up between waves. Because of the work we have already done that makes, this is straightforward to achieve.

Add the highlighted code in the LEVELING_UP state where we handle player input:

// Handle the LEVELING up state 
if (state == State::LEVELING_UP) 
{ 
   // Handle the player LEVELING up 
   if (event.key.code == Keyboard::Num1) 
   { 
     // Increase fire rate
     fireRate++; 
     state = State::PLAYING; 
   } 
 
   if (event.key.code == Keyboard::Num2) 
   { 
     // Increase clip size
     clipSize += clipSize; 
     state = State::PLAYING; 
   } 
 
   if (event.key.code == Keyboard::Num3) 
   { 
     // Increase health
     player.upgradeHealth(); 
     state = State::PLAYING; 
   } 
 
   if (event.key.code == Keyboard::Num4) 
   { 
     // Increase speed
     player.upgradeSpeed(); 
     state = State::PLAYING; 
   } 
 
   if (event.key.code == Keyboard::Num5) 
   { 
     // Upgrade pickup
     healthPickup.upgrade(); 
     state = State::PLAYING; 
   } 
 
   if (event.key.code == Keyboard::Num6) 
   { 
     // Upgrade pickup
     ammoPickup.upgrade(); 
     state = State::PLAYING; 
   } 
 
   if (state == State::PLAYING) 
   { 

The player can now level-up each time a wave of zombies is cleared. We can't, however, increase the number of zombies or the size of the level yet.

In the next part of the LEVELING_UP state, right after the code we have just added, amend the code that runs when the state changes from LEVELING_UP to PLAYING.

Here is the code in full. I have highlighted the lines that are either new or have been slightly amended.

Add or amend the highlighted code:

   if (event.key.code == Keyboard::Num6) 
   { 
      ammoPickup.upgrade(); 
      state = State::PLAYING; 
   } 
 
   if (state == State::PLAYING) 
   { 
     // Increase the wave number
     wave++; 
 
     // Prepare thelevel 
     // We will modify the next two lines later 
     arena.width = 500 * wave;
     arena.height = 500 * wave; 
     arena.left = 0; 
     arena.top = 0; 
 
     // Pass the vertex array by reference  
     // to the createBackground function 
     int tileSize = createBackground(background, arena); 
 
     // Spawn the player in the middle of the arena 
     player.spawn(arena, resolution, tileSize); 
 
     // Configure the pickups 
     healthPickup.setArena(arena); 
     ammoPickup.setArena(arena); 
 
     // Create a horde of zombies 
     numZombies = 5 * wave; 
 
     // Delete the previously allocated memory (if it exists) 
     delete[] zombies; 
     zombies = createHorde(numZombies, arena); 
     numZombiesAlive = numZombies; 
 
     // Play the powerup sound
     powerup.play(); 
 
     // Reset the clock so there isn't a frame jump 
     clock.restart(); 
   } 
}// End LEVELING up 

The previous code starts by incrementing the wave variable. Then the code is amended to make the number of zombies and size of the arena relative to the new value of wave. Finally, we add the call to powerup.play() to play the leveling up sound effect.

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

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