void TileMap::parseTmx(Tmx::Map &tmxMap) { this->width = tmxMap.GetWidth(); this->height = tmxMap.GetHeight(); this->tileWidth = tmxMap.GetTileWidth(); this->tileHeight = tmxMap.GetTileHeight(); LOG(INFO) << "Number of tilesets: " + std::to_string(tmxMap.GetNumTilesets()); std::vector<Tileset> tilesets; for (int i = 0; i < tmxMap.GetNumTilesets(); ++i) { const Tmx::Tileset *tmxTileset = tmxMap.GetTileset(i); Tileset tileset; tileset.imageSource = tmxTileset->GetImage()->GetSource(); tileset.imageWidth = tmxTileset->GetImage()->GetWidth(); tileset.imageHeight = tmxTileset->GetImage()->GetHeight(); tileset.tileWidth = tmxTileset->GetTileWidth(); tileset.tileHeight = tmxTileset->GetTileHeight(); tilesets.push_back(tileset); } for (int i = 0; i < tmxMap.GetNumTileLayers(); ++i) { const Tmx::TileLayer *tmxTileLayer = tmxMap.GetTileLayer(i); parseTileLayer(tilesets, tmxTileLayer); } }
Level* LevelParser::parseLevel(const char* levelFile, PlayState* newState){ //Create the XML document and load from file TiXmlDocument levelDocument; levelDocument.LoadFile(levelFile); //Create the level object Level* pLevel = new Level(); //Get the root node TiXmlElement* pRoot = levelDocument.RootElement(); pRoot->Attribute("tilewidth", &m_tileSize); pRoot->Attribute("width" , &m_width); pRoot->Attribute("height", &m_height); //ToDo add orientation and version number //Look for then parse textures TiXmlElement* textureRoot = findElement(string("properties"), pRoot)->FirstChildElement(); while (textureRoot != NULL) { parseTextures(textureRoot); textureRoot = textureRoot->NextSiblingElement(); } //Look for then parse tilesets for (TiXmlElement* currTileSet = pRoot->FirstChildElement(); currTileSet != NULL ; currTileSet = currTileSet->NextSiblingElement()){ if ((currTileSet->Value() == string("tileset"))){ parseTilesets(currTileSet,pLevel->getTilesets()); } } //Look for and Parse level layers for (TiXmlElement* e = pRoot->FirstChildElement(); e != NULL ; e = e->NextSiblingElement()){ if (e->Value() == string("layer") || e->Value() == string("objectgroup")){ string layerType = e->Attribute("name"); if (e->FirstChildElement()->Value() == string("object")){ //Parse Object layer parseObjectLayer(e, pLevel->getLayers(),layerType,pLevel,newState); } else if (e->FirstChildElement()->Value() == string("properties")){ //Parse Tile Layer parseTileLayer(e, pLevel->getLayers(),pLevel->getCollisionLayers(), pLevel->getTilesets()); } } } return pLevel; }
Level* LevelParser::parseLevel(const char *levelFile) { TiXmlDocument levelDocument; levelDocument.LoadFile(levelFile); Level* pLevel = new Level(); TiXmlElement* pRoot = levelDocument.RootElement(); pRoot->Attribute("tilewidth", &m_tileSizew); pRoot->Attribute("tileheight", &m_tileSizeh); pRoot->Attribute("width", &m_width); pRoot->Attribute("height", &m_height); for (TiXmlElement* e = pRoot->FirstChildElement(); e != NULL; e = e->NextSiblingElement()) { if (e->Value() == std::string("tileset")) { parseTilesets(e, pLevel->getTilesets()); } if (e->Value() == std::string("properties")) { TiXmlElement* pproperties = e; for (TiXmlElement* e = pproperties->FirstChildElement(); e != NULL; e = e->NextSiblingElement()) { if (e->Value() == std::string("property")) { parseTextures(e); } } } if (e->Value() == std::string("objectgroup") || e->Value() == std::string("layer")) { if (e->FirstChildElement()->Value() == std::string("object")) { parseObjectLayer(e, pLevel->getLayers(), pLevel); } else if (e->FirstChildElement()->Value() == std::string("data") || (e->FirstChildElement()->NextSiblingElement() != 0 && e->FirstChildElement()->NextSiblingElement()->Value() == std::string("data"))) { parseTileLayer(e, pLevel->getLayers(), pLevel->getTilesets(), pLevel->getCollisionLayers()); } } } return pLevel; }
Level* LevelParser::parseLevel(const char *levelFile) { // create a TinyXML document and load the map XML TiXmlDocument levelDocument; levelDocument.LoadFile(levelFile); // create the level object Level* pLevel = new Level(); // get the root node TiXmlElement* pRoot = levelDocument.RootElement(); pRoot->Attribute("tilewidth", &m_tileSize); pRoot->Attribute("width", &m_width); pRoot->Attribute("height", &m_height); // parse the tilesets for (TiXmlElement* e = pRoot->FirstChildElement(); e != NULL; e = e->NextSiblingElement()) { if (e->Value() == std::string("properties")) { parseTextures(e->FirstChildElement()); } if (e->Value() == std::string("tileset")) { parseTilesets(e, pLevel->getTilesets()); } } // parse any object layers // parse any object layers for (TiXmlElement* e = pRoot->FirstChildElement(); e != NULL; e = e->NextSiblingElement()) { if (e->Value() == std::string("objectgroup")) { parseObjectLayer(e, pLevel->getLayers(), pLevel); } else if (e->Value() == std::string("layer")) { parseTileLayer(e, pLevel->getLayers(), pLevel->getTilesets(), pLevel->getCollisionLayers()); } } return pLevel; }
Level* LevelParser::parseLevel(const char *levelFile) { // create a tinyXML document and load the map xml TiXmlDocument levelDocument; levelDocument.LoadFile(levelFile); // create the level object Level* pLevel = new Level(); // get the root node and display some values TiXmlElement* pRoot = levelDocument.RootElement(); std::cout << "Loading level:\n" << "Version: " << pRoot->Attribute("version") << "\n"; std::cout << "Width:" << pRoot->Attribute("width") << " - Height:" << pRoot->Attribute("height") << "\n"; std::cout << "Tile Width:" << pRoot->Attribute("tilewidth") << " - Tile Height:" << pRoot->Attribute("tileheight") << "\n"; pRoot->Attribute("tilewidth", &m_tileSize); pRoot->Attribute("width", &m_width); pRoot->Attribute("height", &m_height); //we know that properties is the first child of the root TiXmlElement* pProperties = pRoot->FirstChildElement(); // we must parse the textures needed for this level, which have been added to properties for(TiXmlElement* e = pProperties->FirstChildElement(); e != NULL; e = e->NextSiblingElement()) { if(e->Value() == std::string("property")) { parseTextures(e); } } // we must now parse the tilesets for(TiXmlElement* e = pRoot->FirstChildElement(); e != NULL; e = e->NextSiblingElement()) { if(e->Value() == std::string("tileset")) { parseTilesets(e, pLevel->getTilesets()); } } // parse any object layers for(TiXmlElement* e = pRoot->FirstChildElement(); e != NULL; e = e->NextSiblingElement()) { if(e->Value() == std::string("objectgroup") || e->Value() == std::string("layer")) { if(e->FirstChildElement()->Value() == std::string("object")) { parseObjectLayer(e, pLevel->getLayers(), pLevel); } else if(e->FirstChildElement()->Value() == std::string("data") || (e->FirstChildElement()->NextSiblingElement() != 0 && e->FirstChildElement()->NextSiblingElement()->Value() == std::string("data"))) { parseTileLayer(e, pLevel->getLayers(), pLevel->getTilesets(), pLevel->getCollisionLayers()); } } } // set the players collision layer // pLevel->getPlayer()->setCollisionLayers(pLevel->getCollisionLayers()); return pLevel; }