00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include "asGameState.h"
00018
00019 #ifdef __COMPILE_WITH_ANGELSCRIPT__
00020
00021 #include "../ScriptHelper.h"
00022
00023 #include "../../core/GameState.h"
00024
00025
00026
00027
00028 ScriptedGameState::ScriptedGameState(asIScriptObject *object)
00029 : 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 Entity@ getBaseEntity()");
00051
00052 mContext->Prepare(id);
00053 mContext->SetObject(mObject);
00054 mContext->Execute();
00055 mBaseEntity = static_cast<Entity*>(mContext->GetReturnObject());
00056 }
00057
00058
00059 ScriptedGameState::~ScriptedGameState()
00060 {
00061 mContext->Release();
00062 mObject->Release();
00063 }
00064
00065
00066 void ScriptedGameState::init()
00067 {
00068 int id = mObject->GetObjectType()->GetMethodIdByDecl("void init()");
00069
00070 mContext->Prepare(id);
00071 mContext->SetObject(mObject);
00072 mContext->Execute();
00073 }
00074
00075
00076 void ScriptedGameState::clear()
00077 {
00078 int id = mObject->GetObjectType()->GetMethodIdByDecl("void clear()");
00079
00080 mContext->Prepare(id);
00081 mContext->SetObject(mObject);
00082 mContext->Execute();
00083 }
00084
00085
00086 void ScriptedGameState::update(f32 deltaTime)
00087 {
00088 int id = mObject->GetObjectType()->GetMethodIdByDecl("void update(f32 deltaTime)");
00089
00090 mContext->Prepare(id);
00091 mContext->SetObject(mObject);
00092 mContext->SetArgDWord(0, (asDWORD)deltaTime);
00093 mContext->Execute();
00094 }
00095
00096
00097 void ScriptedGameState::render()
00098 {
00099 int id = mObject->GetObjectType()->GetMethodIdByDecl("void render()");
00100
00101 mContext->Prepare(id);
00102 mContext->SetObject(mObject);
00103 mContext->Execute();
00104 }
00105
00107 GameState* createGameState()
00108 {
00109 return new GameState();
00110 }
00111
00113 void bindGameState(asIScriptEngine *engine)
00114 {
00115
00116 int r;
00117
00118
00119 r = engine->RegisterObjectType("GameState", sizeof(GameState), asOBJ_REF); assert(r >= 0);
00120
00121
00122 r = engine->RegisterObjectBehaviour("GameState", asBEHAVE_FACTORY, "GameState@ f()",
00123 asFUNCTION(createGameState), asCALL_CDECL); assert(r >= 0);
00124 r = engine->RegisterObjectBehaviour("GameState", asBEHAVE_ADDREF, "void f()",
00125 asMETHOD(GameState, grab), asCALL_THISCALL); assert(r >= 0);
00126 r = engine->RegisterObjectBehaviour("GameState", asBEHAVE_RELEASE, "void f()",
00127 asMETHOD(GameState, drop), asCALL_THISCALL); assert(r >= 0);
00128
00129 r = engine->RegisterObjectMethod("GameState", "GameState& opAssign(const GameState &in)",
00130 asFUNCTION(assignT<GameState>), asCALL_CDECL_OBJFIRST); assert(r >= 0);
00131
00132
00133 r = engine->RegisterObjectMethod("GameState", "void init()",
00134 asMETHOD(GameState, init), asCALL_THISCALL); assert(r >= 0);
00135 r = engine->RegisterObjectMethod("GameState", "void clear()",
00136 asMETHOD(DataStack, clear), asCALL_THISCALL); assert(r >= 0);
00137
00138 r = engine->RegisterObjectMethod("GameState", "void update(f32)",
00139 asMETHOD(GameState, update), asCALL_THISCALL); assert(r >= 0);
00140 r = engine->RegisterObjectMethod("GameState", "void render()",
00141 asMETHOD(GameState, render), asCALL_THISCALL); assert(r >= 0);
00142
00143 r = engine->RegisterObjectMethod("GameState", "u32 getID()",
00144 asMETHOD(GameState, getID), asCALL_THISCALL); assert(r >= 0);
00145 r = engine->RegisterObjectMethod("GameState", "Entity@ getBaseEntity()",
00146 asMETHOD(GameState, getBaseEntity), asCALL_THISCALL); assert(r >= 0);
00147
00148
00149 r = engine->RegisterInterface("IGameState"); assert(r >= 0);
00150
00151 r = engine->RegisterInterfaceMethod("IGameState", "void init()");
00152 r = engine->RegisterInterfaceMethod("IGameState", "void clear()");
00153 r = engine->RegisterInterfaceMethod("IGameState", "void update(f32)");
00154 r = engine->RegisterInterfaceMethod("IGameState", "void render()");
00155 r = engine->RegisterInterfaceMethod("IGameState", "u32 getID()");
00156 r = engine->RegisterInterfaceMethod("IGameState", "const Entity@ getBaseEntity()");
00157
00158
00159 std::string decl = "class CGameState : IGameState, IHasSlots\n"
00160 "{\n"
00161 " GameState @_inner;\n"
00162 " Entity @mBaseEntity;\n"
00163 "\n"
00164 " CGameState()\n"
00165 " {\n"
00166 " @_inner = GameState();\n"
00167 " @mBaseEntity = _inner.getBaseEntity();\n"
00168 " }\n"
00169 "\n"
00170 " void init() {}\n"
00171 " void clear() {}\n"
00172 " void update(f32 deltaTime) {}\n"
00173 " void render() {}\n"
00174 "\n"
00175 " u32 getID() { return getID(); }\n"
00176 " const Entity@ getBaseEntity() { return mBaseEntity; }\n"
00177 "}\n";
00178
00179 GameManager::Instance()->getScriptManager()->addScriptSection("CGameState", decl);
00180 }
00181
00182 #endif // __COMPILE_WITH_ANGELSCRIPT__
00183
00184