Пример #1
0
Level* GameInfo::loadLevel(string filename, std::shared_ptr<GenericPool<PhysicsComponent>> physicsPool)
{
	picojson::value v;
	ifstream levelStream;
	levelStream.open(filename);
	int currentTileLayer = 1;
	std::string tileString = "Tile Layer ";
	int width = 0;
	int height = 0;
	int x = 0;
	int y = 0;
	const picojson::array* tileArray = NULL;

	levelStream >> v;
	if (levelStream.fail())
	{
		std::cerr << picojson::get_last_error() << std::endl;
		return NULL;
	}

	//std::cout << "---- dump input ----" << std::endl;
	//std::cout << v << std::endl;

	//std::cout << "---- analyzing input ----" << std::endl;
	/*if (v.is<picojson::null>())
	{
		std::cout << "input is null" << std::endl;
	}
	else if (v.is<bool>())
	{
		std::cout << "input is " << (v.get<bool>() ? "true" : "false") << std::endl;
	}
	else if (v.is<double>())
	{
		std::cout << "input is " << v.get<double>() << std::endl;
	}
	else if (v.is<std::string>())
	{
		std::cout << "input is " << v.get<std::string>() << std::endl;
	}
	else if (v.is<picojson::array>())
	{
		std::cout << "input is an array" << std::endl;
		const picojson::array& a = v.get<picojson::array>();
		for (picojson::array::const_iterator i = a.begin(); i != a.end(); ++i)
		{
			std::cout << "  " << *i << std::endl;
		}
	}*/
	if (v.is<picojson::object>())
	{
		const picojson::object& o = v.get<picojson::object>();
		for (picojson::object::const_iterator i = o.begin(); i != o.end(); ++i)
		{
			if (i->first == "height")
				height = (int)i->second.get<double>();

			else if (i->first == "width")
				width = (int)i->second.get<double>();
		}
	}
	Locator::getLog()->printToFile("GameInfo-LoadLevel", "Width: " + std::to_string(width) + ", Height: " + std::to_string(height));

	Level_Tile_3DVector_.resize(MAXTILELAYERS, std::vector<std::vector<int>>(width, std::vector<int>(height, 0)));

	if (v.is<picojson::object>())
	{
		//std::cout << "input is an object" << std::endl;
		const picojson::object& o = v.get<picojson::object>();
		for (picojson::object::const_iterator i = o.begin(); i != o.end(); ++i)
		{
			if (i->second.is<picojson::array>())
			{
				//std::cout << "2nd is an array" << std::endl;
				const picojson::array& a = i->second.get<picojson::array>();
				for (picojson::array::const_iterator i = a.begin(); i != a.end(); ++i)
				{
					auto g = *i;

					if (g.is<std::string>())
					{
						//std::cout << "g is a string" << std::endl;
					}
					else if (g.is<picojson::object>())	//gets the objects within the layers array
					{
						//std::cout << "g is an object" << std::endl;
						const picojson::object& q = g.get<picojson::object>();
						for (picojson::object::const_iterator p = q.begin(); p != q.end(); ++p)
						{
							if (p->first == "name")
							{
								if (p->second.is<std::string>())
								{
									std::string tempString = p->second.get<std::string>();
									for (int ggg = 0; ggg < MAXTILELAYERS; ggg++)
									{
										if (tempString == tileString + std::to_string(ggg + 1))
										{
											currentTileLayer = ggg;
										}
									}

								}
							}
						}

						for (picojson::object::const_iterator p = q.begin(); p != q.end(); ++p)
						{

							if (p->first == "data")	//tile information
							{
								//we found the tiles here! finally!
								if (p->second.is<picojson::array>())
								{
									//std::cout << "FREAK OUT AGAIN" << std::endl;
									//tileArray = &p->second.get<picojson::array>();
									tileArray = &p->second.get<picojson::array>();
									for (picojson::array::const_iterator u = tileArray->begin(); u != tileArray->end(); ++u)
									{
										auto l = *u;
										//if (l.is<std::string>())	std::cout << "l is a string" << std::endl;
										if (l.is<double>())
										{
											Level_Tile_3DVector_[currentTileLayer][x][y] = (int)l.get<double>();
											x++;

											if (x == width)
											{
												x = 0;
												y++;
											}
											if (y == height)
											{
												x = 0; 
												y = 0;
											}

											//levelTiles_.push_back((int)l.get<double>());

												//std::cout << "added: " << currentTileLayer << std::endl;
										}
									}
									//found the array containing the tile data!
									//const picojson::array& a = p->second.get<picojson::array>();

								}
							}
							
							else if (p->first == "objects")	//collision objects (or any object)
							{
								if (p->second.is<picojson::array>())	//array of actual objects
								{
									tileArray = &p->second.get<picojson::array>();
									for (picojson::array::const_iterator u = tileArray->begin(); u != tileArray->end(); ++u)
									{
										auto l = *u;

										if (l.is<picojson::object>())	//found an actual collision object now!
										{
											const picojson::object& ww = l.get<picojson::object>();	//get the object
											double h = -1, w = -1, xtemp = -1, ytemp = -1;
											for (picojson::object::const_iterator qq = ww.begin(); qq != ww.end(); ++qq)	//iterate through data
											{
												if (qq->first == "height")
													h = qq->second.get<double>();
												else if (qq->first == "width")
													w = qq->second.get<double>();
												else if (qq->first == "x")
													xtemp = qq->second.get<double>();
												else if (qq->first == "y")
													ytemp = qq->second.get<double>();
											}
											PhysicsComponent* phys = physicsPool->getAvailableElement();
											phys->setCoordsAndDimensions(xtemp, ytemp, w, h);
											phys->setPhysicsType(BLOCK);
										}
									}
								}
							}
						}
					}
				}
			}
		}
	}
	//std::cout << "Width: " << width << std::endl;
	//std::cout << "Height: " << height << std::endl;
	if (currentLevel_ != nullptr)
		delete currentLevel_;
	currentLevel_ = new Level(Level_Tile_3DVector_, physicsPool);
	currentLevel_->backgroundTex.create(width*TILESIZE, height*TILESIZE);
	return currentLevel_;
}