Here's the methods where I check if the next position requested by the user is feasible. I was just trying to get it working so please excuse the dirty code:
bool SPlayer::nextPosValid(const sf::Vector2f& offset)
{
sf::Vector2f offsettedPos(m_sprite.getPosition().x + offset.x, m_sprite.getPosition().y + offset.y);
//The bounding rectangle of the future player
sf::FloatRect rect(offsettedPos,
sf::Vector2f(m_sprite.getGlobalBounds().width, m_sprite.getGlobalBounds().height));
if (SSceneManager::getWorld().getCurrentCell()->getTileMap().check(offsettedPos, 2))
{
Ataxia::TileMap::Tile tempTile = SSceneManager::getWorld().getCurrentCell()->getTileMap().check2(rect, 2);
if (rect.intersects(tempTile.bounds))
return false;
}
return true;
}
bool check(const sf::Vector2f& pos, int tileType)
{
for (auto& iter : m_tiles)
{
if (iter.tileType == tileType)
{
if (iter.bounds.contains(pos))
return true;
}
}
return false;
}
Tile& check2(const sf::FloatRect& rect, int tileType)
{
for (auto& iter : m_tiles)
{
if (iter.tileType == tileType)
{
if (iter.bounds.intersects(rect))
return iter;
}
}
}
Thanks!