Coding the loadLevel function

To be clear, this function is part of the Engine class, although it will delegate much of its work to other functions, including those of the LevelManager class that we just built.

First, let's add the declaration for the new function, along with some other new code, to the Engine.h file. Open the Engine.h file and add the highlighted lines of code shown in the following abbreviated snapshot of the Engine.h file:

#pragma once 
#include <SFML/Graphics.hpp> 
#include "TextureHolder.h" 
#include "Thomas.h" 
#include "Bob.h" 
#include "LevelManager.h" 
 
using namespace sf; 
 
class Engine 
{ 
private: 
   // The texture holder 
   TextureHolder th; 
 
   // Thomas and his friend, Bob 
   Thomas m_Thomas; 
   Bob m_Bob; 
 
   // A class to manage all the levels
   LevelManager m_LM; 
 
   const int TILE_SIZE = 50; 
   const int VERTS_IN_QUAD = 4; 
 
   // The force pushing the characters down 
   const int GRAVITY = 300; 
 
   // A regular RenderWindow 
   RenderWindow m_Window; 
 
   // The main Views 
   View m_MainView; 
   View m_LeftView; 
   View m_RightView; 
 
   // Three views for the background 
   View m_BGMainView; 
   View m_BGLeftView; 
   View m_BGRightView; 
 
   View m_HudView; 
 
   // Declare a sprite and a Texture for the background 
   Sprite m_BackgroundSprite; 
   Texture m_BackgroundTexture; 
 
   // Is the game currently playing? 
   bool m_Playing = false; 
 
   // Is character 1 or 2 the current focus? 
   bool m_Character1 = true; 
 
   // Start in full screen mode 
   bool m_SplitScreen = false; 
 
   // How much time is left in the current level 
   float m_TimeRemaining = 10; 
   Time m_GameTimeTotal; 
 
   // Is it time for a new/first level? 
   bool m_NewLevelRequired = true; 
 
   // The vertex array for the level tiles
   VertexArray m_VALevel;
   // The 2d array with the map for the level
   // A pointer to a pointer
   int** m_ArrayLevel =  NULL;
   // Texture for the level tiles
   Texture m_TextureTiles;

   // Private functions for internal use only 
   void input(); 
   void update(float dtAsSeconds); 
   void draw();    

   // Load a new level
   void loadLevel(); 

public: 
   // The Engine constructor 
   Engine(); 
 
   ... 
   ...       
   ... 

You can see the following in the previous code:

  • We included the LevelManager.h file
  • We added an instance of LevelManager called m_LM
  • We added a VertexArray called m_VALevel
  • We added a pointer to a pointer to an int that will hold the two-dimensional array that is returned from nextLevel
  • We added a new Texture object for the sprite sheet
  • We added the declaration for the loadLevel function that we will write now

Right-click Source Files in the Solution Explorer and select Add | New Item.... In the Add New Item window, highlight (by left-clicking) C++ File ( .cpp ) and then in the Name field, type LoadLevel.cpp. Finally, click the Add button. We are now ready to code the loadLevel function.

Add the code for the loadLevel function to the LoadLevel.cpp file, and then we can discuss it:

#include "stdafx.h" 
#include "Engine.h" 
 
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); 
 
   // 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; 
} 

First, we set m_Playing to false to stop parts of the update function from executing. Next, we loop through all the horizontal arrays within m_ArrayLevel and delete them. After the for loop, we delete m_ArrayLevel.

The code, m_ArrayLevel = m_LM.nextLevel(m_VALevel), calls nextLevel and prepares both the VertexArray and m_VALevel, as well as the two-dimensional m_ArrayLevel array. The level is set up and ready to go.

m_TimeRemaining is initialized by calling getTimeLimit, and Thomas and Bob are spawned using the spawn function along with the value returned from getStartPosition.

Finally, m_NewLevelRequired is set to false. As we will see in a few page's time, m_NewLevelRequired being set to true is what causes loadLevel to be called. We only want to run this function once.

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

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