Game Development Reference
In-Depth Information
n
509456361
t
687295179
This works because the
stringLength
passed to each
SDBMCalculator
template is used to index into
the string with 1 subtracted. Then as we start with the length of the string calling the
SDBMCalculate
method recursively down until we hit 1, we access the characters in the string in order.
Template metaprogramming can be hard to understand when you're just learning C++. Don't worry
about that too much for now. Come back to this code in the future when you're ready to tackle some
more template metaprogramming.
We'll update our
QuitOption
to use messages now that we can create a unique ID for the
QuitEvent
.
Using an Event to Quit the Game
Using an event to quit the game is relatively straightforward. To begin we'll update the
QuitOption::Evaluate
method to send the
QuitEvent
. Listing 21-20 shows how this is done.
Listing 21-20. Updating
QuitOption::Evaluate
namespace
{
constexpr int QuitEvent = SDBMCalculator< >::CalculateValue("QuitEvent");
}
void QuitOption::Evaluate(Player& player)
{
SendEvent(QuitEvent);
}
You can finally see the benefit of using the
friend
functions with the
EventManager
, as sending the
QuitEvent
is just a single line of code!
The main change required is to inherit the
Game
class from
EventHandler
as shown in Listing 21-21.
Listing 21-21. Updating the
Game
Class
class Game
: public EventHandler
{
private:
static const unsigned int m_numberOfRooms = 4;
using Rooms = std::array<Room, m_numberOfRooms>;
Rooms m_rooms;
Player m_player;