// game.cpp
void Game::update(sf::Time deltaTime)
{
//Player movement:
mPlayer.update(deltaTime, *this);
// player.cpp
void Player::update(sf::Time deltaTime, const Game &game)
{
sf::Vector2f movement(0.f, 0.f);
float difference = 0.0f;
int currentRoom = game.getCurrentRoom();
/////////////////////////
/// X-Axis Movement ///
/////////////////////////
if (mIsMovingLeft)
movement.x -= Speed;
if (mIsMovingRight)
movement.x += Speed;
mBody.move(movement.x * deltaTime.asSeconds(), 0);
/// X-Axis Player-Wall Collision ///
sf::FloatRect currPlayerRect = mBody.getGlobalBounds();
for (unsigned int i = 0; i < game.getRooms()[currentRoom].getInnerWalls().size(); i++)
{
if (mBody.getGlobalBounds().intersects(game.getRooms()[currentRoom].getInnerWalls()[i].getGlobalBounds()))
{
if (mIsMovingLeft)
{
difference = game.getRooms()[currentRoom].getInnerWalls()[i].getGlobalBounds().left
+ game.getRooms()[currentRoom].getInnerWalls()[i].getGlobalBounds().width
- currPlayerRect.left;
mBody.move(difference, 0);
}
else if (mIsMovingRight)
{
difference = currPlayerRect.left + currPlayerRect.width
- game.getRooms()[currentRoom].getInnerWalls()[i].getGlobalBounds().left;
mBody.move(-difference, 0);
}
}
}
/////////////////////////
/// Y-Axis Movement ///
/////////////////////////
if (mIsMovingUp)
movement.y -= Speed;
if (mIsMovingDown)
movement.y += Speed;
mBody.move(0, movement.y * deltaTime.asSeconds());
/// Y-Axis Player-Wall Collision ///
currPlayerRect = mBody.getGlobalBounds();
for (unsigned int i = 0; i < game.getRooms()[currentRoom].getInnerWalls().size(); i++)
{
if (mBody.getGlobalBounds().intersects(game.getRooms()[currentRoom].getInnerWalls()[i].getGlobalBounds()))
{
if (mIsMovingUp)
{
difference = game.getRooms()[currentRoom].getInnerWalls()[i].getGlobalBounds().top
+ game.getRooms()[currentRoom].getInnerWalls()[i].getGlobalBounds().height
- currPlayerRect.top;
mBody.move(0, difference);
}
else if (mIsMovingDown)
{
difference = currPlayerRect.top + currPlayerRect.height
- game.getRooms()[currentRoom].getInnerWalls()[i].getGlobalBounds().top;
mBody.move(0, -difference);
}
}
}
}