Structuring the Thomas Was Late code

One of the problems that has been quite pronounced in both projects so far is how long and unwieldy the code gets. OOP allows us to break our projects up into logical and manageable chunks called classes.

We will make a big improvement to the manageability of the code in this project with the introduction of an Engine class. Among other functions, the Engine class will have three private functions. They are input, update, and draw. This should sound very familiar. Each of these functions will hold a chunk of the code that was previously all in the main function. Each of these functions will be in a code file of its own, Input.cpp, Update.cpp, and Draw.cpp respectively.

There will also be one public function in the Engine class, which can be called with an instance of Engine. This function is run and will be responsible for calling input, update, and draw, once for each frame of the game:

Structuring the Thomas Was Late code

Furthermore, because we have abstracted the major parts of the game engine to the Engine class, we can also move many of the variables from main and make them members of Engine. All we need to do to get our game engine fired up is create an instance of Engine and call its run function. Here is a sneak preview of the super-simple main function:

int main() 
{ 
   // Declare an instance of Engine 
   Engine engine; 
 
   // Start the engine 
   engine.run(); 
 
   // Quit in the usual way when the engine is stopped 
   return 0; 
} 

Tip

Don't add the preceding code just yet.

To make our code even more manageable and readable, we will also abstract responsibility for big tasks, such as loading a level and collision detection, to separate functions (in separate code files). These two functions are loadLevel and detectCollisions. We will also code other functions to handle some of the new features of the Thomas Was Late project. We will cover them in detail as and when they occur.

To further take advantage of OOP, we will delegate responsibility for particular areas of the game entirely to new classes. You probably remember that the sound and HUD code was quite lengthy in previous projects. We will build a SoundManager and HUD class to handle these aspects in a cleaner manner. Exactly how they work will be explored in depth when we implement them.

The game levels themselves are much more in-depth than previous games, so we will also code a LevelManager class.

As you would expect, the playable characters will be made with classes as well. For this project, however, we will learn some more C++ and implement a PlayableCharacter class with all the common functionality of Thomas and Bob, and then Thomas and Bob classes, which will inherit this common functionality as well as implement their own unique functions and abilities. This, perhaps unsurprisingly, is called inheritance. I will go into more detail about inheritance in the following Chapter 13Advanced OOP, Inheritance, and Polymorphism.

We will also implement a number of other classes to perform specific responsibilities. For example, we will make some neat explosions using particle systems. You might be able to guess that to do this, we will code a Particle class and a ParticleSystem class. All of these classes will have instances that are members of the Engine class. Doing things this way will make all the features of the game accessible from the game engine, but encapsulate the details into appropriate classes.

The last thing to mention before we move on to see the actual code that will make the Engine class is that we will reuse, without any changes whatsoever, the TextureHolder class, which we discussed and coded for the Zombie Arena game.

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

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