- [Narrator] Hi, this is the last video of this section,
Updating the Game Engine to Use Thomas and Bob.
In the previous video,
we looked at building Thomas and Bob classes.
In this video, we'll be updating Engine.h,
the input function, the update function,
and also we'll draw Bob and Thomas.
In order to be able to run the game
and see our new characters,
we have to declare instances of them,
call their spawn functions,
update them each frame and draw them each frame.
Let's start with declaring their instances now.
So, open up the Engine.h file
and add the include directives first.
#include "Thomas.h"
#include "Bob.h"
Now, inside private section, after "TextureHolder th,"
write a comment "//Thomas and his friend , bob"
then "Thomas m_Thomas;"
and "Bob m_Bob;"
So, we have an instance of both Thomas and Bob
which are derived from playable character.
Now, we'll add the ability to control the two characters.
The code we'll add will go in the input part.
Of course, for this project,
we have a dedicated input function.
Open up "Input.cpp" and add this highlighted code.
Note that the code has all the functionalities
contained with Thomas and Bob classes.
All the code has to do is add an include directive
for each of the Thomas and Bob classes.
Then, within the input function,
the code just calls the pure virtual Handle input functions
on m_Thomas and m_Bob.
The reason we wrap each of the calls in an "if" statement
is because they return it true or false based upon
whether a new jump has just been successfully initiated.
We will handle playing the jump sound effects
in section four, Sound Specialization and HUD.
Let's move on to updating the update function
to spawn and update the playable character instances.
This is broken down into two parts.
First, we need to spawn Bob and Thomas
at the start of a new level.
And second, we need to update
by calling their update functions each frame.
We need to call the spawn functions of our Thomas
and Bob objects in a few different places
as the project progresses.
Most obviously, we need to spawn the two characters
when a new level begins.
In this section, as the number of tasks we need to perform
at the beginning of a level increases,
we will write a LoadLevel function.
For now, lets just call spawn on m_Thomas
and m_Bob in the update function,
as seen in this highlighted code.
Add the code but keep in mind that this code
will eventually be deleted and replaced.
The entire code is wrapped in an "if" statement
that checks whether a new level is required.
Then this code simply calls spawn and passes in a location
in the game world along with the gravity.
The actual spawning code will be moved
to a dedicated LoadLevel function but the "if" condition
will be part of the finished project.
Also, m_TimeRemaining is set to an arbitrary 10 seconds.
And here, we set m_NewLevelRequired to false.
Next, we update Thomas and Bob.
So, inside the "if" loop for condition m_Playing,
add a comment, "//Update Thomas"
then "m_Thomas.update(dtAsSeconds);."
Similarly, "//Update Bob"
"m_Bob.update(dtAsSeconds);"
All we do here is call their update functions
and pass in the time this frame has taken.
Now that the characters can move,
we need to update the appropriate view objects
to center around the characters
and make them the center of attention.
Of course, until we have some objects in our game world,
the sensation of actual movement will not be achieved.
Add this highlighted code
just after "End if playing" comment.
The code handles the two possible situations.
First, the "if (m_SplitScreen)" condition positions
the left hand view around m_Thomas
and here it positions the right hand view around m_Bob.
The "else" clause that executes
when the game is in full screen mode
tests to see if "m_Character1" is true.
If it is, then the full screen view "m_MainView"
is centered around Thomas.
Otherwise, in an "else" condition,
it is centered around Bob.
You probably remember that the player can use the E key
to toggle split screen mode and the Q key to toggle
between Bob and Thomas in full screen mode.
We coded this in the input function of the Engine class
back in the previous section,
Abstraction and Code Management; Making Better Use of OOP.
Now, let's draw Bob and Thomas.
Make sure the "Draw.cpp" file is open
and then if not equal to "m_SplitScreen" after
"m_Window.setView" write a comment,
"//Draw Thomas"
"m_Window.draw(m_Thomas.getSprite ());"
In the same way, draw Bob.
"m_Window.draw(m_Bob.getSprite ());"
Notice that we drew both Thomas and Bob for the full screen.
Now, we'll draw them for left and right.
After "m_Window.setView" write comment,
"//Draw bob"
"m_Window.draw(m_Bob.getSprite());"
Similarly, draw Thomas.
"m_Window.draw(m_Thomas.getSprite());"
Here, we have drawn Thomas and Bob for left view.
Now, note the very subtle difference in the way
that we draw the character in split screen mode.
When drawing the left side of the screen,
we switch the order the characters are drawn
and draw Thomas after Bob.
So, Thomas will always be on top on the left
and Bob on the right.
This is because the player controlling Thomas is catered for
on the left and Bob on the right.
You can run the game by clicking on
"Local Windows Debugger" button.
You can move Bob with the help of arrow keys.
See he moves towards left and right.
If you press the Q key to switch focus from Bob to Thomas,
you can move Thomas using keys A and D.
I'm moving Thomas to the left using key A
and to the right using D key.
If you move either of the characters left or right,
you will see them move relative to each other.
Now, try pressing the E key to toggle between full screen
and split screen.
This is the split screen.
Then try moving both characters again to see the effect.
You can see that Thomas is always centered
in the left hand window
and Bob is always centered in the right hand window.
If you leave the game running long enough,
the characters will re-spawn
in their original positions every 10 seconds,
as you can see here.
This is the beginning of the functionality we will need
for the finish game.
This behavior is cause by "m_TimeRemaining" going below zero
and then setting the
"m_NewLevelRequired" variable to "true."
Also note that we can't see the full effect of movement
until we draw the details of the level.
In fact, although it can't be seen,
both characters are continuously falling
at 300 pixels per second.
As the camera is centering around them every frame
and there are no other objects in the game world,
we cannot see this downward movement.
If you want to demonstrate this to yourself,
in "Update.cpp" just change the call to "m_Bob.spawn"
Change this 100 to zero and gravity to zero.
Now that Bob has no gravitational effect,
he'll move an upward direction.
Thomas will visibly fall away from him.
We will add some playable levels to interact with
in the next section.
In this video, we've update the game engine
to use Thomas and Bob.
Amazing!
In this section,
we started off by learning inheritance and polymorphism.
Then looked at abstract classes
and building the playable character class.
Lastly, we explored building the Thomas and Bob classes
and updating the game engine to use Thomas and Bob.
Awesome!
In the next section, we'll learn
"Building Playable Levels and Collision Detection."