Behaviours can now be serialized in from the scene graph xml

This commit is contained in:
2014-08-24 22:18:04 -07:00
parent 186c72334c
commit a3a6e4af78
2 changed files with 29 additions and 1 deletions

View File

@@ -9,6 +9,8 @@
#include "KRBehavior.h"
#include "KRNode.h"
KRBehaviorFactoryFunctionMap m_factoryFunctions;
KRBehavior::KRBehavior()
{
__node = NULL;
@@ -42,5 +44,23 @@ KRBehavior *KRBehavior::LoadXML(KRNode *node, tinyxml2::XMLElement *e)
attributes[attribute->Name()] = attribute->Value();
}
return NULL;
const char *szElementName = e->Attribute("type");
if(szElementName == NULL) {
return NULL;
}
KRBehaviorFactoryFunctionMap::const_iterator itr = m_factoryFunctions.find(szElementName);
if(itr == m_factoryFunctions.end()) {
return NULL;
}
return (*itr->second)(attributes);
}
void KRBehavior::RegisterFactoryCTOR(std::string behaviorName, KRBehaviorFactoryFunction fnFactory)
{
m_factoryFunctions[behaviorName] = fnFactory;
}
void KRBehavior::UnregisterFactoryCTOR(std::string behaviorName)
{
m_factoryFunctions.erase(behaviorName);
}

View File

@@ -10,6 +10,7 @@
#define KRBEHAVIOR_H
#include "tinyxml2.h"
#include <map>
/*
@@ -17,11 +18,18 @@
*/
class KRBehavior;
class KRNode;
typedef KRBehavior *(*KRBehaviorFactoryFunction)(std::map<std::string, std::string> attributes);
typedef std::map<std::string, KRBehaviorFactoryFunction> KRBehaviorFactoryFunctionMap;
class KRBehavior
{
public:
static void RegisterFactoryCTOR(std::string behaviorName, KRBehaviorFactoryFunction fnFactory);
static void UnregisterFactoryCTOR(std::string behaviorName);
KRBehavior();
virtual ~KRBehavior();
KRNode *getNode() const;