Пример #1
0
void Grid::loadGrid(std::string* layout, std::map<int, std::shared_ptr<Equipment>>& equipMap, std::vector<LaserSource>& my_lasers, std::vector<std::shared_ptr<Equipment>>& my_targets)
{
	for (int row = 0; row < GRID_HEIGHT; row++) 
	{
		std::vector<sf::Sprite> rowVec = std::vector<sf::Sprite>();
		for (int col = 0; col < GRID_WIDTH; col++)
		{
			sf::Sprite block = sf::Sprite();
			block.setTexture(gridTexture);
			block.setPosition((float)(MARGIN+col*(BLOCK_SIZE)), (float)(MARGIN+row*(BLOCK_SIZE)));
			rowVec.push_back(block);
			
			switch(layout[row][2*col])
			{
				case LASER_SOURCE_U_RED:
				{
					LaserSource myLaser;
					myLaser.setRotation(270);
					myLaser.setPosition((float)(2*MARGIN+col*(BLOCK_SIZE)), (float)(2*MARGIN+row*(BLOCK_SIZE)));
					myLaser.setColor(sf::Color::Red);
					my_lasers.push_back(myLaser);
					equipMap.insert(std::pair<int, std::shared_ptr<Equipment>>(row*GRID_WIDTH + col, std::make_shared<LaserSource>(myLaser)));
					break;
				}
				case LASER_SOURCE_D_RED:
				{
					LaserSource myLaser;
					myLaser.setRotation(90);
					myLaser.setPosition((float)(2*MARGIN+col*(BLOCK_SIZE)), (float)(2*MARGIN+row*(BLOCK_SIZE)));
					myLaser.setColor(sf::Color::Red);
					my_lasers.push_back(myLaser);
					equipMap.insert(std::pair<int, std::shared_ptr<Equipment>>(row*GRID_WIDTH + col, std::make_shared<LaserSource>(myLaser)));
					break;
				}
				case LASER_SOURCE_L_RED:
				{
					LaserSource myLaser;
					myLaser.setRotation(180);
					myLaser.setPosition((float)(2*MARGIN+col*(BLOCK_SIZE)), (float)(2*MARGIN+row*(BLOCK_SIZE)));
					myLaser.setColor(sf::Color::Red);
					my_lasers.push_back(myLaser);
					equipMap.insert(std::pair<int, std::shared_ptr<Equipment>>(row*GRID_WIDTH + col, std::make_shared<LaserSource>(myLaser)));
					break;
				}
				case LASER_SOURCE_R_RED:
				{
					LaserSource myLaser;
					myLaser.setRotation(0);
					myLaser.setPosition((float)(2*MARGIN+col*(BLOCK_SIZE)), (float)(2*MARGIN+row*(BLOCK_SIZE)));
					myLaser.setColor(sf::Color::Red);
					my_lasers.push_back(myLaser);
					equipMap.insert(std::pair<int, std::shared_ptr<Equipment>>(row*GRID_WIDTH + col, std::make_shared<LaserSource>(myLaser)));
					break;
				}
				case DOOR_U_RED:
				{
					Target myTarget;
					myTarget.setRotation(90);
					myTarget.setPosition((float)(2*MARGIN+col*(BLOCK_SIZE)), (float)(2*MARGIN+row*(BLOCK_SIZE)));
					myTarget.setColor(sf::Color::Red);
					equipMap.insert(std::pair<int, std::shared_ptr<Equipment>>(row*GRID_WIDTH + col, std::make_shared<Target>(myTarget)));
					my_targets.push_back(equipMap[row*GRID_WIDTH + col]);
					break;
				}
				case DOOR_R_RED:
				{
					Target myTarget;
					myTarget.setRotation(180);
					myTarget.setPosition((float)(2*MARGIN+col*(BLOCK_SIZE)), (float)(2*MARGIN+row*(BLOCK_SIZE)));
					myTarget.setColor(sf::Color::Red);
					//my_targets.push_back(std::make_shared<Target>(myTarget));
					equipMap.insert(std::pair<int, std::shared_ptr<Equipment>>(row*GRID_WIDTH + col, std::make_shared<Target>(myTarget)));
					my_targets.push_back(equipMap[row*GRID_WIDTH + col]);
					break;
				}
				case DOOR_D_RED:
				{
					Target myTarget;
					myTarget.setRotation(270);
					myTarget.setPosition((float)(2*MARGIN+col*(BLOCK_SIZE)), (float)(2*MARGIN+row*(BLOCK_SIZE)));
					myTarget.setColor(sf::Color::Red);
					//my_targets.push_back(std::make_shared<Target>(myTarget));
					equipMap.insert(std::pair<int, std::shared_ptr<Equipment>>(row*GRID_WIDTH + col, std::make_shared<Target>(myTarget)));
					my_targets.push_back(equipMap[row*GRID_WIDTH + col]);
					break;
				}
				case DOOR_L_RED:
				{
					Target myTarget;
					myTarget.setRotation(0);
					myTarget.setPosition((float)(2*MARGIN+col*(BLOCK_SIZE)), (float)(2*MARGIN+row*(BLOCK_SIZE)));
					myTarget.setColor(sf::Color::Red);
					//my_targets.push_back(std::make_shared<Target>(myTarget));
					equipMap.insert(std::pair<int, std::shared_ptr<Equipment>>(row*GRID_WIDTH + col, std::make_shared<Target>(myTarget)));
					my_targets.push_back(equipMap[row*GRID_WIDTH + col]);
					break;
				}
			}
			
		}
		gridVec.push_back(rowVec);
	}
}
Пример #2
0
void ToolManagerEdit::update(sf::RenderWindow& window)
{
	
	mouseBounds_.left = (float)sf::Mouse::getPosition(window).x;
	mouseBounds_.top = (float)sf::Mouse::getPosition(window).y;

	// Decide if drag from grid or toolbar
	if(sf::Mouse::isButtonPressed(sf::Mouse::Left))
	{
		if(ToolManagerEdit::getState() == 0)
		{
			//check if the equipments on the toolbar is been selected
			std::map<std::string, std::shared_ptr<Equipment>>::iterator it = equipments_.begin();
			for(; it!= equipments_.end(); it++)
			{
				if((*it).second->getGlobalBounds().intersects(mouseBounds_))
				{
					std::shared_ptr<Equipment> new_equipment;
					((*it).second)->clone(new_equipment);
					ToolManagerEdit::setCopyEquipment(new_equipment);
					ToolManagerEdit::setState(1);
					break;
				}
			}

			//check if the equipment on the grid is selected
			std::map<int, std::shared_ptr<Equipment>>::iterator it_grid = equipments_on_grid_.begin();
			int flag_find = 0;
			for(; it_grid!= equipments_on_grid_.end(); it_grid++)
			{
				if(ToolManagerEdit::getState() == 1)
					break;

				if((*it_grid).second->getGlobalBounds().intersects(mouseBounds_))
				{
					ToolManagerEdit::setCopyEquipment((*it_grid).second);
					ToolManagerEdit::setState(2);
					flag_find = 1;
					break;
				}

			}
			if(flag_find == 1)
			{
				int key = (*it_grid).first;
//				currentScore += (*it_grid).second->cost;
				if((*it_grid).second->isLaserSource())
				{
					std::vector<LaserSource>::iterator it_Laser = my_lasers_.begin();
					for(;it_Laser!= my_lasers_.end(); it_Laser++)
					{
						if((*it_grid).second->getPosition() == (*it_Laser).getPosition())
						{
							my_lasers_.erase(it_Laser);
							break;
						}
					}
				}
				if((*it_grid).second->label == CAPACITOR_R)
				{
					std::vector<std::shared_ptr<Equipment>>::iterator it = my_capacitors_.begin();
					for(;it != my_capacitors_.end(); it++)
					{
						if((*it_grid).second->getPosition() == (*it)->getPosition())
						{
							my_capacitors_.erase(it);
							break;
						}
					}
				}
				equipments_on_grid_.erase(key);
				changeIdx  = key;
			}
		}

		if(ToolManagerEdit::getState() == 1 || ToolManagerEdit::getState() == 2)
		{
			ToolManagerEdit::getCopyEquipment()->setPosition((sf::Vector2f) sf::Mouse::getPosition(window) );
		}

	}

	// Decide if click on equipment availibility block
	if(sf::Mouse::isButtonPressed(sf::Mouse::Left))
	{
		if(ToolManagerEdit::getState() == 0)
		{
			//check if the equipments on the availability is been selected
			for(int i = 0; i != equipments_use_.size(); i++)
			{
				if(equipments_use_[i].getGlobalBounds().intersects(mouseBounds_))
				{
					ToolManagerEdit::setState(3);
					break;
				}
			}
		}

	}


	if(!sf::Mouse::isButtonPressed(sf::Mouse::Left))
	{
		if(ToolManagerEdit::state == 1 || ToolManagerEdit::state == 2)
		{
			unsigned int x = sf::Mouse::getPosition(window).x;
			unsigned int y = sf::Mouse::getPosition(window).y;
			if(x>20&&x<620&&y>20&&y<500)
			{

				int row = (y-MARGIN)/BLOCK_SIZE;
				int col = (x-MARGIN)/BLOCK_SIZE;
				std::shared_ptr<Equipment> new_equipment;
				(ToolManagerEdit::copy_equipment)->clone(new_equipment);
				ToolManagerEdit::equipments_on_grid_.insert(std::pair<int, std::shared_ptr<Equipment>>((row*GRID_WIDTH + col), new_equipment));
				if(ToolManagerEdit::copy_equipment->label == CAPACITOR_R)
				{
					my_capacitors_.push_back(equipments_on_grid_[row*GRID_WIDTH + col]);
					equipments_on_grid_[row*GRID_WIDTH + col]->setPosition((float)(2*MARGIN+col*(BLOCK_SIZE)), (float)(2*MARGIN+row*(BLOCK_SIZE)));
				}
				if(ToolManagerEdit::copy_equipment->isLaserSource())
				{
					LaserSource * newLaser = NULL;
					switch(ToolManagerEdit::copy_equipment->label)
					{
					case '1':
						{
							newLaser = new LaserSource('r');
							break;
						}
					case 'j':
						{
							newLaser = new LaserSource('b');
							break;
						}
					}
					newLaser->setPosition((float)(2*MARGIN+col*(BLOCK_SIZE)), (float)(2*MARGIN+row*(BLOCK_SIZE)));
					newLaser->setRotation(ToolManagerEdit::copy_equipment->getRotation());
					my_lasers_.push_back(*newLaser);
				}
				/*
				if(ToolManagerEdit::copy_equipment->label == CAPACITOR_R  && ToolManagerEdit::state == 1)
				{
					my_capacitors_.push_back(equipments_on_grid_[row*GRID_WIDTH + col]);
				}
				*/
				changeIdx = row*GRID_WIDTH + col;
			}

		}
		if(ToolManagerEdit::state == 3)
		{
			for(int i = 0; i != equipments_use_.size(); i++)
			{
				if(equipments_use_[i].getGlobalBounds().intersects(mouseBounds_))
				{

					unsigned int x = sf::Mouse::getPosition(window).x;
					int idx = (x-1)/60;
					int prevState = equipAvalibility[idx];
					std::map<int, int>::iterator it = equipAvalibility.find(idx);
					if(it != equipAvalibility.end())
						it->second = 1 - prevState;
					equipments_use_[i].setTexture(equipments_texture[2*i+1-prevState]);	
					break;
				}
			}
	
		}
		ToolManagerEdit::setState(0);
					
	}

	if(sf::Mouse::isButtonPressed(sf::Mouse::Right) || ToolManagerEdit::getStateRight() == 1)
	{		
		ToolManagerEdit::setStateRight(1);
		if(!(sf::Mouse::isButtonPressed(sf::Mouse::Right)))
		{
			ToolManagerEdit::setStateRight(0);
		}
		
		if(ToolManagerEdit::getStateRight() == 0)
		{
			std::map<int, std::shared_ptr<Equipment>>::iterator it_on_grid = ToolManagerEdit::equipments_on_grid_.begin();
			for(; it_on_grid!=ToolManagerEdit::equipments_on_grid_.end(); it_on_grid ++)
			{
				if((*it_on_grid).second->getGlobalBounds().intersects(mouseBounds_))
				{	
					(*it_on_grid).second->myRotate_E();
					if((*it_on_grid).second->isLaserSource()) 
					{
						std::vector<LaserSource>::iterator it_Laser = my_lasers_.begin();
						for(;it_Laser!= my_lasers_.end(); it_Laser++)
						{
							if((*it_on_grid).second->getPosition() == (*it_Laser).getPosition())
							{
								(*it_Laser).myRotate_E();
								break;
							}
						}
					}
					changeIdx = (*it_on_grid).first;
				}
			}
		}
	}
}