Game Development Reference
In-Depth Information
protected:
ActorComponentCreatorMap m_actorComponentCreators;
public:
ActorFactory(void);
StrongActorPtr CreateActor(const char* actorResource);
protected:
virtual StrongActorComponentPtr CreateComponent(TiXmlElement* pData);
private:
ActorId GetNextActorId(void) { ++m_lastActorId; return m_lastActorId; }
};
The
typedef
at the very top defines the function pointer signature for instantiating
component objects. These functions are stored in the
m_actorComponentCrea-
tors
map, which is keyed by the string name of the component. This string comes
from the XML.
Everything starts with the
CreateActor()
function, which is the only public
method.
StrongActorPtr ActorFactory::CreateActor(const char* actorResource)
{
// Grab the root XML node
TiXmlElement* pRoot =
XmlResourceLoader::LoadAndReturnRootXmlElement(actorResource);
if (!pRoot)
{
GCC_ERROR(
“
Failed to create actor from resource:
”
+
std::string(actorResource));
return StrongActorPtr();
}
// create the actor instance
StrongActorPtr pActor(GCC_NEW Actor(GetNextActorId()));
if (!pActor->Init(pRoot))
{
GCC_ERROR(
“
Failed to initialize actor:
”
+ std::string(actorResource));
return StrongActorPtr();
}
// Loop through each child element and load the component
for (TiXmlElement* pNode = pRoot->FirstChildElement(); pNode;
pNode = pNode->NextSiblingElement())