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 ~GameObject() {} };
Each of our derived classes will now need to implement this load
function. The SDLGameObject
class will now look like this:
SDLGameObject::SDLGameObject() : GameObject() { } voidSDLGameObject::load(const LoaderParams *pParams) { m_position = Vector2D(pParams->getX(),pParams->getY()); m_velocity = Vector2D(0,0); m_acceleration = Vector2D(0,0); m_width = pParams->getWidth(); m_height = pParams->getHeight(); m_textureID = pParams->getTextureID(); m_currentRow = 1; m_currentFrame = 1; m_numFrames = pParams->getNumFrames(); }
Our objects that derive from SDLGameObject
can use this load
function as well; for example, here is the Player::load
function:
Player::Player() : SDLGameObject() { } void Player::load(const LoaderParams *pParams) { SDLGameObject::load(pParams); }
This may seem a bit pointless but it actually saves us having to pass through LoaderParams
everywhere. Without it, we would need to pass LoaderParams
through the factory's create
function which would then in turn pass it through to the Creator
object. We have eliminated the need for this by having a specific function that handles parsing our loading values. This will make more sense once we start parsing our states from a file.
We have another issue which needs rectifying; we have two classes with extra parameters in their constructors (MenuButton
and AnimatedGraphic
). Both classes take an extra parameter as well as LoaderParams
. To combat this we will add these values to LoaderParams
and give them default values.
LoaderParams(int x, int y, int width, int height, std::string textureID, int numFrames, int callbackID = 0, int animSpeed = 0) : m_x(x), m_y(y), m_width(width), m_height(height), m_textureID(textureID), m_numFrames(numFrames), m_callbackID(callbackID), m_animSpeed(animSpeed) { }
In other words, if the parameter is not passed in, then the default values will be used (0 in both cases). Rather than passing in a function pointer as MenuButton
did, we are using callbackID
to decide which callback function to use within a state. We can now start using our factory and parsing our states from an XML file.
18.224.54.255