Exemple #1
0
void Player::Update(sf::RenderWindow & App, class Physic & physic)
{
	if(!m_inited)
		Init(physic);


	const sf::Input & _input = App.GetInput();

	physic.GetPlayer(0)->vel = physic.GetPlayer(0)->body->GetLinearVelocity();
	physic.GetPlayer(0)->rotation = physic.GetPlayer(0)->body->GetAngle();

		if(_input.IsKeyDown(sf::Key::A)) { physic.GetPlayer(0)->rotation += -1.0f * App.GetFrameTime(); physic.GetPlayer(0)->body->SetTransform(physic.GetPlayer(0)->body->GetPosition(), physic.GetPlayer(0)->rotation); /*m_rotationDegree -= 3; m_rotation = 3; m_player.Rotate( (float)m_rotation );*/ } // turn LEFT
		if(_input.IsKeyDown(sf::Key::D)) { physic.GetPlayer(0)->rotation += 1.0f * App.GetFrameTime(); physic.GetPlayer(0)->body->SetTransform(physic.GetPlayer(0)->body->GetPosition(), physic.GetPlayer(0)->rotation); /*m_rotationDegree += 3; m_rotation = -3; m_player.Rotate( (float)m_rotation  );*/ } // turn RIGHT

		if(_input.IsKeyDown(sf::Key::W)) { m_V += 2.00  ;    }
		if(_input.IsKeyDown(sf::Key::S)) { m_V -= 1.00  ;    }
		if(_input.IsKeyDown(sf::Key::Space)) { m_V = 0.0 ;  }

		physic.GetPlayer(0)->body->SetAngularVelocity(0.f);

/*		 
			if(m_clock2.GetElapsedTime() > 1.0f)
			{
				if(_input.IsMouseButtonDown(sf::Mouse::Left))
				{
					Shoot(App);
					m_reload = true;
					m_clock2.Reset();
				}
				m_reload = false;
			}
			else
			{
				m_reload = true;
			}
		
*/
//		UpdateBullets(App);

//		m_mousePozVec.x = _input.GetMouseX();
//		m_mousePozVec.y = _input.GetMouseY();

		m_mousePozVec = App.ConvertCoords(App.GetInput().GetMouseX(), App.GetInput().GetMouseY());

		m_xH = m_cannon.GetPosition().x - m_mousePozVec.x;
		m_yH = m_cannon.GetPosition().y - m_mousePozVec.y;
		m_cannonRotation =(float)(atan2(m_yH,m_xH) * 180.f / b2_pi + 180.f);


//		if(physic.GetPlayer(0)->rotation > 359) physic.GetPlayer(0)->rotation = 0;
//		if(physic.GetPlayer(0)->rotation < 0) physic.GetPlayer(0)->rotation = 359;
		

        if( m_V > m_maxPlayerSpeed ) m_V = m_maxPlayerSpeed;
        if( m_V <  m_maxPlayerBackSpeed ) m_V = m_maxPlayerBackSpeed;

		physic.GetPlayer(0)->vel.x = m_V * cos((b2_pi*(physic.GetPlayer(0)->rotation * RADTODEG ))/180.f);
        physic.GetPlayer(0)->vel.y = m_V * sin((b2_pi*(physic.GetPlayer(0)->rotation * RADTODEG ))/180.f);

//		if( m_disVec.x >= 1 || m_disVec.x <= - 1 ) { m_playerPozVec.x += int(m_disVec.x) * App.GetFrameTime(); m_disVec.x = m_disVec.x - int( m_disVec.x ); }
//      if( m_disVec.y >= 1 || m_disVec.y <= - 1 ) { m_playerPozVec.y += int(m_disVec.y) * App.GetFrameTime(); m_disVec.y = m_disVec.y - int( m_disVec.y ); }
		
		physic.GetPlayer(0)->body->SetLinearVelocity(physic.GetPlayer(0)->vel);
		
		m_player.SetPosition( physic.GetPlayer(0)->GetPosition() );
		m_player.SetRotation( -physic.GetPlayer(0)->rotation * RADTODEG );


		m_cannon.SetPosition(m_player.GetPosition());
		m_cannon.SetRotation(-m_cannonRotation);

/*
#pragma region DEBUG INFO
std::ostringstream bufor;
sf::String tdebug;
tdebug.SetFont(LoadContent::GetInstance()->m_font2);
tdebug.SetSize(20);
bufor << "\nFPS = " << _Framerate;
tdebug.SetText(bufor.str());
App.Draw(tdebug);		
#pragma endregion	
*/

		App.Draw(m_player);
		App.Draw(m_cannon);
}
Exemple #2
0
void GUI::MouseTracker::update(const sf::RenderWindow& window, const sf::View& view) {
	sf::Vector2f tmp = window.ConvertCoords(x, y, &view);
	position.x = tmp.x;
	position.y = tmp.y;
}
Exemple #3
0
int GameScreen::update (sf::RenderWindow &app)
{
	int nextScreen = myID;
	float elapsedTime = app.GetFrameTime();

	sf::Event event;
	while (app.GetEvent(event)) // Boucle des évènements en attente
	{
		if (event.Type == sf::Event::Closed)
		{
			return EXIT;
		}
		else if(m_iHandler->testEvent(event, "Quit"))
		{
			return EXIT;
		}
		else if (event.Type == sf::Event::MouseMoved)
		{
			sf::Vector2f mouseCoords = app.ConvertCoords(event.MouseMove.X, event.MouseMove.Y, &ConfigOptions::getView());
			m_cursor.moveOnSquare(BoardAlignedSprite::toSquares(mouseCoords), false);
		}
		else if(m_iHandler->testEvent(event, "Up"))
		{
			sf::Vector2i s = BoardAlignedSprite::toSquares(m_cursor.GetPosition()) + sf::Vector2i(0, -1);
			if(isValid(s))
				m_cursor.moveOnSquare(s, false);
		}
		else if(m_iHandler->testEvent(event, "Left"))
		{
			sf::Vector2i s = BoardAlignedSprite::toSquares(m_cursor.GetPosition()) + sf::Vector2i(-1, 0);
			if(isValid(s))
				m_cursor.moveOnSquare(s, false);
		}
		else if(m_iHandler->testEvent(event, "Down"))
		{
			sf::Vector2i s = BoardAlignedSprite::toSquares(m_cursor.GetPosition()) + sf::Vector2i(0, 1);
			if(isValid(s))
				m_cursor.moveOnSquare(s, false);
		}
		else if(m_iHandler->testEvent(event, "Right"))
		{
			sf::Vector2i s = BoardAlignedSprite::toSquares(m_cursor.GetPosition()) + sf::Vector2i(1, 0);
			if(isValid(s))
				m_cursor.moveOnSquare(s, false);
		}
		else if(playerHasHand())
		{
			if(m_iHandler->testEvent(event, "Save"))
			{
				m_game.saveToFile(BACKUP_FILE);
			}
			else if(m_iHandler->testEvent(event, "Load"))
			{
				m_game.loadFromFile(BACKUP_FILE);
				refreshAll();
			}
			else if (m_iHandler->testEvent(event, "LClick"))
			{
				sf::Vector2f mouseCoords = app.ConvertCoords(event.MouseButton.X, event.MouseButton.Y, &ConfigOptions::getView());
				sf::Vector2i s = BoardAlignedSprite::toSquares(mouseCoords);
				if(isValid(s))
					clickOn(s);
				else if(!m_game.hasStarted())
				{
					PieceType type = m_placementUI.click(mouseCoords);
					if(type == NB_PIECES) //end of turn
						tryAndEndTurn();
					else
						m_selectedType = type;
				}
			}
			if (m_iHandler->testEvent(event, "Choose"))
			{
				sf::Vector2i s = BoardAlignedSprite::toSquares(m_cursor.GetPosition());
				if(isValid(s))
					clickOn(s);
			}
			else if (m_iHandler->testEvent(event, "RClick"))
			{
				sf::Vector2f mouseCoords = app.ConvertCoords(event.MouseButton.X, event.MouseButton.Y, &ConfigOptions::getView());
				sf::Vector2i s = BoardAlignedSprite::toSquares(mouseCoords);
				if(!m_game.hasStarted())
					remove(s);
			}
			else if (m_iHandler->testEvent(event, "EndTurn"))
			{
				tryAndEndTurn();
			}
			else if (event.Type == sf::Event::MouseWheelMoved)
			{
				if(event.MouseWheel.Delta > 0)
				{
					m_selectedType = m_placementUI.goUp();
				}
				else
				{
					m_selectedType = m_placementUI.goDown();
				}
			}
			else if (m_iHandler->testEvent(event, "Rabbit"))
			{
				selectPieceType(RABBIT);
			}
			else if (m_iHandler->testEvent(event, "Cat"))
			{
				selectPieceType(CAT);
			}
			else if (m_iHandler->testEvent(event, "Dog"))
			{
				selectPieceType(DOG);
			}
			else if (m_iHandler->testEvent(event, "Horse"))
			{
				selectPieceType(HORSE);
			}
			else if (m_iHandler->testEvent(event, "Camel"))
			{
				selectPieceType(CAMEL);
			}
			else if (m_iHandler->testEvent(event, "Elephant"))
			{
				selectPieceType(ELEPHANT);
			}
		} //end of if(playerHasHand)
		else if(isOver() && m_victorySign.GetColor().a == 255 && (m_iHandler->testEvent(event, "EndTurn") || m_iHandler->testEvent(event, "Choose") || m_iHandler->testEvent(event, "LClick")))
		{
			//if we do something after the match ends, remove the victory sign
			m_victorySign.SetColor(sf::Color(255,255,255,0));
		}

	} //end of event loop

	m_turnSign.update(elapsedTime);
	m_cursor.update(elapsedTime);
	for(std::map<PiecePtr, PieceSprite>::iterator it = m_pieces.begin(); it != m_pieces.end(); ++it)
		it->second.update(elapsedTime);

	//removing pieces that have finished disappearing
	for (auto it = m_disappearingPieces.begin(); it != m_disappearingPieces.end(); ) //++it done below if necessary
	{
		PiecePtr p = *it;
		if (m_pieces[p].hasDisappeared())
		{
			m_pieces.erase(p);
			it = m_disappearingPieces.erase(it); //gives it the value of the next element, thus no need to increase it again
		}
		else
			++it;
	}

	if(isOver()) //manages the appearance of the victory sign
	{
		int alpha = m_victorySign.GetColor().a;
		if(alpha > 0 && alpha < 255) //0 means it's disappeared already
		{
			alpha += (int) (elapsedTime * VICT_APP_SPD * 255);
			if(alpha > 255)
				alpha = 255;
			m_victorySign.SetColor(sf::Color(255,255,255,alpha));
		}
	}

	return nextScreen;
}