void Application::Update(sf::Time time)
{
mPlayer->Update(time);
if(!mMap->CollisionWith(*mPlayer))
mPlayer->move(mPlayer->GetMovement());
}
Player::~Player()
{
for(unsigned int i = 0; i < mAnims.size(); i++)
delete mAnims[i];
}
void Player::SetSpeed(float speed)
{
mSpeed = speed;
}
const int Player::GetDirection()
{
return mDirection;
}
const sf::FloatRect Player::GetBoundingBox()
{
return mBoundingBox;
}
const sf::Vector2f Player::GetMovement()
{
return mMovement;
}
void Player::Update(sf::Time time)
{
mMovement = sf::Vector2f(0.0f, 0.0f);
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
{
mCurrentAnim = mAnims[0];
mMovement.y -= mSpeed;
mNotPressed = false;
mDirection = UP;
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
{
mCurrentAnim = mAnims[1];
mMovement.y += mSpeed;
mNotPressed = false;
mDirection = DOWN;
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
{
mCurrentAnim = mAnims[2];
mMovement.x -= mSpeed;
mNotPressed = false;
mDirection = LEFT;
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
{
mCurrentAnim = mAnims[3];
mMovement.x += mSpeed;
mNotPressed = false;
mDirection = RIGHT;
}
mMovement *= time.asSeconds();
this->StartAnimation(mCurrentAnim);
mBoundingBox = sf::FloatRect(this->getPosition().x, this->getPosition().y + (getGlobalBounds().height - 40), 40, 40);
if(mNotPressed)
this->StopAnimation();
mNotPressed = true;
Entity::Update(time);
}
bool Map::CollisionWith(Player &player)
{
sf::FloatRect box = player.GetBoundingBox();
sf::Vector2f movement = player.GetMovement();
int topLeftX, topLeftY, topRightX, topRightY, bottomLeftX, bottomLeftY, bottomRightX, bottomRightY;
const int direction = player.GetDirection();
topLeftX = (box.left + movement.x) / TILE_SIZE;
topLeftY = (box.top + movement.y) / TILE_SIZE;
topRightX = (box.left + movement.x + TILE_SIZE - 1) / TILE_SIZE;
topRightY = (box.top + movement.y) / TILE_SIZE;
bottomLeftX = (box.left + movement.x) / TILE_SIZE;
bottomLeftY = (box.top + movement.y + TILE_SIZE - 1) / TILE_SIZE;
bottomRightX = (box.left + movement.x + TILE_SIZE - 1) / TILE_SIZE;
bottomRightY = (box.top + movement.y + TILE_SIZE - 1) / TILE_SIZE;
switch(direction)
{
// Haut
case 0:
if(mCases[topLeftY][topLeftX]->GetType() < 2 || mCases[topRightY][topRightX]->GetType() < 2)
{
player.setPosition(player.getPosition().x, mCases[topLeftY][topLeftX]->GetPosition().y + (box.height - (player.getGlobalBounds().height - 40)));
return true;
}
break;
// Bas
case 1:
if(mCases[bottomLeftY][bottomLeftX]->GetType() < 2 || mCases[bottomRightY][bottomRightX]->GetType() < 2)
{
player.setPosition(player.getPosition().x, mCases[bottomLeftY][bottomLeftX]->GetPosition().y - player.getGlobalBounds().height);
return true;
}
break;
// Left
case 2:
if(mCases[topLeftY][topLeftX]->GetType() < 2 || mCases[bottomLeftY][bottomLeftX]->GetType() < 2)
{
player.setPosition(mCases[topLeftY][topLeftX]->GetPosition().x + box.width, player.getPosition().y);
return true;
}
break;
// Right
case 3:
if(mCases[topRightY][topRightX]->GetType() < 2 || mCases[bottomRightY][bottomRightX]->GetType() < 2)
{
player.setPosition(mCases[topRightY][topRightX]->GetPosition().x - box.width, player.getPosition().y);
return true;
}
break;
}
return false;
}