- [Instructor] Coding the loadLevel Function.
In the previous video, we learned
building the level manager class.
In this video we'll have the declaration
and the code for the loadLevel function.
To be clear, this function
is part of the engine class, although
it will delegate much of its work
to other functions, including those
of the LevelManager class that we just built.
First, open the engine.h file
from the solutions explorer window,
and then we'll add the declaration
for the new function, along with
some other new code to the engine.h file.
Let's write the code after #include Bob.h.
Type #include "LevelManager.h".
In this slide, we will include LevelManger.h file.
Now, after Bob m_Bob, add a comment,
a class to manage all the levels,
LevelManager m_LM.
Here we're adding an instance of LevelManager
called m_LM.
Next, after bool m_NewLevelRequired equal to true,
add this highlighted code.
First, we add a vertex array called m_VALevel,
then we add a pointer to a pointer
to an int that will hold
the two dimensional array
that is returned from next level.
We then add a new texture object
for the sprite sheet.
After the draw function, write a comment,
load a new level, void loadLevel.
Here we have added the declaration
for the loadLevel function that we will write now.
So right click source files
in the solution explorer and select add new item.
In the add new item windows
click C++ file, and then in the name field
type loadLevel.
Lastly click add.
We are now read to code the loadLevel function.
Add this highlighted code to the file
for the loadLevel function.
We start by adding necessary include directives.
Inside the loadLevel function,
first we set m_Playing to false
to stop parts of the update function
from executing.
Next, we loop through all the horizontal arrays
within m_ArrayLevel and delete them.
After for loop, we delete m_ArrayLevel.
This line of code calls nextLevel
and prepares both the vertex array and m_VALevel,
as well as a two dimensional m_ArrayLevel array.
The level is setup and ready to go.
m_TimeRemaining is initialized
by calling getTimeLimit, and Thomas
and Bob are spawned using the spawn function
along with the value returned
from getStartPosition.
Lastly, m_NewLevelRequired is set to false.
As we will see in some time,
m_NewLevelRequired being set to true
is what causes loadLevel to be called.
We only want to run this function once.
Great.
In this video we've seen how to code
the loadLevel function.
In the next video we'll be updating the game engine.