Game Development Reference
In-Depth Information
private:
using JoiningRooms = std::array<Room*, static_cast<size_t>(JoiningDirections::Max)>;
JoiningRooms m_pJoiningRooms;
using StaticOptions = std::map<unsigned int, Option*>;
StaticOptions m_staticOptions;
unsigned int m_staticOptionStartKey{ 1 };
using DynamicOptions = std::vector<Option*>;
DynamicOptions m_dynamicOptions;
};
A type alias is created for all three of the STL containers in use. The
JoiningRooms
alias uses a
static_cast
to convert the
JoiningDirections::Max
enum element into a usable size for the STL
array
. The compiler would give you a specialization error because it cannot implicitly cast an enum
class value, and the
static_cast
avoids this error. There are additional type aliases, one for the map
to store static options and one for the vector that stores the dynamic options.
The
map
is keyed by unsigned integers that will allow us to use the numbers entered by the user to
access the relevant pointer for the selected option. They keys are set automatically by the
Room
class
as the
Options
are added so we set the initial key to 1.
Note
Initializing
m_staticOptionStartKey
in this manner is using a new C++11 feature. Before C++11
only static constants that were declared using built-in types could be initialized inside a class. C++11 added
the ability to initialize members where they are declared to reduce the size of constructors.
Listing 19-3 shows the new methods that have been added to the
Room
class to add dynamic and
static options.
Listing 19-3. Adding Dynamic and Static Options to
Room
void Room::AddStaticOption(Option* option)
{
assert(option != nullptr);
m_staticOptions[m_staticOptionStartKey++] = option;
}
void Room::AddDynamicOption(Option* option)
{
assert(option != nullptr);
m_dynamicOptions.push_back(option);
}