00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include "asEntityComponent.h"
00018
00019 #ifdef __COMPILE_WITH_ANGELSCRIPT__
00020
00021 #include "../ScriptHelper.h"
00022
00023 #include "../../core/GameManager.h"
00024
00025
00026
00027
00028 ScriptedEntityComponent::ScriptedEntityComponent(asIScriptObject *object)
00029 : EntityComponent(NULL), mObject(object)
00030 {
00031
00032 mIDCount--;
00033
00034
00035 mObject->AddRef();
00036 mContext = GameManager::Instance()->getScriptManager()->getEngine()->CreateContext();
00037
00038
00039 int id;
00040
00041
00042 id = mObject->GetObjectType()->GetMethodIdByDecl("u32 getID()");
00043
00044 mContext->Prepare(id);
00045 mContext->SetObject(mObject);
00046 mContext->Execute();
00047 mID = mContext->GetReturnDWord();
00048
00049
00050 id = mObject->GetObjectType()->GetMethodIdByDecl("const string getName()");
00051
00052 mContext->Prepare(id);
00053 mContext->SetObject(mObject);
00054 mContext->Execute();
00055 mName = *reinterpret_cast<std::string*>(mContext->GetReturnObject());
00056
00057
00058 id = mObject->GetObjectType()->GetMethodIdByDecl("Entity@ getParent()");
00059
00060 mContext->Prepare(id);
00061 mContext->SetObject(mObject);
00062 mContext->Execute();
00063 Entity *parent = reinterpret_cast<Entity*>(mContext->GetReturnObject());
00064
00065 if(parent != NULL)
00066 {
00067 if(parent->addComponent(this))
00068 {
00069 pParent = parent;
00070 parent->grab();
00071 }
00072
00073 else pParent = NULL;
00074 }
00075
00076 else pParent = NULL;
00077 }
00078
00079
00080 ScriptedEntityComponent::~ScriptedEntityComponent()
00081 {
00082 mContext->Release();
00083 mObject->Release();
00084 }
00085
00086
00088 EntityComponent* createEntityComponent(Entity* parent)
00089 {
00090 return new EntityComponent(parent);
00091 }
00092
00094 void bindEntityComponent(asIScriptEngine *engine)
00095 {
00096
00097 int r;
00098
00099
00100 r = engine->RegisterObjectType("EntityComponent", sizeof(EntityComponent), asOBJ_REF);
00101 r = engine->RegisterObjectType("Entity", sizeof(Entity), asOBJ_REF);
00102
00103
00104 bindEntityComponentBase<EntityComponent>(engine, "EntityComponent");
00105
00106
00107 r = engine->RegisterObjectBehaviour("EntityComponent", asBEHAVE_FACTORY, "EntityComponent@ f(Entity @+)",
00108 asFUNCTION(createEntityComponent), asCALL_CDECL); assert(r >= 0);
00109
00110 r = engine->RegisterObjectMethod("EntityComponent", "EntityComponent& opAssign(const EntityComponent &in)",
00111 asFUNCTION(assignT<EntityComponent>), asCALL_CDECL_OBJFIRST); assert(r >= 0);
00112
00113
00114 r = engine->RegisterInterface("IEntityComponent");
00115
00116 r = engine->RegisterInterfaceMethod("IEntityComponent", "u32 getID()");
00117 r = engine->RegisterInterfaceMethod("IEntityComponent", "const string getName()");
00118 r = engine->RegisterInterfaceMethod("IEntityComponent", "Entity@ getParent()");
00119
00120
00121 std::string decl = "class CEntityComponent : IEntityComponent, IHasSlots\n"
00122 "{\n"
00123 " EntityComponent @_inner;\n"
00124 " Entity @pParent;\n"
00125 " string mName;\n"
00126 "\n"
00127 " CEntityComponent(Entity @parent)\n"
00128 " {\n"
00129 " @_inner = EntityComponent(null);\n"
00130 "\n"
00131 " @pParent = @parent;\n"
00132 " pParent.addComponent(this);\n"
00133 " }\n"
00134 "\n"
00135 " u32 getID() { return _inner.getID(); }\n"
00136 " const string getName() { return mName; }\n"
00137 " Entity@ getParent() { return pParent; }\n"
00138 "}\n";
00139
00140 GameManager::Instance()->getScriptManager()->addScriptSection("CEntityComponent", decl);
00141 }
00142
00143 #endif // __COMPILE_WITH_ANGELSCRIPT__
00144
00145