Game Development Reference
In-Depth Information
Listing 26-6.
WindowsEnemy
,
AndroidEnemy
, and
iOSEnemy
class WindowsEnemy
: public EnemyBase
{
public:
WindowsEnemy(EnemyType type, const uint32_t serializableId)
: EnemyBase(type, serializableId)
{
std::cout << "Created Windows Enemy!" << std::endl;
}
};
class AndroidEnemy
: public EnemyBase
{
public:
AndroidEnemy(EnemyType type, const uint32_t serializableId)
: EnemyBase( type , serializableId )
{
std::cout << "Created Android Enemy!" << std::endl;
}
};
class iOSEnemy
: public EnemyBase
{
public:
iOSEnemy(EnemyType type, const uint32_t serializableId)
: EnemyBase(type, serializableId)
{
std::cout << "Created iOS Enemy!" << std::endl;
}
};
These three classes rely on polymorphism to allow the rest of the program to work with the
EnemyBase
class rather than the platform-specific implementations. The last problem to solve is how
to create these classes. Fortunately the
Factory
pattern gives us a ready-made solution. Listing 26-7
updates
EnemyFactory
to create the correct type of
EnemyBase
for our implementation.
Listing 26-7. Updating
EnemyFactory
with Platform-Specific Types
namespace
{
#if PLATFORM_WINDOWS
#include "WindowsEnemy.h"
using Enemy = WindowsEnemy;
#elif PLATFORM_ANDROID
#include "AndroidEnemy.h"
using Enemy = AndroidEnemy;