Playing sounds

Open up the LoadLevel.cpp file and add the call to the new populateEmitters function, as highlighted in the following code:

void Engine::loadLevel() 
{ 
   m_Playing = false; 
 
   // Delete the previously allocated memory 
   for (int i = 0; i < m_LM.getLevelSize().y; ++i) 
   { 
      delete[] m_ArrayLevel[i]; 
 
   } 
   delete[] m_ArrayLevel; 
 
   // Load the next 2d array with the map for the level 
   // And repopulate the vertex array as well 
   m_ArrayLevel = m_LM.nextLevel(m_VALevel); 
 
   // Prepare the sound emitters
   populateEmitters(m_FireEmitters, m_ArrayLevel); 
 
   // How long is this new time limit 
   m_TimeRemaining = m_LM.getTimeLimit(); 
 
   // Spawn Thomas and Bob 
   m_Thomas.spawn(m_LM.getStartPosition(), GRAVITY); 
   m_Bob.spawn(m_LM.getStartPosition(), GRAVITY); 
 
   // Make sure this code isn't run again 
   m_NewLevelRequired = false; 
} 

The first sound to add is the jump sound. You might remember that the keyboard handling code is in the pure virtual functions within both the Bob and Thomas classes, and that the handleInput function returns true when a jump has been successfully initiated.

Open up the Input.cpp file and add the highlighted lines of code to play a jump sound when either Thomas or Bob successfully begins a jump:

// Handle input specific to Thomas 
if (m_Thomas.handleInput()) 
{ 
   // Play a jump sound 
   m_SM.playJump(); 
} 
 
// Handle input specific to Bob 
if (m_Bob.handleInput()) 
{ 
   // Play a jump sound 
   m_SM.playJump(); 
} 

Open up the Update.cpp file and add the highlighted line of code to play a success sound when both Thomas and Bob have simultaneously reached the goal for the current level:

// Detect collisions and see if characters have reached the goal tile 
// The second part of the if condition is only executed 
// when thomas is touching the home tile 
if (detectCollisions(m_Thomas) && detectCollisions(m_Bob)) 
{ 
   // New level required 
   m_NewLevelRequired = true; 
 
   // Play the reach goal sound 
   m_SM.playReachGoal(); 
 
} 
else 
{ 
   // Run bobs collision detection 
   detectCollisions(m_Bob); 
} 

Also within the Update.cpp file, we will add code to loop through the m_FireEmitters vector and decide when we need to call the playFire function of the SoundManager class.

Look closely at the small amount of context around the new highlighted code. It is essential to add this code in exactly the right place:

}// End if playing 
 
// Check if a fire sound needs to be played
vector<Vector2f>::iterator it;

// Iterate through the vector of Vector2f objects
for (it = m_FireEmitters.begin();it != m_FireEmitters.end(); it++)
{
   // Where is this emitter?
   // Store the location in pos
   float posX = (*it).x;
   float posY = (*it).y;
   // is the emiter near the player?
   // Make a 500 pixel rectangle around the emitter
   FloatRect localRect(posX - 250, posY - 250, 500, 500);

   // Is the player inside localRect?
   if (m_Thomas.getPosition().intersects(localRect))
   {
     // Play the sound and pass in the location as well
     m_SM.playFire(Vector2f(posX, posY), m_Thomas.getCenter());
   }
} 
    
// Set the appropriate view around the appropriate character 

The previous code is a bit like collision detection for sound. Whenever Thomas stays within a 500-by-500 pixel rectangle surrounding a fire emitter, the playFire function is called, passing in the coordinates of the emitter and of Thomas. The playFire function does the rest of the work and sets off a spatialized, looping sound effect.

Open up the DetectCollisions.cpp file, find the appropriate place, and add the highlighted code as shown in the following. The two highlighted lines of code trigger the playing of a sound effect when either character falls into a water or fire tile:

// Has character been burnt or drowned? 
// Use head as this allows him to sink a bit 
if (m_ArrayLevel[y][x] == 2 || m_ArrayLevel[y][x] == 3) 
{ 
   if (character.getHead().intersects(block)) 
   { 
      character.spawn(m_LM.getStartPosition(), GRAVITY); 
      // Which sound should be played? 
      if (m_ArrayLevel[y][x] == 2)// Fire, ouch! 
      { 
        // Play a sound 
        m_SM.playFallInFire(); 
 
      } 
      else // Water 
      { 
        // Play a sound 
        m_SM.playFallInWater(); 
      } 
   } 
} 

Playing the game will allow you to hear all the sounds, including cool spatialization, when near a fire tile.

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

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