1
00:00:01,006 --> 00:00:03,007
- [Instructor] Coding
the loadLevel Function.

2
00:00:03,007 --> 00:00:05,004
In the previous video, we learned

3
00:00:05,004 --> 00:00:07,009
building the level manager class.

4
00:00:07,009 --> 00:00:10,001
In this video we'll have the declaration

5
00:00:10,001 --> 00:00:13,008
and the code for the loadLevel function.

6
00:00:13,008 --> 00:00:15,005
To be clear, this function

7
00:00:15,005 --> 00:00:17,004
is part of the engine class, although

8
00:00:17,004 --> 00:00:19,000
it will delegate much of its work

9
00:00:19,000 --> 00:00:20,009
to other functions, including those

10
00:00:20,009 --> 00:00:24,000
of the LevelManager
class that we just built.

11
00:00:24,000 --> 00:00:26,004
First, open the engine.h file

12
00:00:26,004 --> 00:00:29,000
from the solutions explorer window,

13
00:00:29,000 --> 00:00:30,009
and then we'll add the declaration

14
00:00:30,009 --> 00:00:32,001
for the new function, along with

15
00:00:32,001 --> 00:00:36,006
some other new code to the engine.h file.

16
00:00:36,006 --> 00:00:41,006
Let's write the code after #include Bob.h.

17
00:00:41,006 --> 00:00:46,008
Type #include "LevelManager.h".

18
00:00:46,008 --> 00:00:51,000
In this slide, we will
include LevelManger.h file.

19
00:00:51,000 --> 00:00:55,009
Now, after Bob m_Bob, add a comment,

20
00:00:55,009 --> 00:00:58,005
a class to manage all the levels,

21
00:00:58,005 --> 00:01:01,009
LevelManager m_LM.

22
00:01:01,009 --> 00:01:04,007
Here we're adding an
instance of LevelManager

23
00:01:04,007 --> 00:01:07,009
called m_LM.

24
00:01:07,009 --> 00:01:12,009
Next, after bool
m_NewLevelRequired equal to true,

25
00:01:12,009 --> 00:01:15,003
add this highlighted code.

26
00:01:15,003 --> 00:01:20,001
First, we add a vertex
array called m_VALevel,

27
00:01:20,001 --> 00:01:22,001
then we add a pointer to a pointer

28
00:01:22,001 --> 00:01:23,007
to an int that will hold

29
00:01:23,007 --> 00:01:24,008
the two dimensional array

30
00:01:24,008 --> 00:01:27,003
that is returned from next level.

31
00:01:27,003 --> 00:01:29,003
We then add a new texture object

32
00:01:29,003 --> 00:01:31,002
for the sprite sheet.

33
00:01:31,002 --> 00:01:34,000
After the draw function, write a comment,

34
00:01:34,000 --> 00:01:39,002
load a new level, void loadLevel.

35
00:01:39,002 --> 00:01:40,008
Here we have added the declaration

36
00:01:40,008 --> 00:01:44,002
for the loadLevel function
that we will write now.

37
00:01:44,002 --> 00:01:46,004
So right click source files

38
00:01:46,004 --> 00:01:49,008
in the solution explorer
and select add new item.

39
00:01:49,008 --> 00:01:51,003
In the add new item windows

40
00:01:51,003 --> 00:01:54,009
click C++ file, and then in the name field

41
00:01:54,009 --> 00:01:56,007
type loadLevel.

42
00:01:56,007 --> 00:01:58,006
Lastly click add.

43
00:01:58,006 --> 00:02:01,006
We are now read to code
the loadLevel function.

44
00:02:01,006 --> 00:02:03,004
Add this highlighted code to the file

45
00:02:03,004 --> 00:02:05,002
for the loadLevel function.

46
00:02:05,002 --> 00:02:08,003
We start by adding necessary
include directives.

47
00:02:08,003 --> 00:02:10,002
Inside the loadLevel function,

48
00:02:10,002 --> 00:02:13,001
first we set m_Playing to false

49
00:02:13,001 --> 00:02:14,008
to stop parts of the update function

50
00:02:14,008 --> 00:02:16,003
from executing.

51
00:02:16,003 --> 00:02:19,001
Next, we loop through
all the horizontal arrays

52
00:02:19,001 --> 00:02:22,008
within m_ArrayLevel and delete them.

53
00:02:22,008 --> 00:02:26,008
After for loop, we delete m_ArrayLevel.

54
00:02:26,008 --> 00:02:29,003
This line of code calls nextLevel

55
00:02:29,003 --> 00:02:33,002
and prepares both the
vertex array and m_VALevel,

56
00:02:33,002 --> 00:02:37,003
as well as a two dimensional
m_ArrayLevel array.

57
00:02:37,003 --> 00:02:39,005
The level is setup and ready to go.

58
00:02:39,005 --> 00:02:42,002
m_TimeRemaining is initialized

59
00:02:42,002 --> 00:02:45,003
by calling getTimeLimit, and Thomas

60
00:02:45,003 --> 00:02:48,004
and Bob are spawned
using the spawn function

61
00:02:48,004 --> 00:02:49,007
along with the value returned

62
00:02:49,007 --> 00:02:51,008
from getStartPosition.

63
00:02:51,008 --> 00:02:56,008
Lastly, m_NewLevelRequired
is set to false.

64
00:02:56,008 --> 00:02:58,005
As we will see in some time,

65
00:02:58,005 --> 00:03:01,009
m_NewLevelRequired being set to true

66
00:03:01,009 --> 00:03:04,006
is what causes loadLevel to be called.

67
00:03:04,006 --> 00:03:07,003
We only want to run this function once.

68
00:03:07,003 --> 00:03:08,002
Great.

69
00:03:08,002 --> 00:03:10,005
In this video we've seen how to code

70
00:03:10,005 --> 00:03:12,002
the loadLevel function.

71
00:03:12,002 --> 00:03:16,001
In the next video we'll be
updating the game engine.

