1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
|
void Player::CollisionDetectionMap( sf::RenderWindow& window, Map& map)
{
//check for slopes(only if moving down)
if( yVel > 0 ){
int tsx, tsy; // slope tile coordinates
int sx = xPos + (PLAYERWIDTH>>1) + xVel; // slope check point x coordinated
if( CollisionSlope(sx, (yPos + PLAYERHEIGHT), tsx, tsy, map) ){ // we entered a slope
xPos += xVel; // move on
//y has been set by CollisionSlope
UnlockJump(); //we hit the ground - player may jump again
yVel = 1; // test against the ground again in the text frame
slope_prevTileX = tsx;
slope_prevTileY = tsy;
return;
}
else{ // were not on a slope this frame - check if we left a slope
//-1...we diddnt move from slope to slope
//0...we left a slope after moving down
//1...we left a slope after moving up
int what = -1;
if(map.ColMapXY( slope_prevTileX, slope_prevTileY) == Tile::t_slopeRight){
if( xVel > 0 ) // sloperight + xvel > 0 = we left a slope after moving up the slope
what = 0;
else
what = 1; // we left after moving down the slope
}
else if(map.ColMapXY(slope_prevTileX, slope_prevTileY) == Tile::t_slopeLeft){
if( xVel < 0 ) // sloperight + xvel > 0 == we left a slope after moving up the slope
what = 0;
else
what = 1; // we left after moving down the slope
}
if( what!= 1){ //if we left a slope and now are on a slope
int sy;
if( what == 1){
yPos = tsy*TILESIZE - PLAYERHEIGHT - 1;// move y to top of the slope tile
sy = yPos + PLAYERHEIGHT;
}
else{
yPos = (tsy+1) * TILESIZE - PLAYERHEIGHT -1; //move y to the bottom of the slope tile
sy = yPos + PLAYERHEIGHT + TILESIZE;
}
//Check for slopes on the new position
if( CollisionSlope(sx, sy, tsx, tsy, map ) ){ // slope on new pos (entered a slope after leaving a slope)
xPos += xVel;
UnlockJump();
yVel = 1;
slope_prevTileX = tsx;
slope_prevTileY = tsy;
return;
}
}
}
}
//no slope collisions were found ->check for collisions with the map
//Check for collisions with the map
int tilecoord;
//x axis
if(xVel > 0){ //if moving right
if( CollisionVer(xPos + xVel + PLAYERWIDTH, yPos, tilecoord, map)){ //Collision on the right side of player
xPos = tilecoord*TILESIZE -PLAYERWIDTH - 1;
}// Move to the left edge of the tile
else{ // no collision
xPos += xVel;
}
}
else if( xVel < 0){ //if moving left
if( CollisionVer(xPos + xVel, yPos, tilecoord, map)) //Collision on the left side of player
xPos = (tilecoord+1)*TILESIZE+1; //move to the right edge of the tile
else // no collision
xPos += xVel;
}
//y axis
if( yVel < 0 ){ //moving up
if( CollisionHor_Up( xPos, (yPos + yVel), tilecoord, map)){ //Collision on the top of player
yPos = (tilecoord+1) *20 +1;//Set y to bottom of the above tile
yVel = 0; // stop moving the player
}
else{ // no collision
yPos += yVel;
yVel += GRAVITATION;
}
}
else{ //moving down or on ground
if( CollisionHor_Down( xPos, (yPos + yVel + PLAYERHEIGHT),
tilecoord, map)){// on ground
yPos = (tilecoord*TILESIZE) -PLAYERHEIGHT -1; //yPos = top of tile below
yVel = 1; //1-> we test against the ground againt in the next frame
//0-> we tvest against the ground in the next+1 frame
UnlockJump();
}
else{ //falling or in air
yPos += yVel;
yVel += GRAVITATION;
if( yVel >= TILESIZE-1) //might fall through a tile if speed is higher than tile
yVel = TILESIZE -1;
lockJump = true; // cannot jump while falling
}
}
slope_prevTileX = (xPos + (PLAYERWIDTH>>1)) / TILESIZE;
slope_prevTileY = (yPos + PLAYERHEIGHT) / TILESIZE;
}
|