Game Development Reference
In-Depth Information
bool playerWon = false;
while (m_playerQuit == false
&&
playerWon == false)
{
GivePlayerOptions();
stringstream playerInputStream;
GetPlayerInput(playerInputStream);
EvaluateInput(playerInputStream);
playerWon = m_dragon.IsAlive() == false && m_orc.IsAlive() == false;
}
if (playerWon == true)
{
cout << "Congratulations, you rid the dungeon of monsters!" << endl;
cout << "Type goodbye to end" << endl;
std::string input;
cin >>input;
}
DetachEvent(QuitEvent, this);
delete EventManager::GetSingletonPtr();
}
The
RunGame
method now has code to create an
EventManager
, to register the
QuitEvent
, and to
attach itself as a listener for that event. The last two lines also detach from the event and delete the
EventManager
. The other main change is that
EvaluateInput
is no longer required to return the chosen
option. We now catch the event in the
HandleEvent
method, which you can see in Listing 21-23.
Listing 21-23. The
Game::HandleEvent
Method
void Game::HandleEvent(const Event* pEvent)
{
if (pEvent->GetID() == QuitEvent)
{
m_playerQuit = true;
}
}
Handling events is really easy. You just need to check the ID of the
Event
and carry out the required
action, which in this case is setting the
m_playerQuit
variable to
true
. If we had attached to more
events we could have used
else if
statements to check against more
EventIDs
.