-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameObjectFactory.cpp
48 lines (37 loc) · 1.08 KB
/
GameObjectFactory.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include "GameObjectFactory.h"
//allocating & initializing InputHandler pointer.
GameObjectFactory* GameObjectFactory::s_pInstance = 0;
bool GameObjectFactory::registerType(std::string typeId, BaseCreator* pCreator)
{
std::map<std::string, BaseCreator*>::iterator it = m_creators.find(typeId);
//if type already registered, do nothing.
if(it != m_creators.end())
{
delete pCreator;
std::cout << "GameObjectFactory::registerType(): " << typeId << " is already registered" << std::endl;
return false;
}
m_creators[typeId] = pCreator;
return true;
}
GameObject* GameObjectFactory::create(std::string typeId)
{
std::map<std::string, BaseCreator*>::iterator it = m_creators.find(typeId);
if(it == m_creators.end())
{
std::cout << "Could not find type: " << typeId <<std::endl;
return NULL;
}
// BaseCreator* pCreator = (*it).second;
BaseCreator* pCreator = it->second;
return pCreator->createGameObject();
}
GameObjectFactory* GameObjectFactory::instance()
{
if(s_pInstance == 0)
{
s_pInstance = new GameObjectFactory();
return s_pInstance;
}
return s_pInstance;
}