Playing the rest of the sounds

Now we will add the rest of the calls to the play function. We deal with each of them individually as locating exactly where they go is key to playing them at the right moment.

Adding sound effects while the player is reloading

Add the highlighted code in three places to play the appropriate reload or reloadFailed sound when the player presses the R key to attempt to reload their gun:

if (state == State::PLAYING) 
{ 
   // Reloading 
   if (event.key.code == Keyboard::R) 
   { 
      if (bulletsSpare >= clipSize) 
      { 
         // Plenty of bullets. Reload. 
         bulletsInClip = clipSize; 
         bulletsSpare -= clipSize;      
         reload.play(); 
      } 
      else if (bulletsSpare > 0) 
      { 
         // Only few bullets left 
         bulletsInClip = bulletsSpare; 
         bulletsSpare = 0;           
         reload.play(); 
      } 
      else 
      { 
         // More here soon?! 
         reloadFailed.play(); 
      } 
   } 
} 

Make a shooting sound

Add the highlighted call to shoot.play() near the end of the code that handles the player clicking the left mouse button:

// Fire a bullet 
if (sf::Mouse::isButtonPressed(sf::Mouse::Left)) 
{ 
 
   if (gameTimeTotal.asMilliseconds() 
      - lastPressed.asMilliseconds() 
      > 1000 / fireRate && bulletsInClip > 0) 
   { 
 
      // Pass the centre of the player and crosshair 
      // to the shoot function 
      bullets[currentBullet].shoot( 
         player.getCenter().x, player.getCenter().y, 
         mouseWorldPosition.x, mouseWorldPosition.y); 
 
      currentBullet++; 
      if (currentBullet > 99) 
      { 
         currentBullet = 0; 
      } 
      lastPressed = gameTimeTotal; 
 
      shoot.play(); 
 
      bulletsInClip--; 
   } 
 
}// End fire a bullet 

Play a sound when the player is hit

In this next code, we wrap the call to hit.play in a test to see whether the player.hit function returns true. Remember that the player.hit function tests to see whether a hit has been recorded in the previous 100 milliseconds. This will have the effect of playing a fast, repeating, thudding sound, but not so fast that the sound blurs into one noise.

Add the call to hit.play, as highlighted here:

// Have any zombies touched the player        
for (int i = 0; i < numZombies; i++) 
{ 
   if (player.getPosition().intersects 
      (zombies[i].getPosition()) && zombies[i].isAlive()) 
   { 
 
      if (player.hit(gameTimeTotal)) 
      { 
         // More here later 
         hit.play(); 
      } 
 
      if (player.getHealth() <= 0) 
      { 
         state = State::GAME_OVER; 
 
         std::ofstream OutputFile("gamedata/scores.txt"); 
         OutputFile << hiScore; 
         OutputFile.close(); 
          
      } 
   } 
}// End player touched 

Play a sound when getting a pickup

When the player picks up a health pickup, we will play the regular pickup sound, but when the player gets an ammo pickup we play the reload sound effect.

Add the two calls to play sounds as highlighted here, within the appropriate collision detection code:

// Has the player touched health pickup 
if (player.getPosition().intersects 
   (healthPickup.getPosition()) && healthPickup.isSpawned()) 
{ 
   player.increaseHealthLevel(healthPickup.gotIt()); 
   // Play a sound
   pickup.play(); 
    
} 
 
// Has the player touched ammo pickup 
if (player.getPosition().intersects 
   (ammoPickup.getPosition()) && ammoPickup.isSpawned()) 
{ 
   bulletsSpare += ammoPickup.gotIt(); 
   // Play a sound
   reload.play(); 
    
} 

Make a splat sound when a zombie is shot

Add a call to splat.play at the end of the section of code that detects a bullet colliding with a zombie:

// Have any zombies been shot? 
for (int i = 0; i < 100; i++) 
{ 
   for (int j = 0; j < numZombies; j++) 
   { 
      if (bullets[i].isInFlight() &&  
         zombies[j].isAlive()) 
      { 
         if (bullets[i].getPosition().intersects 
            (zombies[j].getPosition())) 
         { 
            // Stop the bullet 
            bullets[i].stop(); 
 
            // Register the hit and see if it was a kill 
            if (zombies[j].hit()) { 
               // Not just a hit but a kill too 
               score += 10; 
               if (score >= hiScore) 
               { 
                  hiScore = score; 
               } 
 
               numZombiesAlive--; 
 
               // When all the zombies are dead (again) 
               if (numZombiesAlive == 0) { 
                  state = State::LEVELING_UP; 
               } 
            }   
 
           // Make a splat sound
           splat.play(); 
             
         } 
      } 
 
   } 
}// End zombie being shot 

You can now play the completed game and watch the number of zombies and the arena increase with each wave. Choose your level-ups carefully:

Make a splat sound when a zombie is shot

Congratulations!

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

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