- [Instructor] This is building the Thomas and Bob classes.
In the last video,
we looked at building the playable character class.
In this video,
we're going to take a look at coding Thomas.h,
Thomas.cpp,
Bob.h,
and Bob.cpp.
Now we get to use inheritance for real.
We will build a class for Thomas and a class for Bob.
They will both inherit from the playable character class
we just coded.
They will then have all the functionality
of the playable character class,
including direct access to its protected variables.
We will also add the definition
for the pure virtual function handle input.
You will notice that the handle input functions
for Thomas and Bob will be different.
Let's start with coding Thomas.h.
Right-click Header Files in the Solution Explorer
and select Add New Item,
then highlight by left-clicking Header File.h,
and then in the name field, type Thomas.
Click Add button at the end.
We're now ready to code the header file
for the Thomas class.
Now add this code to the Thomas.h class.
The code is very short and sweet.
The Thomas class is inheriting
from the playable character class we have just coded.
It will have all of the functionality
of the playable character class.
Here you can see that we have a constructor
and notice that we have added the definition
of the pure virtual handleInput function,
and now we're going to implement it.
So moving on to coding Thomas.cpp.
Right-click Source Files in the Solution Explorer
and select Add New Item.
In this window,
click C++ file,
and then in the name field,
type Thomas.
So you have the file name Thomas.cpp.
Click Add.
We can now code the .cpp file for the Thomas class.
Add the Thomas constructor to the file,
as I just did.
Now, in this code,
first we add the necessary include directives,
and this is the constructor function, Thomas.
Here we load the thomas.png graphics
and then set the duration of a jump, m_JumpDuration,
to 0.45, nearly half a second.
Now add the definition of the handleInput function.
This highlighted code should look quite familiar.
We are using the SFML isKeyPressed function
to see whether any of the W, A, or D keys are pressed.
Now, when W key is pressed,
they player is attempting to jump.
The code then uses the if not m_IsJumping
and not m_IsFalling condition,
which checks if the character's not already jumping
and that it is not falling, either.
When these tests are both true,
m_IsJumping is set to true,
m_TimeThisJump is set to zero,
and m_JustJumped is set to true.
When the previous two tests don't evaluate to true,
the else clause is executed,
and m_Jumping is set to false
and m_IsFalling is set to true.
Now, the handling of the A and D keys being pressed
is as simple as setting m_LeftPressed
or m_RightPressed to true or false.
The update function will now be able
to handle moving the character.
The last line of code in the function
returns the value of m_JustJumped.
This'll let the calling code know
if it needs to play a jumping sound effect.
We will now code the Bob class,
although this is nearly identical to the Thomas class,
except it has different jumping abilities,
a different texture,
and uses different keys on the keyboard.
For coding Bob.h,
right-click Header Files in the Solution Explorer
and click Add New Item.
Then click Header File.h,
and then in the Name field, type Bob.
Lastly, click Add button.
Now let's code the header file for the Bob class.
Add this highlighted code to the file.
First we've added the necessarily include directives.
See that the Bob class is identical in structure
to the Thomas class,
it inherits from PlayableCharacter,
and it has a constructor same as the class name Bob.
The difference compared to Thomas
is that we initialize
some of Bob's member variables differently
and we handle input in the handleInput function
differently as well.
Now, moving on to coding Bob.cpp.
Just as we always do,
right-click Source Files in the Solution Explorer
and select Add New Item.
In the Add New Item window,
click C++ File,
and then in the Name field, type Bob.
Finally, click Add.
So we are now ready to code the .cpp file
for the Bob class.
Now add this code to the Bob constructor to the file.
This is the Bob constructor.
Notice that the texture is different,
bob.png,
and that m_JumpDuration is initialized
to a significantly smaller value,
that is, 0.25.
Bob is now his own unique self.
Now add the handleInput code
immediately after the Bob constructor.
I've highlighted the whole code.
Notice that the code is nearly identical
to the code in the hanldeInput function
of the Thomas class.
The only difference is that we respond to different keys,
up arrow key for jump,
the left arrow key,
and the right arrow key.
Now we have a playable character class
that has been extended by Bob and Thomas.
We can add a Bob and Thomas instance to the game.
Cool.
In this video, we have learned
building the Thomas and Bob classes.
Great!
Next up, we'll update the game engine
to use Thomas and Bob.