1
00:00:01,004 --> 00:00:04,002
- [Instructor] This is building
the Thomas and Bob classes.

2
00:00:04,002 --> 00:00:05,004
In the last video,

3
00:00:05,004 --> 00:00:08,001
we looked at building the
playable character class.

4
00:00:08,001 --> 00:00:09,002
In this video,

5
00:00:09,002 --> 00:00:12,001
we're going to take a
look at coding Thomas.h,

6
00:00:12,001 --> 00:00:13,006
Thomas.cpp,

7
00:00:13,006 --> 00:00:15,000
Bob.h,

8
00:00:15,000 --> 00:00:17,002
and Bob.cpp.

9
00:00:17,002 --> 00:00:20,002
Now we get to use inheritance for real.

10
00:00:20,002 --> 00:00:23,001
We will build a class for
Thomas and a class for Bob.

11
00:00:23,001 --> 00:00:25,006
They will both inherit from
the playable character class

12
00:00:25,006 --> 00:00:26,009
we just coded.

13
00:00:26,009 --> 00:00:28,009
They will then have all the functionality

14
00:00:28,009 --> 00:00:30,005
of the playable character class,

15
00:00:30,005 --> 00:00:33,006
including direct access to
its protected variables.

16
00:00:33,006 --> 00:00:34,009
We will also add the definition

17
00:00:34,009 --> 00:00:37,006
for the pure virtual
function handle input.

18
00:00:37,006 --> 00:00:39,009
You will notice that the
handle input functions

19
00:00:39,009 --> 00:00:42,003
for Thomas and Bob will be different.

20
00:00:42,003 --> 00:00:45,000
Let's start with coding Thomas.h.

21
00:00:45,000 --> 00:00:47,008
Right-click Header Files
in the Solution Explorer

22
00:00:47,008 --> 00:00:49,006
and select Add New Item,

23
00:00:49,006 --> 00:00:53,007
then highlight by
left-clicking Header File.h,

24
00:00:53,007 --> 00:00:56,004
and then in the name field, type Thomas.

25
00:00:56,004 --> 00:00:58,002
Click Add button at the end.

26
00:00:58,002 --> 00:01:00,001
We're now ready to code the header file

27
00:01:00,001 --> 00:01:01,007
for the Thomas class.

28
00:01:01,007 --> 00:01:05,003
Now add this code to the Thomas.h class.

29
00:01:05,003 --> 00:01:07,009
The code is very short and sweet.

30
00:01:07,009 --> 00:01:09,004
The Thomas class is inheriting

31
00:01:09,004 --> 00:01:12,002
from the playable character
class we have just coded.

32
00:01:12,002 --> 00:01:13,004
It will have all of the functionality

33
00:01:13,004 --> 00:01:15,004
of the playable character class.

34
00:01:15,004 --> 00:01:17,003
Here you can see that
we have a constructor

35
00:01:17,003 --> 00:01:20,001
and notice that we have
added the definition

36
00:01:20,001 --> 00:01:22,005
of the pure virtual handleInput function,

37
00:01:22,005 --> 00:01:24,006
and now we're going to implement it.

38
00:01:24,006 --> 00:01:28,005
So moving on to coding Thomas.cpp.

39
00:01:28,005 --> 00:01:31,003
Right-click Source Files
in the Solution Explorer

40
00:01:31,003 --> 00:01:33,003
and select Add New Item.

41
00:01:33,003 --> 00:01:34,002
In this window,

42
00:01:34,002 --> 00:01:36,006
click C++ file,

43
00:01:36,006 --> 00:01:37,007
and then in the name field,

44
00:01:37,007 --> 00:01:39,001
type Thomas.

45
00:01:39,001 --> 00:01:42,004
So you have the file name Thomas.cpp.

46
00:01:42,004 --> 00:01:43,007
Click Add.

47
00:01:43,007 --> 00:01:47,002
We can now code the .cpp
file for the Thomas class.

48
00:01:47,002 --> 00:01:49,002
Add the Thomas constructor to the file,

49
00:01:49,002 --> 00:01:50,005
as I just did.

50
00:01:50,005 --> 00:01:52,006
Now, in this code,

51
00:01:52,006 --> 00:01:55,000
first we add the necessary
include directives,

52
00:01:55,000 --> 00:01:58,005
and this is the constructor
function, Thomas.

53
00:01:58,005 --> 00:02:01,008
Here we load the thomas.png graphics

54
00:02:01,008 --> 00:02:05,009
and then set the duration
of a jump, m_JumpDuration,

55
00:02:05,009 --> 00:02:08,009
to 0.45, nearly half a second.

56
00:02:08,009 --> 00:02:12,005
Now add the definition of
the handleInput function.

57
00:02:12,005 --> 00:02:15,003
This highlighted code
should look quite familiar.

58
00:02:15,003 --> 00:02:19,000
We are using the SFML
isKeyPressed function

59
00:02:19,000 --> 00:02:23,002
to see whether any of the
W, A, or D keys are pressed.

60
00:02:23,002 --> 00:02:26,000
Now, when W key is pressed,

61
00:02:26,000 --> 00:02:28,000
they player is attempting to jump.

62
00:02:28,000 --> 00:02:32,002
The code then uses the if not m_IsJumping

63
00:02:32,002 --> 00:02:34,007
and not m_IsFalling condition,

64
00:02:34,007 --> 00:02:37,001
which checks if the
character's not already jumping

65
00:02:37,001 --> 00:02:38,009
and that it is not falling, either.

66
00:02:38,009 --> 00:02:40,009
When these tests are both true,

67
00:02:40,009 --> 00:02:43,009
m_IsJumping is set to true,

68
00:02:43,009 --> 00:02:47,004
m_TimeThisJump is set to zero,

69
00:02:47,004 --> 00:02:50,008
and m_JustJumped is set to true.

70
00:02:50,008 --> 00:02:53,006
When the previous two tests
don't evaluate to true,

71
00:02:53,006 --> 00:02:55,003
the else clause is executed,

72
00:02:55,003 --> 00:02:58,004
and m_Jumping is set to false

73
00:02:58,004 --> 00:03:01,006
and m_IsFalling is set to true.

74
00:03:01,006 --> 00:03:04,009
Now, the handling of the
A and D keys being pressed

75
00:03:04,009 --> 00:03:08,003
is as simple as setting m_LeftPressed

76
00:03:08,003 --> 00:03:12,002
or m_RightPressed to true or false.

77
00:03:12,002 --> 00:03:13,008
The update function will now be able

78
00:03:13,008 --> 00:03:15,005
to handle moving the character.

79
00:03:15,005 --> 00:03:17,008
The last line of code in the function

80
00:03:17,008 --> 00:03:21,000
returns the value of m_JustJumped.

81
00:03:21,000 --> 00:03:22,006
This'll let the calling code know

82
00:03:22,006 --> 00:03:25,004
if it needs to play a
jumping sound effect.

83
00:03:25,004 --> 00:03:27,004
We will now code the Bob class,

84
00:03:27,004 --> 00:03:30,006
although this is nearly
identical to the Thomas class,

85
00:03:30,006 --> 00:03:32,009
except it has different jumping abilities,

86
00:03:32,009 --> 00:03:34,000
a different texture,

87
00:03:34,000 --> 00:03:37,000
and uses different keys on the keyboard.

88
00:03:37,000 --> 00:03:38,008
For coding Bob.h,

89
00:03:38,008 --> 00:03:41,003
right-click Header Files
in the Solution Explorer

90
00:03:41,003 --> 00:03:43,005
and click Add New Item.

91
00:03:43,005 --> 00:03:46,002
Then click Header File.h,

92
00:03:46,002 --> 00:03:49,001
and then in the Name field, type Bob.

93
00:03:49,001 --> 00:03:51,000
Lastly, click Add button.

94
00:03:51,000 --> 00:03:54,002
Now let's code the header
file for the Bob class.

95
00:03:54,002 --> 00:03:56,008
Add this highlighted code to the file.

96
00:03:56,008 --> 00:04:00,000
First we've added the
necessarily include directives.

97
00:04:00,000 --> 00:04:02,004
See that the Bob class
is identical in structure

98
00:04:02,004 --> 00:04:03,009
to the Thomas class,

99
00:04:03,009 --> 00:04:06,001
it inherits from PlayableCharacter,

100
00:04:06,001 --> 00:04:10,000
and it has a constructor
same as the class name Bob.

101
00:04:10,000 --> 00:04:11,008
The difference compared to Thomas

102
00:04:11,008 --> 00:04:12,008
is that we initialize

103
00:04:12,008 --> 00:04:15,000
some of Bob's member variables differently

104
00:04:15,000 --> 00:04:17,005
and we handle input in
the handleInput function

105
00:04:17,005 --> 00:04:18,009
differently as well.

106
00:04:18,009 --> 00:04:22,005
Now, moving on to coding Bob.cpp.

107
00:04:22,005 --> 00:04:24,001
Just as we always do,

108
00:04:24,001 --> 00:04:26,006
right-click Source Files
in the Solution Explorer

109
00:04:26,006 --> 00:04:28,005
and select Add New Item.

110
00:04:28,005 --> 00:04:30,005
In the Add New Item window,

111
00:04:30,005 --> 00:04:32,003
click C++ File,

112
00:04:32,003 --> 00:04:34,006
and then in the Name field, type Bob.

113
00:04:34,006 --> 00:04:36,007
Finally, click Add.

114
00:04:36,007 --> 00:04:40,005
So we are now ready to code the .cpp file

115
00:04:40,005 --> 00:04:42,008
for the Bob class.

116
00:04:42,008 --> 00:04:46,007
Now add this code to the
Bob constructor to the file.

117
00:04:46,007 --> 00:04:48,008
This is the Bob constructor.

118
00:04:48,008 --> 00:04:51,001
Notice that the texture is different,

119
00:04:51,001 --> 00:04:52,006
bob.png,

120
00:04:52,006 --> 00:04:56,004
and that m_JumpDuration is initialized

121
00:04:56,004 --> 00:04:58,003
to a significantly smaller value,

122
00:04:58,003 --> 00:05:00,006
that is, 0.25.

123
00:05:00,006 --> 00:05:04,004
Bob is now his own unique self.

124
00:05:04,004 --> 00:05:06,005
Now add the handleInput code

125
00:05:06,005 --> 00:05:08,008
immediately after the Bob constructor.

126
00:05:08,008 --> 00:05:10,006
I've highlighted the whole code.

127
00:05:10,006 --> 00:05:12,009
Notice that the code is nearly identical

128
00:05:12,009 --> 00:05:15,001
to the code in the hanldeInput function

129
00:05:15,001 --> 00:05:16,005
of the Thomas class.

130
00:05:16,005 --> 00:05:19,004
The only difference is that
we respond to different keys,

131
00:05:19,004 --> 00:05:21,002
up arrow key for jump,

132
00:05:21,002 --> 00:05:22,003
the left arrow key,

133
00:05:22,003 --> 00:05:24,002
and the right arrow key.

134
00:05:24,002 --> 00:05:26,005
Now we have a playable character class

135
00:05:26,005 --> 00:05:29,001
that has been extended by Bob and Thomas.

136
00:05:29,001 --> 00:05:32,000
We can add a Bob and Thomas
instance to the game.

137
00:05:32,000 --> 00:05:32,009
Cool.

138
00:05:32,009 --> 00:05:34,006
In this video, we have learned

139
00:05:34,006 --> 00:05:37,003
building the Thomas and Bob classes.

140
00:05:37,003 --> 00:05:38,002
Great!

141
00:05:38,002 --> 00:05:41,002
Next up, we'll update the game engine

142
00:05:41,002 --> 00:05:43,001
to use Thomas and Bob.

