Game Development Reference
In-Depth Information
Chapter 19
Using the STL in Text Adventure
So far you have seen how a game can be built using classes, member variables, and C-style arrays.
In this chapter you will see how you can use some of the STL classes covered in this part of the
book to simplify the code you have been creating. This chapter uses polymorphism to store pointers
to base classes in our STL containers. If you need a recap on polymorphism and how it is used, you
should read over Chapter 11.
Some gameplay elements are also added to this chapter. Our Text Adventure game now has an
end state. There are two enemies in the game world and the player must find and kill both of these
enemies. Before the player will be able to attack the enemies, they must be armed with a weapon.
This gameplay logic is relatively simple and there are still only a handful of rooms in the world. This is
to help keep the code size small so you can get a good grasp on how to write a complete game.
Using STL array to Store Room Pointers
When you last saw the code for the Text Adventure game it was using a standard C-style array in
the Game class to store the Room objects. In this chapter we switch over to using an STL array .
Listing 19-1 shows the STL array code.
Listing 19-1. Using an STL array to Store Rooms
class Game
{
private:
static const unsigned int m_numberOfRooms = 4;
using Rooms = std::array<Room, m_numberOfRooms>;
Rooms m_rooms;
};
185
 
Search WWH ::




Custom Search