Game Development Reference
In-Depth Information
cout << "There is no room to the "
<< strDirection << endl << endl;
}
}
}
The UpdateOnOption method can be broken down into three main sections. First it works out which
direction the player would like to move in from the supplied option. It does this using the first switch
statement in the method. It then updates the current Room pointer stored by m_player so long as the
Room we are trying to move to is valid and not a nullptr . If the chosen Room is a nullptr , the method
then outputs to the player that there is no room in the direction in which he or she tried to move.
You now have the ability to move through the game world. Congratulations! We could stop learning
about C++ at this point and have the ability to write a full-text adventure game with everything you
have learned so far; however, your code would be very long and you would find yourself repeating
certain code over and over again. The rest of this topic will try to expand your knowledge of C++
and show you how some more advanced features of the language can be used to cut down on the
amount of code you have to write.
Summary
This chapter has introduced you to the concept of class inheritance. The inheritance you've seen in
this chapter is useful for sharing behavior and code between objects that can be logically grouped
together. Vehicles are a classic example used to introduce new programmers to inheritance, as it is
a simple set of objects to group logically and they have simple features that can be used to show
differences between different members of the set.
You've seen in this chapter that the access specifier ( public , protected , and private ) used when
declaring class member variables has an important part to play in sharing data between base
classes and their derived children. You've also learned that constructors and destructors play an
important role in the creation and destruction of objects and in which order they are called. Finally
you learned that it's possible to alter the behavior of a derived class by overriding methods that exist
in a parent class by defining a method with the same signature in the derived class.
The following chapter is going to introduce you to the concept of polymorphism. Polymorphism
allows us to create class interfaces and abstract out class implementations. This is a very powerful
technique that you will find used in game play, engine, and cross-platform game programming.
The code you have seen in this chapter for the Text Adventure game will be rewritten once you
understand the concepts behind polymorphism and you will see how much simpler and clean-looking
your code can be.
 
Search WWH ::




Custom Search