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 mID--;
00033 mIDCount--;
00034
00035
00036 mObject->AddRef();
00037 mContext = GameManager::Instance()->getScriptManager()->getEngine()->CreateContext();
00038
00039
00040 int id = mObject->GetObjectType()->GetMethodIdByDecl("const Entity@ getBaseEntity()");
00041
00042 mContext->Prepare(id);
00043 mContext->SetObject(mObject);
00044 mContext->Execute();
00045 mBaseEntity = static_cast<Entity*>(mContext->GetReturnObject());
00046 }
00047
00048
00049 ScriptedGameState::~ScriptedGameState()
00050 {
00051 mContext->Release();
00052 mObject->Release();
00053 }
00054
00055
00056 void ScriptedGameState::init()
00057 {
00058 int id = mObject->GetObjectType()->GetMethodIdByDecl("void init()");
00059
00060 mContext->Prepare(id);
00061 mContext->SetObject(mObject);
00062 mContext->Execute();
00063 }
00064
00065
00066 void ScriptedGameState::clear()
00067 {
00068 int id = mObject->GetObjectType()->GetMethodIdByDecl("void clear()");
00069
00070 mContext->Prepare(id);
00071 mContext->SetObject(mObject);
00072 mContext->Execute();
00073 }
00074
00075
00076 void ScriptedGameState::update(f32 deltaTime)
00077 {
00078 int id = mObject->GetObjectType()->GetMethodIdByDecl("void init()");
00079
00080 mContext->Prepare(id);
00081 mContext->SetObject(mObject);
00082 mContext->SetArgDWord(0, (asDWORD)deltaTime);
00083 mContext->Execute();
00084 }
00085
00086
00087 void ScriptedGameState::render()
00088 {
00089 int id = mObject->GetObjectType()->GetMethodIdByDecl("void render()");
00090
00091 mContext->Prepare(id);
00092 mContext->SetObject(mObject);
00093 mContext->Execute();
00094 }
00095
00097 GameState* createGameState()
00098 {
00099 return new GameState();
00100 }
00101
00103 void bindGameState(asIScriptEngine *engine)
00104 {
00105
00106 int r;
00107
00108
00109 r = engine->RegisterObjectType("GameState", sizeof(GameState), asOBJ_REF); assert(r >= 0);
00110
00111
00112 r = engine->RegisterObjectBehaviour("GameState", asBEHAVE_FACTORY, "GameState@ f()",
00113 asFUNCTION(createGameState), asCALL_CDECL); assert(r >= 0);
00114 r = engine->RegisterObjectBehaviour("GameState", asBEHAVE_ADDREF, "void f()",
00115 asMETHOD(GameState, grab), asCALL_THISCALL); assert(r >= 0);
00116 r = engine->RegisterObjectBehaviour("GameState", asBEHAVE_RELEASE, "void f()",
00117 asMETHOD(GameState, drop), asCALL_THISCALL); assert(r >= 0);
00118
00119 r = engine->RegisterObjectMethod("GameState", "GameState& opAssign(const GameState &in)",
00120 asFUNCTION(assignT<GameState>), asCALL_CDECL_OBJFIRST); assert(r >= 0);
00121
00122
00123 r = engine->RegisterObjectMethod("GameState", "void init()",
00124 asMETHOD(GameState, init), asCALL_THISCALL); assert(r >= 0);
00125 r = engine->RegisterObjectMethod("GameState", "void clear()",
00126 asMETHOD(DataStack, clear), asCALL_THISCALL); assert(r >= 0);
00127
00128 r = engine->RegisterObjectMethod("GameState", "void update()",
00129 asMETHOD(GameState, update), asCALL_THISCALL); assert(r >= 0);
00130 r = engine->RegisterObjectMethod("GameState", "void render()",
00131 asMETHOD(GameState, render), asCALL_THISCALL); assert(r >= 0);
00132
00133 r = engine->RegisterObjectMethod("GameState", "const Entity@ getBaseEntity()",
00134 asMETHOD(GameState, getBaseEntity), asCALL_THISCALL); assert(r >= 0);
00135
00136
00137 r = engine->RegisterInterface("IGameState"); assert(r >= 0);
00138
00139 r = engine->RegisterInterfaceMethod("IGameState", "void init()");
00140 r = engine->RegisterInterfaceMethod("IGameState", "void clear()");
00141 r = engine->RegisterInterfaceMethod("IGameState", "void update()");
00142 r = engine->RegisterInterfaceMethod("IGameState", "void render()");
00143 r = engine->RegisterInterfaceMethod("IGameState", "const Entity@ getBaseEntity()");
00144
00145
00146 std::string decl = "class CGameState : IGameState\n"
00147 "{\n"
00148 " GameState _inner;\n"
00149 " Entity @mBaseEntity;\n"
00150 "\n"
00151 " CGameState()\n"
00152 " {\n"
00153 " this._inner = GameState();\n"
00154 " this.mBaseEntity = this._inner.getBaseEntity();\n"
00155 " }\n"
00156 "\n"
00157 " void init() {}\n"
00158 " void clear() {}\n"
00159 " void update() {}\n"
00160 " void render() {}\n"
00161 "\n"
00162 " const Entity@ getBaseEntity() { return this._inner.getBaseEntity(); }\n"
00163 "}\n";
00164
00165 GameManager::Instance()->getScriptManager()->addScriptSection("CGameState", decl);
00166 }
00167
00168 #endif // __COMPILE_WITH_ANGELSCRIPT__
00169
00170