00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "EntityManager.h"
00019 #include "GameManager.h"
00020
00021
00022
00023
00024 EntityManager::EntityManager()
00025 {
00026 init();
00027 }
00028
00029
00030 EntityManager::~EntityManager()
00031 {
00032 clear();
00033 }
00034
00035
00036 void EntityManager::init()
00037 {
00038 }
00039
00040
00041 void EntityManager::clear()
00042 {
00043
00044 removeAllEntities();
00045 }
00046
00047
00048 void EntityManager::refAdd()
00049 {
00050
00051 }
00052
00053
00054 void EntityManager::refRelease()
00055 {
00056
00057 }
00058
00059
00060 bool EntityManager::addEntity(Entity *entity)
00061 {
00062
00063 if(entity == NULL)
00064 return false;
00065
00066
00067 if(getEntity(entity->getName()))
00068 return false;
00069
00070
00071 mEntities.push_back(entity);
00072
00073 return true;
00074 }
00075
00076
00077 Entity* EntityManager::createEntity(const std::string &name, Entity *parent)
00078 {
00079
00080 if(getEntity(name))
00081 return NULL;
00082
00083
00084 Entity *entity = new Entity(name, parent);
00085 entity->refAdd();
00086 mEntities.push_back(entity);
00087 return entity;
00088 }
00089
00090
00091 Entity* EntityManager::getEntity(const u32 id)
00092 {
00093 for(u32 i = 0; i < mEntities.size(); i++)
00094 {
00095 if(mEntities[i]->getID() == id)
00096 return mEntities[i];
00097 }
00098
00099 return NULL;
00100 }
00101
00102
00103 Entity* EntityManager::getEntity(const std::string &name)
00104 {
00105 for(u32 i = 0; i < mEntities.size(); i++)
00106 {
00107 if(mEntities[i]->getName() == name)
00108 return mEntities[i];
00109 }
00110
00111 return NULL;
00112 }
00113
00114
00115 void EntityManager::removeAllEntities()
00116 {
00117 for(u32 i = 0; i < mEntities.size(); i++)
00118 delete mEntities[i];
00119
00120 mEntities.clear();
00121 }
00122
00123
00124 bool EntityManager::removeEntity(Entity *entity)
00125 {
00126
00127 if(entity == NULL)
00128 return false;
00129
00130
00131 vector<Entity*>::iterator it;
00132
00133 for(it = mEntities.begin(); it < mEntities.end(); it++)
00134 {
00135 Entity *ent = *it;
00136
00137 if(ent == entity)
00138 {
00139 delete ent;
00140 mEntities.erase(it);
00141 return true;
00142 }
00143 }
00144
00145
00146 return false;
00147 }
00148
00149
00150 bool EntityManager::removeEntity(const u32 id)
00151 {
00152
00153 vector<Entity*>::iterator it;
00154
00155 for(it = mEntities.begin(); it < mEntities.end(); it++)
00156 {
00157 Entity *ent = *it;
00158
00159 if(ent->getID() == id)
00160 {
00161 delete ent;
00162 mEntities.erase(it);
00163 return true;
00164 }
00165 }
00166
00167
00168 return false;
00169 }
00170
00171
00172 bool EntityManager::removeEntity(const std::string &name)
00173 {
00174
00175 vector<Entity*>::iterator it;
00176
00177 for(it = mEntities.begin(); it < mEntities.end(); it++)
00178 {
00179 Entity *ent = *it;
00180
00181 if(ent->getName() == name)
00182 {
00183 delete ent;
00184 mEntities.erase(it);
00185 return true;
00186 }
00187 }
00188
00189
00190 return false;
00191 }
00192
00193
00194
00195 void bindEntityManager(asIScriptEngine *engine)
00196 {
00197
00198 int r;
00199
00200
00201 r = engine->RegisterObjectType("EntityManager", sizeof(EntityManager), asOBJ_REF); assert(r >= 0);
00202
00203
00204 r = engine->RegisterObjectBehaviour("EntityManager", asBEHAVE_ADDREF, "void f()",
00205 asMETHOD(EntityManager, refAdd), asCALL_THISCALL); assert(r >= 0);
00206 r = engine->RegisterObjectBehaviour("EntityManager", asBEHAVE_RELEASE, "void f()",
00207 asMETHOD(EntityManager, refRelease), asCALL_THISCALL); assert(r >= 0);
00208
00209
00210 r = engine->RegisterObjectMethod("EntityManager", "void init()",
00211 asMETHOD(EntityManager, init), asCALL_THISCALL); assert(r >= 0);
00212 r = engine->RegisterObjectMethod("EntityManager", "void clear()",
00213 asMETHOD(EntityManager, clear), asCALL_THISCALL); assert(r >= 0);
00214
00215 r = engine->RegisterObjectMethod("EntityManager", "bool addEntity(Entity @)",
00216 asMETHOD(EntityManager, addEntity), asCALL_THISCALL); assert(r >= 0);
00217 r = engine->RegisterObjectMethod("EntityManager", "Entity@ createEntity(const string &)",
00218 asMETHOD(EntityManager, createEntity), asCALL_THISCALL); assert(r >= 0);
00219 r = engine->RegisterObjectMethod("EntityManager", "Entity@ createEntity(const string &, Entity @)",
00220 asMETHOD(EntityManager, createEntity), asCALL_THISCALL); assert(r >= 0);
00221
00222 r = engine->RegisterObjectMethod("EntityManager", "Entity@ getEntity(const u32)",
00223 asMETHODPR(EntityManager, getEntity, (const u32), Entity*),
00224 asCALL_THISCALL); assert(r >= 0);
00225 r = engine->RegisterObjectMethod("EntityManager", "Entity@ getEntity(const string &)",
00226 asMETHODPR(EntityManager, getEntity, (const std::string &), Entity*),
00227 asCALL_THISCALL); assert(r >= 0);
00228
00229 r = engine->RegisterObjectMethod("EntityManager", "void removeAllEntities()",
00230 asMETHOD(EntityManager, removeAllEntities), asCALL_THISCALL); assert(r >= 0);
00231 r = engine->RegisterObjectMethod("EntityManager", "bool removeEntity(Entity @)",
00232 asMETHODPR(EntityManager, removeEntity, (Entity*), bool),
00233 asCALL_THISCALL); assert(r >= 0);
00234 r = engine->RegisterObjectMethod("EntityManager", "bool removeEntity(const u32)",
00235 asMETHODPR(EntityManager, removeEntity, (const u32), bool),
00236 asCALL_THISCALL); assert(r >= 0);
00237 r = engine->RegisterObjectMethod("EntityManager", "bool removeEntity(const string &)",
00238 asMETHODPR(EntityManager, removeEntity, (const std::string &), bool),
00239 asCALL_THISCALL); assert(r >= 0);
00240 }
00241