NLS Engine  v0.1
The Next Logical Step in game engine design.
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Defines
EntityFactory.h
Go to the documentation of this file.
00001 #pragma once
00002 
00016 // Standard Includes
00017 #include <map>
00018 #include <string>
00019 #include <assert.h>
00020 
00021 // Library Includes
00022 #include <boost/any.hpp>
00023 #include <d3dx9math.h>
00024 #include <angelscript/scriptany/scriptany.h>
00025 
00026 // Local Includes
00027 #include "../sharedbase/Envelope.h"
00028 #include "../sharedbase/Entity.h"
00029 #include "../sharedbase/ScriptObjectInterface.h"
00030 
00031 // *HACK: This is indicating that we need to re-think how modules register their information with the script engine.
00032 #include "../GraphicsDLL/component/RenderableComponent.h"
00033 
00034 // Forward Declarations
00035 
00036 // Typedefs
00037 
00038 class CreateEntityFactory : public ScriptObjectInterface {
00039 public:
00040   CreateEntityFactory (void) : ScriptObjectInterface() {
00041     this->entity.reset();
00042   }
00043   ~CreateEntityFactory (void) {
00044   }
00045 
00046   void SetName(std::string name) {
00047     LOG(LOG_PRIORITY::INFO, "This method is depricated and does nothing.");
00048   }
00049   void AddAttribute(std::string componentType, std::string key, std::string value) {
00050     this->components[componentType][key] = value;
00051   }
00052 
00053   void AddAttribute(std::string componentType, std::string key, CScriptAny* value) {
00054     std::string type(engine->GetTypeDeclaration(value->GetTypeId()));
00055     boost::any val;
00056     
00057     if (type == "bool") {
00058       bool myvalue;
00059       value->Retrieve(&myvalue, value->GetTypeId());
00060       val = myvalue;
00061     }
00062     else if (type == "int64") {
00063       long long myvalue;
00064       value->Retrieve(&myvalue, value->GetTypeId());
00065       val = myvalue;
00066     }
00067     else if (type.compare(0, 3, "int") == 0) {
00068       int myvalue;
00069       value->Retrieve(&myvalue, value->GetTypeId());
00070       val = myvalue;
00071     }
00072     else if (type == "uint64") {
00073       unsigned long long myvalue;
00074       value->Retrieve(&myvalue, value->GetTypeId());
00075       val = myvalue;
00076     }
00077     else if (type.compare(0, 4, "uint") == 0) {
00078       unsigned int myvalue;
00079       value->Retrieve(&myvalue, value->GetTypeId());
00080       val = myvalue;
00081     }
00082     else if (type == "float") {
00083       float myvalue;
00084       value->Retrieve(&myvalue, value->GetTypeId());
00085       val = myvalue;
00086     }
00087     else if (type == "double") {
00088       double myvalue;
00089       value->Retrieve(&myvalue, value->GetTypeId());
00090       val = myvalue;
00091     }
00092     else if (type == "string") {
00093       std::string myvalue;
00094       value->Retrieve(&myvalue, value->GetTypeId());
00095       val = myvalue;
00096     }
00097     else if (type == "Rotation") {
00098       D3DXQUATERNION myvalue;
00099       value->Retrieve(&myvalue, value->GetTypeId());
00100       val = myvalue;
00101     }
00102     else if (type == "Vector") {
00103       D3DXVECTOR3 myvalue;
00104       value->Retrieve(&myvalue, value->GetTypeId());
00105       val = myvalue;
00106     }
00107     else if (type == "Color") {
00108       D3DXCOLOR myvalue;
00109       value->Retrieve(&myvalue, value->GetTypeId());
00110       val = myvalue;
00111     }
00112     else if (type == "Entity") {
00113       EntitySPTR myvalue;
00114       value->Retrieve(&myvalue, value->GetTypeId());
00115       EntitySPTR ent(myvalue);
00116       val = ent;
00117     }
00118     else if (type == "Envelope") {
00119       Envelope* myvalue;
00120       value->Retrieve(&myvalue, value->GetTypeId());
00121       val = myvalue;
00122     }
00123     else if (type == "EnvelopeSPTR") {
00124       EnvelopeSPTR myvalue;
00125       value->Retrieve(&myvalue, value->GetTypeId());
00126       val = myvalue;
00127     }
00128     else if (type == "ProjectionStyle") {
00129       GraphicsCore::ECoordinateSpace myvalue;
00130       value->Retrieve(&myvalue, value->GetTypeId());
00131       val = myvalue;
00132     }
00133     else {
00134       assert(/*unknown type*/false);
00135     }
00136     
00137     this->components[componentType][key] = val;
00138   }
00139 
00140   void SetEntity(EntitySPTR entity) {
00141     this->entity = entity;
00142   }
00143   void CreateEntity(int id = 0) {
00144     this->entity = Entity::Factory("", id);
00145   }
00146   void Finalize(EnvelopeSPTR envelope) {
00147     if (this->entity.get() != nullptr) {
00148       EntitySPTR ent(this->entity);
00149       envelope->AddData(ent->GetName());
00150       envelope->AddData(ent);
00151       envelope->AddData(this->components);
00152     }
00153     else {
00154       LOG(LOG_PRIORITY::CONFIG, "Message being finalized with a null Entity!  Aborting...");
00155       assert(/* Missing Entity */this->entity.get() != nullptr);
00156     }
00157   }
00158   
00159   static asIScriptEngine* engine;
00160 private:
00161   EntitySPTR entity;
00162   std::map< std::string, std::map<std::string, boost::any> > components;
00163 };