Creating Objects with the Prototype Pattern

We saw in the last chapter how using a Dynamic Factory can help us decouple our high-level modules, such as the M5StageManager or M5ObjectManager from the implementation details of our derived M5Stage or M5Component classes. We did this by pushing those dependencies into derived builder classes that would be used by a Dynamic Factory. This allowed us the freedom to change our derived stage and component classes without needing to modify our higher level modules. C++ template classes made using the Dynamic Factory very easy, since we were not required to create a derived class builder for every stage and component.

However, we are required to create a builder for each M5Object type we want, since they will contain a set of components that are unique to each object. Unfortunately, these builders may require frequent changes as we playtest, balance, and modify our game design. Each time these builders change, the game will be need to be recompiled.

The goal would be to have our game object types be completely defined in a file. This would give the game designer the ability to test, tweak, and balance the game without needing to touch C++ code or ever recompile. Of course, all of this could be done in a level editor tool, which could also be given to players, allowing them to create additional game content.

In this chapter, we will explore the Prototype pattern and how it can help us define objects completely within a text file. We will do this by first looking at a simple example of the pattern, then looking at how the Mach5 Engine, and the M5ObjectManager specifically, makes use of this pattern to read game object definitions from a file. Along the way, we will look at some language features of C++ that will help us write better, safer code.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.129.210.102