Game Development Reference
In-Depth Information
Here you can see a const unsigned int that stores the number of rooms our game contains. The
game will have the same T layout for the game rooms that we used in Chapter 12. The second line
uses a type alias to reduce the amount of typing we would need to reference the type of the array in
other places in the code. We then use the alias to create the array of rooms.
The rest of the code to deal with the Rooms array remains the same. This is one of the main reasons
you should switch from old C-style arrays to newer style arrays. The newer style arrays can be used
with other STL algorithms and also provide iterator support, which C-style arrays do not.
The Rooms array change is relatively straightforward, but the changes to the Options are much more
substantial.
Using a vector and a map to Store Options
In Chapter 11 the Option pointers were stored in a C-style array in the Game class. This chapter
reworks the Option storage entirely. The first significant change is that the Option pointers are no
longer stored in the Game class. Each Room object stores pointers to the relevant options instead. This
allows us to do some things a little differently. One drawback of the previous solution was that all of
the options were presented for every room. In this chapter, we only show options that have a logical
action. This means the player will never see an option to move east if the current Room does not
have a Room joining to the east. This is a general improvement in usability.
The options for moving between rooms and for quitting the game are constant operations that
never change. Our new gameplay logic also includes operations that can only be performed once.
These are the options to open a chest and the option to attack an enemy. For simplicity's sake, the
enemies in our game are currently killed in a single attack and a chest can only be opened once.
When players carry out these actions, they will not be shown them a second time. This gives us two
different classes of options, dynamic options and static options.
We will be storing the dynamic options in a vector . This would allow us to have as many dynamic
options as necessary and allow us to add new options as the game progresses. Our static options
will be stored in a map . The map allows the Option pointers to be associated with a key. The key will
be used to access the Option pointer quickly regardless of how many static options we add.
While we're looking at the Room class you can also see that the JoiningRooms array has been updated
to an STL array , as shown in Listing 19-2.
Listing 19-2. Using STL Containers in the Room Class
class Room
: public Entity
{
public:
enum class JoiningDirections
{
North = 0,
East,
South,
West,
Max
};
 
Search WWH ::




Custom Search