GameObjectPtr CLevel::CreateObject( const char * type, const char * templ ) { GameObjectPtr obj; if ( type != NULL && type[0] != 0 ) { if ( strcasecmp(type, "cgameobject") == 0 ) { obj = new CGameObject( this ); } else if ( strcasecmp(type, "cparticlesystem") == 0 ) { obj = new CParticleSystem( this ); } else { IwAssertMsg(GAME, false, ("Cant load unknown type '%s'", type)); } } else { IwAssertMsg(GAME, false, ("CLevel::CreateObject invalid type")); } if ( templ != NULL ) { TiXmlElement * elem_tmpl = GetTemplate( templ ); obj->Load( elem_tmpl ); } return obj; }
void CLevel::Load( TiXmlElement * root ) { if ( root == NULL ) { return; } int sw = VIRTUAL_WIDTH; int sh = VIRTUAL_HEIGHT; root->QueryIntAttribute("w", &sw); root->QueryIntAttribute("h", &sh); mSceneWidth = (uint32)sw; mSceneHeight = (uint32)sh; int initCamX = 0; int initCamY = 0; root->QueryIntAttribute("init_cam_x", &initCamX); root->QueryIntAttribute("init_cam_y", &initCamY); mInitCam.x = (float)initCamX; mInitCam.y = (float)initCamY; // includes TiXmlElement * elem_inc = root->FirstChildElement("include"); for(; elem_inc; elem_inc = elem_inc->NextSiblingElement("include")) { const char * path = elem_inc->Attribute("path"); if ( path != 0 ) { TiXmlDocument doc; if ( OpenXmlFile(path, &doc) ) { TiXmlElement * elem_templ = doc.FirstChildElement("template"); for(; elem_templ; elem_templ = elem_templ->NextSiblingElement("template")) { AddTemplate( (TiXmlElement *)elem_templ->Clone(), true ); } } } mIncludes.push_back((TiXmlElement *)elem_inc->Clone()); } // templates TiXmlElement * elem_templ = root->FirstChildElement("template"); for(; elem_templ; elem_templ = elem_templ->NextSiblingElement("template")) { AddTemplate( (TiXmlElement *)elem_templ->Clone(), false ); } TiXmlElement * elem_obj = root->FirstChildElement("object"); for(; elem_obj; elem_obj = elem_obj->NextSiblingElement("object")) { GameObjectPtr obj; const char * type = elem_obj->Attribute("type"); const char * tmpl = elem_obj->Attribute("template"); obj = CreateObject( type, tmpl ); IwAssert(LEVEL, obj != NULL); obj->Load( elem_obj ); obj->Init(); AddObject( obj ); #if APP_SAVE_XML obj->SetSave(true); #endif } DoAddObjs(); }