00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "GameState.h"
00019
00020
00021
00022
00023 u32 GameState::mIDCount = 0;
00024
00025
00026 GameState::GameState()
00027 {
00028 mID = mIDCount++;
00029
00030
00031 std::stringstream ss;
00032 ss << "GameState#" << mID;
00033 mBaseEntity = GameManager::Instance()->getEntityManager()->createEntity(ss.str());
00034
00035
00036 mEventGroup = "ent#" + ss.str();
00037 }
00038
00039
00040 GameState::~GameState()
00041 {
00042 GameManager::Instance()->getEntityManager()->removeEntity(mBaseEntity);
00043 }
00044
00045
00046
00047 void GameState::init()
00048 {
00049 }
00050
00051
00052
00053 void GameState::clear()
00054 {
00055 }
00056
00057
00058
00059 void GameState::update(f32 deltaTime)
00060 {
00061 }
00062
00063
00064
00065 void GameState::render()
00066 {
00067 }
00068
00069
00070
00071 GameState* GameState::refFactory()
00072 {
00073 return new GameState();
00074 }
00075
00076
00077 void GameState::refAdd()
00078 {
00079 mRefCount++;
00080 }
00081
00082
00083 void GameState::refRelease()
00084 {
00085 if(--mRefCount == 0)
00086 delete this;
00087 }
00088
00089
00090 const Entity* GameState::getBaseEntity() const
00091 {
00092 return mBaseEntity;
00093 }
00094
00095
00096
00097 void GameState::onUpdate(f32 deltaTime)
00098 {
00099 GameManager::Instance()->getEventManager()->emitEvent(mEventGroup, "onUpdate", &deltaTime);
00100 }
00101
00102
00103 void GameState::onRender()
00104 {
00105 GameManager::Instance()->getEventManager()->emitEvent(mEventGroup, "onRender");
00106 }
00107
00108
00109 void GameState::onPause()
00110 {
00111 GameManager::Instance()->getEventManager()->emitEvent(mEventGroup, "onPause");
00112 }
00113
00114
00115 void GameState::onUnPause()
00116 {
00117 GameManager::Instance()->getEventManager()->emitEvent(mEventGroup, "onUnPause");
00118 }
00119
00120
00121
00122
00123 ScriptedGameState::ScriptedGameState(asIScriptObject *object)
00124 : mObject(object)
00125 {
00126
00127 mID--;
00128 mIDCount--;
00129
00130
00131 mObject->AddRef();
00132 mContext = GameManager::Instance()->getScriptManager()->getEngine()->CreateContext();
00133
00134
00135 int id = mObject->GetObjectType()->GetMethodIdByDecl("const Entity@ getBaseEntity()");
00136
00137 mContext->Prepare(id);
00138 mContext->SetObject(mObject);
00139 mContext->Execute();
00140 mBaseEntity = static_cast<Entity*>(mContext->GetReturnObject());
00141 }
00142
00143
00144 ScriptedGameState::~ScriptedGameState()
00145 {
00146 mContext->Release();
00147 mObject->Release();
00148 }
00149
00150
00151 void ScriptedGameState::init()
00152 {
00153 int id = mObject->GetObjectType()->GetMethodIdByDecl("void init()");
00154
00155 mContext->Prepare(id);
00156 mContext->SetObject(mObject);
00157 mContext->Execute();
00158 }
00159
00160
00161 void ScriptedGameState::clear()
00162 {
00163 int id = mObject->GetObjectType()->GetMethodIdByDecl("void clear()");
00164
00165 mContext->Prepare(id);
00166 mContext->SetObject(mObject);
00167 mContext->Execute();
00168 }
00169
00170
00171 void ScriptedGameState::update(f32 deltaTime)
00172 {
00173 int id = mObject->GetObjectType()->GetMethodIdByDecl("void init()");
00174
00175 mContext->Prepare(id);
00176 mContext->SetObject(mObject);
00177 mContext->SetArgDWord(0, deltaTime);
00178 mContext->Execute();
00179 }
00180
00181
00182 void ScriptedGameState::render()
00183 {
00184 int id = mObject->GetObjectType()->GetMethodIdByDecl("void render()");
00185
00186 mContext->Prepare(id);
00187 mContext->SetObject(mObject);
00188 mContext->Execute();
00189 }
00190
00191
00192
00193 void bindGameState(asIScriptEngine *engine)
00194 {
00195
00196 int r;
00197
00198
00199 r = engine->RegisterObjectType("GameState", sizeof(GameState), asOBJ_REF); assert(r >= 0);
00200
00201
00202 r = engine->RegisterObjectBehaviour("GameState", asBEHAVE_FACTORY, "GameState@ f()",
00203 asFUNCTION(GameState::refFactory), asCALL_CDECL); assert(r >= 0);
00204 r = engine->RegisterObjectBehaviour("GameState", asBEHAVE_ADDREF, "void f()",
00205 asMETHOD(GameState, refAdd), asCALL_THISCALL); assert(r >= 0);
00206 r = engine->RegisterObjectBehaviour("GameState", asBEHAVE_RELEASE, "void f()",
00207 asMETHOD(GameState, refRelease), asCALL_THISCALL); assert(r >= 0);
00208
00209
00210 r = engine->RegisterObjectMethod("GameState", "void init()",
00211 asMETHOD(GameState, init), asCALL_THISCALL); assert(r >= 0);
00212 r = engine->RegisterObjectMethod("GameState", "void clear()",
00213 asMETHOD(DataStack, clear), asCALL_THISCALL); assert(r >= 0);
00214
00215 r = engine->RegisterObjectMethod("GameState", "void update()",
00216 asMETHOD(GameState, update), asCALL_THISCALL); assert(r >= 0);
00217 r = engine->RegisterObjectMethod("GameState", "void render()",
00218 asMETHOD(GameState, render), asCALL_THISCALL); assert(r >= 0);
00219
00220 r = engine->RegisterObjectMethod("GameState", "const Entity@ getBaseEntity()",
00221 asMETHOD(GameState, getBaseEntity), asCALL_THISCALL); assert(r >= 0);
00222
00223
00224 r = engine->RegisterInterface("IGameState"); assert(r >= 0);
00225
00226 r = engine->RegisterInterfaceMethod("IGameState", "void init()");
00227 r = engine->RegisterInterfaceMethod("IGameState", "void clear()");
00228 r = engine->RegisterInterfaceMethod("IGameState", "void update()");
00229 r = engine->RegisterInterfaceMethod("IGameState", "void render()");
00230 r = engine->RegisterInterfaceMethod("IGameState", "const Entity@ getBaseEntity()");
00231
00232
00233 std::string decl = "CGameState : IGameState\n"
00234 "{\n"
00235 " GameState _inner;\n"
00236 " Entity @mBaseEntity\n;"
00237 "\n"
00238 " CGameState::CGameState()\n"
00239 " {\n"
00240 " _inner = GameState();\n"
00241 " mBaseEntity = _inner.getBaseEntity();\n"
00242 " }\n"
00243 "\n"
00244 " void init() {}\n"
00245 " void clear() {}\n"
00246 " void update() {}\n"
00247 " void render() {}\n"
00248 "\n"
00249 " const Entity@ getBaseEntity() { return _inner.getBaseEntity(); }\n"
00250 "}\n";
00251
00252 GameManager::Instance()->getScriptManager()->addScriptSection("CGameState", decl);
00253 }
00254