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 ...

Get Beginning C++ Game Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.