Game Development Reference
In-Depth Information
{
unsigned int dynamicIndex = playerInput - 1;
option = m_dynamicOptions[dynamicIndex];
m_dynamicOptions.erase(m_dynamicOptions.begin() + dynamicIndex);
}
else
{
option = m_staticOptions.at(playerInput - numDynamicOptions);
}
return option;
}
The player has been presented options that could include dynamic and static options. The static options
have been offset by the number of dynamic options, so EvaluateInput must account for this offsetting.
It does this by determining if the entered number is less than the number of dynamic options. If it is then
the chosen options can be used as an index into the m_dynamicOptions vector after subtracting 1.
Note We must subtract 1 because the vector indexes begin at 0; however, we have shown the player
choices beginning at 1, because not many nonprogrammers like to begin counting from 0.
If the player's input was greater than the number of dynamic options, we can calculate the key for
the map of static options by subtracting the number of dynamic options from the player's input. This
will work when there are no dynamic options, as the subtraction will remove 0 from the player input
and we do not need to subtract 1 from the key as we initialized the map's keys beginning at 1.
The most important part of this method to pay attention to is the line that calls erase on
m_dynamicOptions . This is the line that makes these options dynamic, as it ensures that each
dynamic option can only be called once and it means that the options presented to the player after
the option has been executed will be different. This is less than ideal from a usability perspective,
as the user will be presented different numbers to choose the same actions; however, I felt it was a
useful exercise in showing how a vector can be used for dynamic data sets.
Adding Gameplay to Text Adventure
Now that we have a mechanism in place to add dynamic and static options to Room instances, it's time
to look at a couple of gameplay elements. The first gameplay element will be the ability to open chests
and retrieve their contents. To do this we will need a Chest class. Listing 19-6 shows the class definition.
Listing 19-6. The Chest Class
class Chest
: public Entity
{
private:
const Item* m_item;
bool m_isOpen{ false };
 
 
Search WWH ::




Custom Search