Fitting the factory into the framework
With our factory now in place, we can start altering our GameObject
classes to use it. Our first step is to ensure that we have a
Creator
class for each of our objects. Here is one for Player
:
class PlayerCreator : public BaseCreator { GameObject* createGameObject() const { return new Player(); } };
This can be added to the bottom of the Player.h
file. Any object we want the factory to create must have its own Creator
implementation. Another addition we must make is to move LoaderParams
from the constructor to their own function called load
. This stops the need for us to pass the LoaderParams
object to the factory itself. We will put the load
function into the GameObject
base class, as we want every object to have one.
class GameObject { public: virtual void draw()=0; virtual void update()=0; virtual void clean()=0; // new load function virtual void load(const LoaderParams* pParams)=0; protected: GameObject() {} virtual...