コード例 #1
0
	LevelFactory * RandomObjectFactoryDataModel::InternalRead(ifstream * stream, map<string, LevelFactory*>* previousFactories)
	{
		RandomObjectFactory* result = new RandomObjectFactory();

		string currentLine;
		// Get the line that should state the random factory adding phase beginning.
		getline(*stream, currentLine);
		if (currentLine != "Begin AddRandomFactory")
		{
			throw new std::invalid_argument("Wrong format.");
		}

		// Get the first random factory probability.
		getline(*stream, currentLine);
		while (currentLine != "End AddRandomFactory")
		{
			// Convert the read string to a proper probability float.
			float probability = std::stof(currentLine);
		
			// Get the random factory name.
			getline(*stream, currentLine);
			// Get the random factory according to its name, if it exist.
			LevelFactory* composerFactory = GetFactoryByName(currentLine, previousFactories);
			
			// Add the found factory as a random factory of the currently constructing factory.
			result->AddAlternativeFactory(probability, composerFactory);

			// Read the newt line. Either a random factory probability or an end marker.
			getline(*stream, currentLine);
		}

		_factory = result;
		return _factory;
	}
	void NeighborDensityFactoryDataModel::ReadCustomRule(ifstream * stream, map<string, LevelFactory*>* previousFactories, NeighborDensityFactory* factory)
	{
		string currentLine;
		// Get the rule factory name.
		getline(*stream, currentLine);
		// Get the corresponding factory.
		LevelFactory* ruleFactory = GetFactoryByName(currentLine, previousFactories);

		if (ruleFactory == NULL)
		{
			throw new std::invalid_argument("Cannot have a null factory as a rule.");
		}

		// Rule to be added.
		Rule* newRule = new Rule(ruleFactory);

		getline(*stream, currentLine);

		// Fill the conditions of the Rule. Until the "End of Rule" marker is met.
		while (currentLine != "End AddConditions")
		{
			Vector3 fetchCoordinates = UtilityReaderWriter::ReadVector3(stream);
			bool expectedValue = UtilityReaderWriter::ReadBool(stream);

			// Add the new condition to the Rule.
			newRule->AddCondition(fetchCoordinates, expectedValue);

			getline(*stream, currentLine);
		}

		factory->AddRule(newRule);
	}
	LevelFactory * TransformationFactoryDataModel::InternalRead(ifstream * stream, map<string, LevelFactory*>* previousFactories)
	{
		// Name of the subLevelFactory to be read in the file.
		string subLevelFactoryName;
		// Get the string name of the sublevel factory.
		getline(*stream, subLevelFactoryName);
		// Get the sublevel factory according to its name, if it exist.
		LevelFactory* subLevelFactory = GetFactoryByName(subLevelFactoryName, previousFactories);

		// The sublevel has to exist.
		if (subLevelFactory == NULL)
		{
			throw new std::invalid_argument("A transformation factory should always have a sublevel.");
		}

		// Descriptor of the matrix constructor to read and use.
		string matrixConstructor;
		// Get the matrix constructor.
		getline(*stream, matrixConstructor);

		// Final transformation matrix.
		Matrix4 transformation;

		if (matrixConstructor == "Matrix")
		{
			transformation = ReadMatrix4(stream);
		}
		else if(matrixConstructor == "YRotation")
		{
			transformation = ReadYRotation(stream);
		}
		else if (matrixConstructor == "Translation")
		{
			transformation = ReadTranslation(stream);
		}
		else
		{
			throw new std::exception("Invalid constructor specified.");
		}

		return new TransformationFactory(subLevelFactory, transformation);
	}
	void NeighborDensityFactoryDataModel::Read8FetchRule(ifstream * stream, map<string, LevelFactory*>* previousFactories, NeighborDensityFactory* factory)
	{
		string currentLine;
		// Get the rule factory name.
		getline(*stream, currentLine);
		// Get the corresponding factory.
		LevelFactory* ruleFactory = GetFactoryByName(currentLine, previousFactories);

		if (ruleFactory == NULL)
		{
			throw new std::invalid_argument("Cannot have a null factory as a rule.");
		}

		// Read the 8 conditions and store them into a list.
		list<bool> conditions = list<bool>();
		for (int i = 0; i < 8; i++)
		{
			getline(*stream, currentLine);
			conditions.push_back((bool)std::stoi(currentLine));
		}

		factory->AddRule(conditions, ruleFactory);
	}