Game Development Reference
In-Depth Information
void Game::RunGame()
{
WelcomePlayer();
bool shouldEnd = false;
while (shouldEnd == false)
{
GivePlayerOptions();
string playerInput;
GetPlayerInput(playerInput);
shouldEnd = EvaluateInput(playerInput) == PlayerOptions::Quit;
}
}
The examples so far in this chapter had all defined the class methods inside the class declaration.
Our Game class now declares the methods in the header file and creates the declarations in a source
file. You can see how we achieve this by prepending the class name to the method name like this:
void Game::WelcomePlayer()
Another change made to the methods when comparing them to the functions in Chapter 7 is that
they can now access the m_player object as a member variable rather than requiring that it be
passed into the methods. You can also see that we have moved our entire game loop into the
RunGame method. This is the change that has allowed you to make all of the other methods that make
up the Game class private. This is a good example of encapsulation, as any other code that now uses
the Game class can only ask it to run. All of its implementation details have been hidden behind its
public interface.
We have been altering our Player class as we moved through this chapter, and the final result is
shown in Listing 8-17.
Listing 8-17. The Player Class
class Player
{
private:
std::string m_name;
public:
void SetName(const std::string& name)
{
m_name = name;
}
const std::string& GetName() const
{
return m_name;
}
};
 
Search WWH ::




Custom Search