void MapEditor::writeEntityToFile(std::string filename){

	std::string posString;
	std::string output;
	std::ofstream entityFile(filename);

	if (entityFile.is_open()){
		for (Entities::size_type i = 0; i < mEntities.size(); i++){

			// Inserts typename into output followed by '-'
			switch (mEntities[i]->getType()){
			case Entity::PLAYER:
				output.push_back('P');
				output.push_back('0');
				break;
		
			case Entity::WORM:
				output.push_back('W');
				output.push_back('0');
				break;

			case Entity::GERM:
				output.push_back('G');
				output.push_back('E');
				break;

			case Entity::ACIDMONSTER:
				output.push_back('A');
				output.push_back('C');
				break;

			case Entity::MEATBALL:
				output.push_back('M');
				output.push_back('0');
				break;

			case Entity::EXTRALIFE:
				output.push_back('E');
				output.push_back('0');
				break;

			case Entity::OCTO_PI:
				output.push_back('O');
				output.push_back('P');
				break;

			default:
				break;
			}
			output.push_back('|');

			// Inserts xpos into output followed by a ','
			posString = MapEditor::floatToString(mEntities[i]->getPos().x + mEntities[i]->getOffset().x);
			for (std::string::size_type iS = 0; iS < posString.size(); iS++){
				output.push_back(posString[iS]);
			}
			output.push_back(',');

			// Inserts ypos into output
			posString = MapEditor::floatToString(mEntities[i]->getPos().y + mEntities[i]->getOffset().y);
			for (std::string::size_type iS = 0; iS < posString.size(); iS++){
				output.push_back(posString[iS]);
			}

			// Writes output into file
			entityFile << output << std::endl;
			//

			output.clear();
			posString.clear();
		}
	}
	entityFile.close();
}
Beispiel #2
0
World::World(std::string *level, TextureLibrary *lib, int playerX, int playerY, int playerRot) {
	std::string line;
	std::ifstream entityFile(*level);
	boost::char_separator<char> sep(" ");
	int width, height, tileSize;
	std::string tilePath;

	// Gets level parameters
	if (std::getline(entityFile, line)) {
		boost::tokenizer<boost::char_separator<char>> tok(line, sep);
		TokenIterator it = tok.begin();

		width = std::stoi(*it);
		++it;

		height = std::stoi(*it);
		++it;

		tileSize = std::stoi(*it);
		++it;

		_size.x = std::stoi(*it);
		++it;

		_size.y = std::stoi(*it);
		++it;

		tilePath = *it;
	}
	else {
		throw new std::runtime_error("Parsing error on header: unexpected end of line");
	}

	if ((!std::getline(entityFile, line)) || (line != "")) {
		throw new std::runtime_error("Parsing error on header: unexpected end of line");
	}

	// Get map tiles
	sf::Texture* tex;

	for (int i = 0; i < height; i++) {
		if (std::getline(entityFile, line)) {
			boost::tokenizer<boost::char_separator<char>> tok(line, sep);
			TokenIterator it = tok.begin();

			for (int j = 0; j < width; j++) {
				if (it == tok.end()) {
					throw new std::runtime_error("Parsing error on tilemap: unexpected end of line");
				}

				// Builds the texture
				tex = lib->Load(tilePath, tileSize, std::stoi(*it));

				// Builds the sprite
				sf::Sprite sprite;
				sprite.setTexture(*tex);
				sprite.setPosition(j * tileSize, i * tileSize);
				_mapTiles.push_back(sprite);
				
				++it;
			}
		}
		else {
			throw new std::runtime_error("Parsing error on tilemap: unexpected EOF");
		}
	}

	if ((!std::getline(entityFile, line)) || (line != "")) {
		throw new std::runtime_error("Parsing error on tilemap: unexpected end of line");
	}

	// Builds the world collision map
	for (int i = 0; i < height; i++) {
		_mapBarriers.push_back(std::vector<TileType>());

		if (std::getline(entityFile, line)) {
			boost::tokenizer<boost::char_separator<char>> tok(line, sep);
			TokenIterator it = tok.begin();

			for (int j = 0; j < width; j++) {
				if (it == tok.end()) {
					throw new std::runtime_error("Parsing error on wallmap: unexpected end of line");
				}

				// Determines if the tile is solid
				_mapBarriers[i].push_back((TileType)(std::stoi(*it)));

				++it;
			}
		}
		else {
			throw new std::runtime_error("Parsing error on wallmap: unexpected EOF");
		}
	}

	if ((!std::getline(entityFile, line)) || (line != "")) {
		throw new std::runtime_error("Parsing error on tilemap: unexpected end of line");
	}

	// Loading entities required at initialization of the level
	while (std::getline(entityFile, line) && line != "") {
		boost::tokenizer<boost::char_separator<char>> tok(line, sep);
		TokenIterator it = tok.begin();

		if (it != tok.end()) {
			if (*it == "player") {
				++it;
				AddEntity(playerX, playerY, playerRot, &(std::string(*it)));
			}
			else {
				int x, y, rot;

				x = std::stoi(*it);
				++it;

				y = std::stoi(*it);
				++it;

				rot = std::stoi(*it);
				++it;

				AddEntity(x, y, rot, &(std::string(*it)));
			}
		}
		else {
			throw new std::runtime_error("Parsing error on entities: unexpected end of line");
		}
	}
}