00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include "Entity.h"
00018 #include "GameManager.h"
00019
00020
00021
00022
00023 u32 Entity::mIDCount = 0;
00024
00025
00026 Entity::Entity(const std::string &name, Entity *parent)
00027 : mRefCount(1), mName(name), mPosition(0, 0, 0)
00028 {
00029 mID = mIDCount++;
00030
00031
00032 if(parent != NULL)
00033 {
00034 if(parent->addChild(this))
00035 pParent = parent;
00036
00037 else pParent = NULL;
00038 }
00039
00040 else pParent = NULL;
00041
00042
00043 EventManager *pEventMgr = GameManager::Instance()->getEventManager();
00044 std::string groupName;
00045
00046 groupName = "ent#" + mName;
00047 pEventMgr->createEventGroup(groupName);
00048 pEventMgr->createEventSlot(groupName, "onPositionChange");
00049 pEventMgr->createEventSlot(groupName, "onUpdate");
00050 pEventMgr->createEventSlot(groupName, "onRender");
00051 pEventMgr->createEventSlot(groupName, "onPause");
00052 pEventMgr->createEventSlot(groupName, "onUnPause");
00053
00054
00055 if(pParent != NULL)
00056 {
00057 groupName = "ent#" + pParent->getName();
00058 pEventMgr->connectEventSignal(groupName, "onUpdate", this, &Entity::onPositionChange);
00059 pEventMgr->connectEventSignal(groupName, "onUpdate", this, &Entity::onUpdate);
00060 pEventMgr->connectEventSignal(groupName, "onRender", this, &Entity::onRender);
00061 pEventMgr->connectEventSignal(groupName, "onPause", this, &Entity::onPause);
00062 pEventMgr->connectEventSignal(groupName, "onUnPause", this, &Entity::onUnPause);
00063 }
00064 }
00065
00066
00067 Entity::~Entity()
00068 {
00069
00070 removeAllChildren();
00071 removeAllComponents();
00072
00073
00074 GameManager::Instance()->getEventManager()->removeEventGroup(std::string("ent#") + mName);
00075 }
00076
00077
00078 Entity* Entity::refFactory(const std::string &name)
00079 {
00080 return new Entity(name);
00081 }
00082
00083
00084 void Entity::refAdd()
00085 {
00086 mRefCount++;
00087 }
00088
00089
00090 void Entity::refRelease()
00091 {
00092 if(--mRefCount == 0)
00093 {
00094 if(pParent != NULL)
00095 {
00096 if(!pParent->removeChild(this))
00097 {
00098 if(!GameManager::Instance()->getEntityManager()->removeEntity(this))
00099 delete this;
00100 }
00101 }
00102
00103 else if(!GameManager::Instance()->getEntityManager()->removeEntity(this))
00104 delete this;
00105 }
00106 }
00107
00108
00109 u32 Entity::getID() const
00110 {
00111 return mID;
00112 }
00113
00114
00115 const std::string& Entity::getName() const
00116 {
00117 return mName;
00118 }
00119
00120
00121 bool Entity::addChild(Entity *entity)
00122 {
00123
00124 if(entity == NULL)
00125 return false;
00126
00127
00128 if(getChild(entity->getName()))
00129 return false;
00130
00131
00132 mChildren.push_back(entity);
00133
00134 return true;
00135 }
00136
00137
00138 bool Entity::addComponent(EntityComponent *component)
00139 {
00140
00141 if(component == NULL)
00142 return false;
00143
00144
00145 if(getComponent(component->getName()))
00146 return false;
00147
00148
00149 mComponents.push_back(component);
00150
00151 return true;
00152 }
00153
00154
00155 Entity* Entity::getChild(const u32 id)
00156 {
00157 for(u32 i = 0; i < mChildren.size(); i++)
00158 {
00159 if(mChildren[i]->getID() == id)
00160 return mChildren[i];
00161 }
00162
00163 return NULL;
00164 }
00165
00166
00167 Entity* Entity::getChild(const std::string &name)
00168 {
00169 for(u32 i = 0; i < mChildren.size(); i++)
00170 {
00171 if(mChildren[i]->getName() == name)
00172 return mChildren[i];
00173 }
00174
00175 return NULL;
00176 }
00177
00178
00179 EntityComponent* Entity::getComponent(const u32 id)
00180 {
00181 for(u32 i = 0; i < mComponents.size(); i++)
00182 {
00183 if(mComponents[i]->getID() == id)
00184 return mComponents[i];
00185 }
00186
00187 return NULL;
00188 }
00189
00190
00191 EntityComponent* Entity::getComponent(const std::string &name)
00192 {
00193 for(u32 i = 0; i < mComponents.size(); i++)
00194 {
00195 if(mComponents[i]->getName() == name)
00196 return mComponents[i];
00197 }
00198
00199 return NULL;
00200 }
00201
00202
00203 Entity* Entity::getParent() const
00204 {
00205 return pParent;
00206 }
00207
00208
00209 const vector3df& Entity::getPosition() const
00210 {
00211 return mPosition;
00212 }
00213
00214
00215 void Entity::removeAllChildren()
00216 {
00217 for(u32 i = 0; i < mChildren.size(); i++)
00218 {
00219 if(!GameManager::Instance()->getEntityManager()->removeEntity(mChildren[i]))
00220 delete mChildren[i];
00221 }
00222
00223 mChildren.clear();
00224 }
00225
00226
00227 void Entity::removeAllComponents()
00228 {
00229 for(u32 i = 0; i < mComponents.size(); i++)
00230 delete mComponents[i];
00231
00232 mComponents.clear();
00233 }
00234
00235
00236 bool Entity::removeChild(Entity *entity)
00237 {
00238
00239 if(entity == NULL)
00240 return false;
00241
00242
00243 vector<Entity*>::iterator it;
00244
00245 for(it = mChildren.begin(); it < mChildren.end(); it++)
00246 {
00247 Entity *ent = *it;
00248
00249 if(ent == entity)
00250 {
00251 if(!GameManager::Instance()->getEntityManager()->removeEntity(ent))
00252 delete ent;
00253
00254 mChildren.erase(it);
00255 return true;
00256 }
00257 }
00258
00259
00260 return false;
00261 }
00262
00263
00264 bool Entity::removeChild(const u32 id)
00265 {
00266
00267 vector<Entity*>::iterator it;
00268
00269 for(it = mChildren.begin(); it < mChildren.end(); it++)
00270 {
00271 Entity *ent = *it;
00272
00273 if(ent->getID() == id)
00274 {
00275 if(!GameManager::Instance()->getEntityManager()->removeEntity(ent))
00276 delete ent;
00277
00278 mChildren.erase(it);
00279 return true;
00280 }
00281 }
00282
00283
00284 return false;
00285 }
00286
00287
00288 bool Entity::removeChild(const std::string &name)
00289 {
00290
00291 vector<Entity*>::iterator it;
00292
00293 for(it = mChildren.begin(); it < mChildren.end(); it++)
00294 {
00295 Entity *ent = *it;
00296
00297 if(ent->getName() == name)
00298 {
00299 if(!GameManager::Instance()->getEntityManager()->removeEntity(ent))
00300 delete ent;
00301
00302 mChildren.erase(it);
00303 return true;
00304 }
00305 }
00306
00307
00308 return false;
00309 }
00310
00311
00312 bool Entity::removeComponent(EntityComponent *component)
00313 {
00314
00315 if(component == NULL)
00316 return false;
00317
00318
00319 vector<EntityComponent*>::iterator it;
00320
00321 for(it = mComponents.begin(); it < mComponents.end(); it++)
00322 {
00323 EntityComponent *comp = *it;
00324
00325 if(comp == component)
00326 {
00327 delete comp;
00328 mComponents.erase(it);
00329 return true;
00330 }
00331 }
00332
00333
00334 return false;
00335 }
00336
00337
00338 bool Entity::removeComponent(const u32 id)
00339 {
00340
00341 vector<EntityComponent*>::iterator it;
00342
00343 for(it = mComponents.begin(); it < mComponents.end(); it++)
00344 {
00345 EntityComponent *comp = *it;
00346
00347 if(comp->getID() == id)
00348 {
00349 delete comp;
00350 mComponents.erase(it);
00351 return true;
00352 }
00353 }
00354
00355
00356 return false;
00357 }
00358
00359
00360 bool Entity::removeComponent(const std::string &name)
00361 {
00362
00363 vector<EntityComponent*>::iterator it;
00364
00365 for(it = mComponents.begin(); it < mComponents.end(); it++)
00366 {
00367 EntityComponent *comp = *it;
00368
00369 if(comp->getName() == name)
00370 {
00371 delete comp;
00372 mComponents.erase(it);
00373 return true;
00374 }
00375 }
00376
00377
00378 return false;
00379 }
00380
00381
00382 void Entity::setPosition(const vector3df &position)
00383 {
00384
00385 vector3df diff = position - mPosition;
00386
00387
00388 mPosition = position;
00389
00390
00391 GameManager::Instance()->getEventManager()->emitEvent(std::string("ent#") + mName,
00392 "onPositionChange", &diff);
00393 }
00394
00395
00396 void Entity::onPositionChange(void *p)
00397 {
00398 vector3df diff = static_cast<vector3df*>(p)[0];
00399 mPosition += diff;
00400 }
00401
00402
00403 void Entity::onUpdate(void *p)
00404 {
00405 GameManager::Instance()->getEventManager()->emitEvent(std::string("ent#") + mName,
00406 "onUpdate", p);
00407 }
00408
00409
00410 void Entity::onRender(void *p)
00411 {
00412 GameManager::Instance()->getEventManager()->emitEvent(std::string("ent#") + mName,
00413 "onRender", p);
00414 }
00415
00416
00417 void Entity::onPause(void *p)
00418 {
00419 GameManager::Instance()->getEventManager()->emitEvent(std::string("ent#") + mName,
00420 "onPause", p);
00421 }
00422
00423
00424 void Entity::onUnPause(void *p)
00425 {
00426 GameManager::Instance()->getEventManager()->emitEvent(std::string("ent#") + mName,
00427 "onUnPause", p);
00428 }
00429
00430
00431
00432 void bindEntity(asIScriptEngine *engine)
00433 {
00434
00435 int r;
00436
00437
00438 r = engine->RegisterObjectType("Entity", sizeof(Entity), asOBJ_REF);
00439 r = engine->RegisterObjectType("EntityComponent", sizeof(EntityComponent), asOBJ_REF);
00440
00441
00442 r = engine->RegisterObjectBehaviour("Entity", asBEHAVE_FACTORY, "Entity@ f(const string &)",
00443 asFUNCTION(Entity::refFactory), asCALL_CDECL); assert(r >= 0);
00444 r = engine->RegisterObjectBehaviour("Entity", asBEHAVE_ADDREF, "void f()",
00445 asMETHOD(Entity, refAdd), asCALL_THISCALL); assert(r >= 0);
00446 r = engine->RegisterObjectBehaviour("Entity", asBEHAVE_RELEASE, "void f()",
00447 asMETHOD(Entity, refRelease), asCALL_THISCALL); assert(r >= 0);
00448
00449
00450 r = engine->RegisterObjectMethod("Entity", "u32 getID()",
00451 asMETHOD(Entity, getID), asCALL_THISCALL); assert(r >= 0);
00452 r = engine->RegisterObjectMethod("Entity", "const string& getName()",
00453 asMETHOD(Entity, getName), asCALL_THISCALL); assert(r >= 0);
00454
00455 r = engine->RegisterObjectMethod("Entity", "bool addComponent()",
00456 asMETHOD(Entity, addComponent), asCALL_THISCALL); assert(r >= 0);
00457
00458 r = engine->RegisterObjectMethod("Entity", "Entity@ getChild(const u32)",
00459 asMETHODPR(Entity, getChild, (const u32), Entity*),
00460 asCALL_THISCALL); assert(r >= 0);
00461 r = engine->RegisterObjectMethod("Entity", "Entity@ getChild(const string &)",
00462 asMETHODPR(Entity, getChild, (const std::string &), Entity*),
00463 asCALL_THISCALL); assert(r >= 0);
00464 r = engine->RegisterObjectMethod("Entity", "EntityComponent@ getComponent(const u32)",
00465 asMETHODPR(Entity, getComponent, (const u32), EntityComponent*),
00466 asCALL_THISCALL); assert(r >= 0);
00467 r = engine->RegisterObjectMethod("Entity", "EntityComponent@ getComponent(const string &)",
00468 asMETHODPR(Entity, getComponent, (const std::string &), EntityComponent*),
00469 asCALL_THISCALL); assert(r >= 0);
00470
00471 r = engine->RegisterObjectMethod("Entity", "Entity@ getParent()",
00472 asMETHOD(Entity, getParent), asCALL_THISCALL); assert(r >= 0);
00473
00474 r = engine->RegisterObjectMethod("Entity", "void removeAllChildren()",
00475 asMETHOD(Entity, removeAllChildren), asCALL_THISCALL); assert(r >= 0);
00476 r = engine->RegisterObjectMethod("Entity", "void removeAllComponents()",
00477 asMETHOD(Entity, removeAllComponents), asCALL_THISCALL); assert(r >= 0);
00478 r = engine->RegisterObjectMethod("Entity", "bool removeChild(Entity @)",
00479 asMETHODPR(Entity, removeChild, (Entity*), bool),
00480 asCALL_THISCALL); assert(r >= 0);
00481 r = engine->RegisterObjectMethod("Entity", "bool removeChild(const u32)",
00482 asMETHODPR(Entity, removeChild, (const u32), bool),
00483 asCALL_THISCALL); assert(r >= 0);
00484 r = engine->RegisterObjectMethod("Entity", "bool removeChild(const string &)",
00485 asMETHODPR(Entity, removeChild, (const std::string&), bool),
00486 asCALL_THISCALL); assert(r >= 0);
00487 r = engine->RegisterObjectMethod("Entity", "bool removeComponent(EntityComponent @)",
00488 asMETHODPR(Entity, removeComponent, (EntityComponent*), bool),
00489 asCALL_THISCALL); assert(r >= 0);
00490 r = engine->RegisterObjectMethod("Entity", "bool removeComponent(const u32)",
00491 asMETHODPR(Entity, removeComponent, (const u32), bool),
00492 asCALL_THISCALL); assert(r >= 0);
00493 r = engine->RegisterObjectMethod("Entity", "bool removeComponent(const string &)",
00494 asMETHODPR(Entity, removeComponent, (const std::string&), bool),
00495 asCALL_THISCALL); assert(r >= 0);
00496 }
00497
00498