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 #include "../components/components.h"
00022
00023
00024
00025
00026 EntityManager::EntityManager()
00027 {
00028 init();
00029 }
00030
00031
00032 EntityManager::~EntityManager()
00033 {
00034 clear();
00035 }
00036
00037
00038 void EntityManager::init()
00039 {
00040 }
00041
00042
00043 void EntityManager::clear()
00044 {
00045
00046 removeEntities();
00047 }
00048
00049
00050 void EntityManager::grab()
00051 {
00052
00053 }
00054
00055
00056 void EntityManager::drop()
00057 {
00058
00059 }
00060
00061
00062 bool EntityManager::addEntity(Entity *entity)
00063 {
00064
00065 if(entity == NULL)
00066 return false;
00067
00068
00069 if(getEntity(entity->getName()))
00070 return false;
00071
00072
00073 mEntities.push_back(entity);
00074 entity->grab();
00075
00076 return true;
00077 }
00078
00079
00080 Entity* EntityManager::createEntity(const std::string &name, Entity *parent, bool grab)
00081 {
00082
00083 if(grab && getEntity(name))
00084 return NULL;
00085
00086
00087 Entity *entity = new Entity(name, parent);
00088
00089 if(grab)
00090 {
00091 mEntities.push_back(entity);
00092 entity->grab();
00093 }
00094
00095 return entity;
00096 }
00097
00098
00099 Entity* EntityManager::createEntityFromXML(const std::string &fileName, Entity *parent, AssetGroup *assets, bool grab)
00100 {
00101
00102 IFileSystem *fileSystem = GameManager::Instance()->getDevice()->getFileSystem();
00103
00104
00105 if(!fileSystem->existFile(fileName.c_str()))
00106 return NULL;
00107
00108
00109 IXMLReader *file = fileSystem->createXMLReader(fileName.c_str());
00110
00111 return createEntityFromXML(file, parent, assets, grab);
00112 }
00113
00114
00115 Entity* EntityManager::createEntityFromXML(IXMLReader *file, Entity *parent, AssetGroup *assets, bool grab)
00116 {
00117
00118 if(!file)
00119 return NULL;
00120
00121
00122 Entity *entity = NULL;
00123 bool doneParsing = false;
00124
00125 while(file->read() && !doneParsing)
00126 {
00127 switch(file->getNodeType())
00128 {
00129 case io::EXN_ELEMENT:
00130
00131
00132 if(stringw("Entity") == file->getNodeName())
00133 {
00134
00135 stringc name = file->getAttributeValue(L"name");
00136
00137 if(grab && getEntity(name.c_str()) != NULL)
00138 return NULL;
00139
00140 entity = new Entity(name.c_str(), parent, assets);
00141
00142
00143 if(parent == NULL)
00144 {
00145
00146 stringc parentFile = file->getAttributeValue(L"parent");
00147
00148
00149 if(parentFile != "")
00150 {
00151
00152 Entity *retrievedParent = NULL;
00153
00154 if(assets != NULL)
00155 {
00156 EntityProcessor *processor = static_cast<EntityProcessor*>
00157 (assets->getAssetProcessor("entities"));
00158
00159 retrievedParent = processor->getEntity(parentFile.c_str());
00160 }
00161
00162 else
00163 retrievedParent = GameManager::Instance()->getEntityManager()
00164 ->getEntity(parentFile.c_str());
00165
00166 if(retrievedParent == NULL && assets == NULL)
00167 {
00168 parent = GameManager::Instance()->getEntityManager()->
00169 createEntityFromXML(parentFile.c_str(), NULL, NULL, grab);
00170 }
00171
00172
00173 if(retrievedParent != NULL)
00174 entity->setParent(retrievedParent);
00175 }
00176 }
00177 }
00178
00179
00180 if(entity != NULL)
00181 {
00182
00183 if(stringw("absolutePosition") == file->getNodeName())
00184 {
00185 entity->setAbsolutePosition(vector3df( file->getAttributeValueAsFloat(L"x"),
00186 file->getAttributeValueAsFloat(L"y"),
00187 file->getAttributeValueAsFloat(L"z") ));
00188 }
00189
00190
00191 else if(stringw("position") == file->getNodeName())
00192 {
00193 entity->setPosition(vector3df( file->getAttributeValueAsFloat(L"x"),
00194 file->getAttributeValueAsFloat(L"y"),
00195 file->getAttributeValueAsFloat(L"z") ));
00196 }
00197
00198
00199 else if(stringw("absoluteRotation") == file->getNodeName())
00200 {
00201 entity->setAbsoluteRotation(vector3df( file->getAttributeValueAsFloat(L"x"),
00202 file->getAttributeValueAsFloat(L"y"),
00203 file->getAttributeValueAsFloat(L"z") ));
00204 }
00205
00206
00207 else if(stringw("rotation") == file->getNodeName())
00208 {
00209 entity->setRotation(vector3df( file->getAttributeValueAsFloat(L"x"),
00210 file->getAttributeValueAsFloat(L"y"),
00211 file->getAttributeValueAsFloat(L"z") ));
00212 }
00213
00214
00215 else if(stringw("components") == file->getNodeName())
00216 parseComponentsXML(file, entity);
00217 }
00218
00219 break;
00220
00221 case io::EXN_ELEMENT_END:
00222
00223
00224 if(stringw("Entity") == file->getNodeName())
00225 doneParsing = true;
00226
00227 break;
00228
00229 default:
00230 break;
00231 }
00232 }
00233
00234
00235 if(grab && entity != NULL)
00236 {
00237 mEntities.push_back(entity);
00238 entity->grab();
00239 }
00240
00241
00242 return entity;
00243 }
00244
00245
00246 Entity* EntityManager::getEntity(const u32 id)
00247 {
00248 for(u32 i = 0; i < mEntities.size(); i++)
00249 {
00250 if(mEntities[i]->getID() == id)
00251 return mEntities[i];
00252 }
00253
00254 return NULL;
00255 }
00256
00257
00258 Entity* EntityManager::getEntity(const std::string &name)
00259 {
00260 for(u32 i = 0; i < mEntities.size(); i++)
00261 {
00262 if(mEntities[i]->getName() == name)
00263 return mEntities[i];
00264 }
00265
00266 return NULL;
00267 }
00268
00269
00270 void EntityManager::removeEntities()
00271 {
00272 for(u32 i = 0; i < mEntities.size(); i++)
00273 mEntities[i]->drop();
00274
00275 mEntities.clear();
00276 }
00277
00278
00279 bool EntityManager::removeEntity(Entity *entity)
00280 {
00281
00282 if(entity == NULL)
00283 return false;
00284
00285
00286 vector<Entity*>::iterator it;
00287
00288 for(it = mEntities.begin(); it < mEntities.end(); it++)
00289 {
00290 Entity *ent = *it;
00291
00292 if(ent == entity)
00293 {
00294 ent->drop();
00295 mEntities.erase(it);
00296 return true;
00297 }
00298 }
00299
00300
00301 return false;
00302 }
00303
00304
00305 bool EntityManager::removeEntity(const u32 id)
00306 {
00307
00308 vector<Entity*>::iterator it;
00309
00310 for(it = mEntities.begin(); it < mEntities.end(); it++)
00311 {
00312 Entity *ent = *it;
00313
00314 if(ent->getID() == id)
00315 {
00316 ent->drop();
00317 mEntities.erase(it);
00318 return true;
00319 }
00320 }
00321
00322
00323 return false;
00324 }
00325
00326
00327 bool EntityManager::removeEntity(const std::string &name)
00328 {
00329
00330 vector<Entity*>::iterator it;
00331
00332 for(it = mEntities.begin(); it < mEntities.end(); it++)
00333 {
00334 Entity *ent = *it;
00335
00336 if(ent->getName() == name)
00337 {
00338 ent->drop();
00339 mEntities.erase(it);
00340 return true;
00341 }
00342 }
00343
00344
00345 return false;
00346 }
00347
00348