1
00:00:01,003 --> 00:00:04,000
- [Narrator] Hi, this is the
last video of this section,

2
00:00:04,000 --> 00:00:06,005
Updating the Game Engine
to Use Thomas and Bob.

3
00:00:06,005 --> 00:00:08,002
In the previous video,

4
00:00:08,002 --> 00:00:11,000
we looked at building
Thomas and Bob classes.

5
00:00:11,000 --> 00:00:13,009
In this video, we'll be updating Engine.h,

6
00:00:13,009 --> 00:00:16,006
the input function, the update function,

7
00:00:16,006 --> 00:00:18,008
and also we'll draw Bob and Thomas.

8
00:00:18,008 --> 00:00:20,008
In order to be able to run the game

9
00:00:20,008 --> 00:00:22,004
and see our new characters,

10
00:00:22,004 --> 00:00:24,006
we have to declare instances of them,

11
00:00:24,006 --> 00:00:26,000
call their spawn functions,

12
00:00:26,000 --> 00:00:29,004
update them each frame
and draw them each frame.

13
00:00:29,004 --> 00:00:32,005
Let's start with declaring
their instances now.

14
00:00:32,005 --> 00:00:34,008
So, open up the Engine.h file

15
00:00:34,008 --> 00:00:38,002
and add the include directives first.

16
00:00:38,002 --> 00:00:40,001
#include "Thomas.h"

17
00:00:40,001 --> 00:00:43,002
#include "Bob.h"

18
00:00:43,002 --> 00:00:46,009
Now, inside private section,
after "TextureHolder th,"

19
00:00:46,009 --> 00:00:50,005
write a comment "//Thomas
and his friend , bob"

20
00:00:50,005 --> 00:00:53,001
then "Thomas m_Thomas;"

21
00:00:53,001 --> 00:00:55,009
and "Bob m_Bob;"

22
00:00:55,009 --> 00:00:58,006
So, we have an instance
of both Thomas and Bob

23
00:00:58,006 --> 00:01:01,007
which are derived from playable character.

24
00:01:01,007 --> 00:01:04,006
Now, we'll add the ability to
control the two characters.

25
00:01:04,006 --> 00:01:07,008
The code we'll add will
go in the input part.

26
00:01:07,008 --> 00:01:09,001
Of course, for this project,

27
00:01:09,001 --> 00:01:11,001
we have a dedicated input function.

28
00:01:11,001 --> 00:01:16,006
Open up "Input.cpp" and
add this highlighted code.

29
00:01:16,006 --> 00:01:18,004
Note that the code has
all the functionalities

30
00:01:18,004 --> 00:01:21,003
contained with Thomas and Bob classes.

31
00:01:21,003 --> 00:01:24,006
All the code has to do is
add an include directive

32
00:01:24,006 --> 00:01:27,001
for each of the Thomas and Bob classes.

33
00:01:27,001 --> 00:01:28,009
Then, within the input function,

34
00:01:28,009 --> 00:01:32,006
the code just calls the pure
virtual Handle input functions

35
00:01:32,006 --> 00:01:36,006
on m_Thomas and m_Bob.

36
00:01:36,006 --> 00:01:39,007
The reason we wrap each of
the calls in an "if" statement

37
00:01:39,007 --> 00:01:42,003
is because they return it
true or false based upon

38
00:01:42,003 --> 00:01:45,005
whether a new jump has just
been successfully initiated.

39
00:01:45,005 --> 00:01:48,001
We will handle playing
the jump sound effects

40
00:01:48,001 --> 00:01:51,009
in section four, Sound
Specialization and HUD.

41
00:01:51,009 --> 00:01:54,002
Let's move on to updating
the update function

42
00:01:54,002 --> 00:01:57,009
to spawn and update the
playable character instances.

43
00:01:57,009 --> 00:02:00,001
This is broken down into two parts.

44
00:02:00,001 --> 00:02:02,005
First, we need to spawn Bob and Thomas

45
00:02:02,005 --> 00:02:04,001
at the start of a new level.

46
00:02:04,001 --> 00:02:06,000
And second, we need to update

47
00:02:06,000 --> 00:02:09,001
by calling their update
functions each frame.

48
00:02:09,001 --> 00:02:11,004
We need to call the spawn
functions of our Thomas

49
00:02:11,004 --> 00:02:13,009
and Bob objects in a few different places

50
00:02:13,009 --> 00:02:15,005
as the project progresses.

51
00:02:15,005 --> 00:02:18,004
Most obviously, we need to
spawn the two characters

52
00:02:18,004 --> 00:02:20,005
when a new level begins.

53
00:02:20,005 --> 00:02:23,002
In this section, as the number
of tasks we need to perform

54
00:02:23,002 --> 00:02:24,009
at the beginning of a level increases,

55
00:02:24,009 --> 00:02:27,005
we will write a LoadLevel function.

56
00:02:27,005 --> 00:02:31,002
For now, lets just call spawn on m_Thomas

57
00:02:31,002 --> 00:02:34,000
and m_Bob in the update function,

58
00:02:34,000 --> 00:02:36,007
as seen in this highlighted code.

59
00:02:36,007 --> 00:02:39,005
Add the code but keep
in mind that this code

60
00:02:39,005 --> 00:02:42,001
will eventually be deleted and replaced.

61
00:02:42,001 --> 00:02:45,001
The entire code is wrapped
in an "if" statement

62
00:02:45,001 --> 00:02:48,001
that checks whether a
new level is required.

63
00:02:48,001 --> 00:02:51,005
Then this code simply calls
spawn and passes in a location

64
00:02:51,005 --> 00:02:53,005
in the game world along with the gravity.

65
00:02:53,005 --> 00:02:56,003
The actual spawning code will be moved

66
00:02:56,003 --> 00:02:59,005
to a dedicated LoadLevel
function but the "if" condition

67
00:02:59,005 --> 00:03:02,001
will be part of the finished project.

68
00:03:02,001 --> 00:03:07,004
Also, m_TimeRemaining is set
to an arbitrary 10 seconds.

69
00:03:07,004 --> 00:03:12,008
And here, we set
m_NewLevelRequired to false.

70
00:03:12,008 --> 00:03:15,008
Next, we update Thomas and Bob.

71
00:03:15,008 --> 00:03:18,009
So, inside the "if" loop
for condition m_Playing,

72
00:03:18,009 --> 00:03:22,007
add a comment, "//Update Thomas"

73
00:03:22,007 --> 00:03:26,009
then "m_Thomas.update(dtAsSeconds);."

74
00:03:26,009 --> 00:03:29,001
Similarly, "//Update Bob"

75
00:03:29,001 --> 00:03:34,000
"m_Bob.update(dtAsSeconds);"

76
00:03:34,000 --> 00:03:36,004
All we do here is call
their update functions

77
00:03:36,004 --> 00:03:39,001
and pass in the time this frame has taken.

78
00:03:39,001 --> 00:03:41,003
Now that the characters can move,

79
00:03:41,003 --> 00:03:43,009
we need to update the
appropriate view objects

80
00:03:43,009 --> 00:03:45,006
to center around the characters

81
00:03:45,006 --> 00:03:47,006
and make them the center of attention.

82
00:03:47,006 --> 00:03:50,001
Of course, until we have some
objects in our game world,

83
00:03:50,001 --> 00:03:53,000
the sensation of actual
movement will not be achieved.

84
00:03:53,000 --> 00:03:54,009
Add this highlighted code

85
00:03:54,009 --> 00:03:58,008
just after "End if playing" comment.

86
00:03:58,008 --> 00:04:01,002
The code handles the
two possible situations.

87
00:04:01,002 --> 00:04:04,007
First, the "if (m_SplitScreen)"
condition positions

88
00:04:04,007 --> 00:04:07,003
the left hand view around m_Thomas

89
00:04:07,003 --> 00:04:11,008
and here it positions the
right hand view around m_Bob.

90
00:04:11,008 --> 00:04:14,000
The "else" clause that executes

91
00:04:14,000 --> 00:04:15,008
when the game is in full screen mode

92
00:04:15,008 --> 00:04:19,001
tests to see if "m_Character1" is true.

93
00:04:19,001 --> 00:04:23,003
If it is, then the full
screen view "m_MainView"

94
00:04:23,003 --> 00:04:25,003
is centered around Thomas.

95
00:04:25,003 --> 00:04:27,001
Otherwise, in an "else" condition,

96
00:04:27,001 --> 00:04:29,000
it is centered around Bob.

97
00:04:29,000 --> 00:04:32,000
You probably remember that
the player can use the E key

98
00:04:32,000 --> 00:04:35,003
to toggle split screen mode
and the Q key to toggle

99
00:04:35,003 --> 00:04:38,008
between Bob and Thomas
in full screen mode.

100
00:04:38,008 --> 00:04:41,006
We coded this in the input
function of the Engine class

101
00:04:41,006 --> 00:04:43,002
back in the previous section,

102
00:04:43,002 --> 00:04:48,000
Abstraction and Code Management;
Making Better Use of OOP.

103
00:04:48,000 --> 00:04:50,002
Now, let's draw Bob and Thomas.

104
00:04:50,002 --> 00:04:53,002
Make sure the "Draw.cpp" file is open

105
00:04:53,002 --> 00:04:57,002
and then if not equal
to "m_SplitScreen" after

106
00:04:57,002 --> 00:05:01,003
"m_Window.setView" write a comment,

107
00:05:01,003 --> 00:05:02,005
"//Draw Thomas"

108
00:05:02,005 --> 00:05:10,001
"m_Window.draw(m_Thomas.getSprite ());"

109
00:05:10,001 --> 00:05:12,008
In the same way, draw Bob.

110
00:05:12,008 --> 00:05:19,006
"m_Window.draw(m_Bob.getSprite ());"

111
00:05:19,006 --> 00:05:22,007
Notice that we drew both Thomas
and Bob for the full screen.

112
00:05:22,007 --> 00:05:26,003
Now, we'll draw them for left and right.

113
00:05:26,003 --> 00:05:29,006
After "m_Window.setView" write comment,

114
00:05:29,006 --> 00:05:30,008
"//Draw bob"

115
00:05:30,008 --> 00:05:36,002
"m_Window.draw(m_Bob.getSprite());"

116
00:05:36,002 --> 00:05:37,009
Similarly, draw Thomas.

117
00:05:37,009 --> 00:05:45,009
"m_Window.draw(m_Thomas.getSprite());"

118
00:05:45,009 --> 00:05:49,008
Here, we have drawn Thomas
and Bob for left view.

119
00:05:49,008 --> 00:05:52,002
Now, note the very subtle
difference in the way

120
00:05:52,002 --> 00:05:54,008
that we draw the character
in split screen mode.

121
00:05:54,008 --> 00:05:57,004
When drawing the left side of the screen,

122
00:05:57,004 --> 00:05:59,007
we switch the order the
characters are drawn

123
00:05:59,007 --> 00:06:01,005
and draw Thomas after Bob.

124
00:06:01,005 --> 00:06:04,004
So, Thomas will always
be on top on the left

125
00:06:04,004 --> 00:06:06,002
and Bob on the right.

126
00:06:06,002 --> 00:06:09,001
This is because the player
controlling Thomas is catered for

127
00:06:09,001 --> 00:06:12,008
on the left and Bob on the right.

128
00:06:12,008 --> 00:06:14,004
You can run the game by clicking on

129
00:06:14,004 --> 00:06:16,007
"Local Windows Debugger" button.

130
00:06:16,007 --> 00:06:19,004
You can move Bob with
the help of arrow keys.

131
00:06:19,004 --> 00:06:21,009
See he moves towards left and right.

132
00:06:21,009 --> 00:06:25,005
If you press the Q key to
switch focus from Bob to Thomas,

133
00:06:25,005 --> 00:06:29,001
you can move Thomas using keys A and D.

134
00:06:29,001 --> 00:06:31,009
I'm moving Thomas to the left using key A

135
00:06:31,009 --> 00:06:35,001
and to the right using D key.

136
00:06:35,001 --> 00:06:37,006
If you move either of the
characters left or right,

137
00:06:37,006 --> 00:06:40,003
you will see them move
relative to each other.

138
00:06:40,003 --> 00:06:44,006
Now, try pressing the E key
to toggle between full screen

139
00:06:44,006 --> 00:06:46,004
and split screen.

140
00:06:46,004 --> 00:06:48,007
This is the split screen.

141
00:06:48,007 --> 00:06:52,000
Then try moving both characters
again to see the effect.

142
00:06:52,000 --> 00:06:54,007
You can see that Thomas is always centered

143
00:06:54,007 --> 00:06:55,009
in the left hand window

144
00:06:55,009 --> 00:06:58,009
and Bob is always centered
in the right hand window.

145
00:06:58,009 --> 00:07:01,000
If you leave the game running long enough,

146
00:07:01,000 --> 00:07:02,005
the characters will re-spawn

147
00:07:02,005 --> 00:07:04,009
in their original
positions every 10 seconds,

148
00:07:04,009 --> 00:07:07,003
as you can see here.

149
00:07:07,003 --> 00:07:09,004
This is the beginning of the
functionality we will need

150
00:07:09,004 --> 00:07:11,000
for the finish game.

151
00:07:11,000 --> 00:07:15,008
This behavior is cause by
"m_TimeRemaining" going below zero

152
00:07:15,008 --> 00:07:16,009
and then setting the

153
00:07:16,009 --> 00:07:20,005
"m_NewLevelRequired" variable to "true."

154
00:07:20,005 --> 00:07:23,002
Also note that we can't see
the full effect of movement

155
00:07:23,002 --> 00:07:25,004
until we draw the details of the level.

156
00:07:25,004 --> 00:07:27,001
In fact, although it can't be seen,

157
00:07:27,001 --> 00:07:29,004
both characters are continuously falling

158
00:07:29,004 --> 00:07:31,003
at 300 pixels per second.

159
00:07:31,003 --> 00:07:34,001
As the camera is centering
around them every frame

160
00:07:34,001 --> 00:07:36,001
and there are no other
objects in the game world,

161
00:07:36,001 --> 00:07:38,005
we cannot see this downward movement.

162
00:07:38,005 --> 00:07:40,007
If you want to demonstrate
this to yourself,

163
00:07:40,007 --> 00:07:47,001
in "Update.cpp" just change
the call to "m_Bob.spawn"

164
00:07:47,001 --> 00:07:50,008
Change this 100 to zero
and gravity to zero.

165
00:07:50,008 --> 00:07:53,006
Now that Bob has no gravitational effect,

166
00:07:53,006 --> 00:07:55,008
he'll move an upward direction.

167
00:07:55,008 --> 00:07:58,005
Thomas will visibly fall away from him.

168
00:07:58,005 --> 00:08:01,000
We will add some playable
levels to interact with

169
00:08:01,000 --> 00:08:03,000
in the next section.

170
00:08:03,000 --> 00:08:05,004
In this video, we've
update the game engine

171
00:08:05,004 --> 00:08:07,002
to use Thomas and Bob.

172
00:08:07,002 --> 00:08:08,004
Amazing!

173
00:08:08,004 --> 00:08:09,005
In this section,

174
00:08:09,005 --> 00:08:12,001
we started off by learning
inheritance and polymorphism.

175
00:08:12,001 --> 00:08:14,007
Then looked at abstract classes

176
00:08:14,007 --> 00:08:17,002
and building the playable character class.

177
00:08:17,002 --> 00:08:20,007
Lastly, we explored building
the Thomas and Bob classes

178
00:08:20,007 --> 00:08:23,007
and updating the game engine
to use Thomas and Bob.

179
00:08:23,007 --> 00:08:24,008
Awesome!

180
00:08:24,008 --> 00:08:26,007
In the next section, we'll learn

181
00:08:26,007 --> 00:08:29,009
"Building Playable Levels
and Collision Detection."

