Sorry, took me a while to compile it in code::blocks (Somehow i picked the win32 bins instead of the gcc ones... not a good idea obviously...)
Actually it is working just fine with the attempt i wrote about earlier (see 25th May):
sf::Vector2f PlayerPos = player.player.getPosition();
sf::Vector2i PlayerTile = sf::Vector2i(PlayerPos.x / 32.0f, PlayerPos.y / 32.0f);
if(player.upPressed)
{
float Line = (float)PlayerTile.y * 32.0f + 7.5f;
float Remainder = std::fmod(PlayerPos.x, 32.0f);
int Offset = 0;
if(Remainder < 7.5f)
Offset = -1;
else if(Remainder > 32.0f - 7.5f)
Offset = 1;
if((level.level[PlayerTile.y-1][PlayerTile.x] == 1 || level.level[PlayerTile.y-1][PlayerTile.x+Offset] == 1) && PlayerPos.y < Line)
PlayerPos.y = Line;
}
else if(player.downPressed)
{
float Line = (float)PlayerTile.y * 32.0f + 32.0f - 7.5f;
float Remainder = std::fmod(PlayerPos.x, 32.0f);
int Offset = 0;
if(Remainder < 7.5f)
Offset = -1;
else if(Remainder > 32.0f - 7.5f)
Offset = 1;
if((level.level[PlayerTile.y+1][PlayerTile.x] == 1 || level.level[PlayerTile.y+1][PlayerTile.x+Offset] == 1) && PlayerPos.y > Line)
PlayerPos.y = Line;
}
else if(player.rightPressed)
{
float Line = (float)PlayerTile.x * 32.0f + 32.0f - 7.5f;
float Remainder = std::fmod(PlayerPos.y, 32.0f);
int Offset = 0;
if(Remainder < 7.5f)
Offset = -1;
else if(Remainder > 32.0f - 7.5f)
Offset = 1;
if((level.level[PlayerTile.y][PlayerTile.x+1] == 1 || level.level[PlayerTile.y+Offset][PlayerTile.x+1] == 1)&& PlayerPos.x > Line)
PlayerPos.x = Line;
}
else if(player.leftPressed)
{
float Line = (float)PlayerTile.x * 32.0f + 7.5f;
float Remainder = std::fmod(PlayerPos.y, 32.0f);
int Offset = 0;
if(Remainder < 7.5f)
Offset = -1;
else if(Remainder > 32.0f - 7.5f)
Offset = 1;
if((level.level[PlayerTile.y][PlayerTile.x-1] == 1 || level.level[PlayerTile.y+Offset][PlayerTile.x-1] == 1) && PlayerPos.x < Line)
PlayerPos.x = Line;
}
player.player.setPosition(PlayerPos);
Just put this above the for loop and delete your old collision code for the player in the for loop.
Furthermore you have to call the Move Functions and execute the code which updates the positions of objects relative to the player and the update of the sf::View AFTER the collision stuff but before rendering.
Furthermore make sure to call the move functions and update functions before doing the collision stuff, then execute the code which updated the positions of objects relative to the player and the sf::View. After those steps, in that order, render everything. (In you current code you are first checking for collisions, then call the move functions and then render which would cause some jitter)
That way it is working fine over here.
EDIT: oh an you will have to include the cmath header in main.cpp. If you still have questions regarding the code you can pm me
EDIT2: moreover, you will probably have to fix the enemies... sometimes they are getting stuck because you just negate the velocity (and sometimes they slowly travel through walls because of that and non-fixed timesteps). Additionally, You should kind of "pause" the game when the player is dead or he is done with the level. The way it is now one could finish level1 and then still run into the enemies losing lifes.