summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Level.cpp21
1 files changed, 13 insertions, 8 deletions
diff --git a/Level.cpp b/Level.cpp
index 11cb3d4..b67ed2c 100644
--- a/Level.cpp
+++ b/Level.cpp
@@ -71,7 +71,7 @@ void Level::move() {
}
}
- if (rect.x >= 0 && rect.x <= SCREEN_WIDTH - rect.w - 32)
+ if (rect.x >= 0)
player.setX(rect.x);
// Player y-axis movement
@@ -107,18 +107,23 @@ void Level::draw(Screen *screen) {
// Blit background
screen->blit(0, 0, background);
- screen->blit(player.getX(), player.getY(), player.image, player.getClip());
-
- // Scroll level
- if (player.getX() < -player.getXVel() || player.getX() >= SCREEN_WIDTH
- - player.getClip()->w - 32 - MOVE_AMOUNT)
- if (player.getXVel() < 0 || player.getXVel() > 0)
+ // If player is in the middle of the screen ...
+ if (player.getX() + cameraX > SCREEN_WIDTH / 2 - player.getClip()->w / 2 + player.getXVel()
+ && player.getX() + cameraX < SCREEN_WIDTH / 2 + player.getClip()->w / 2 + player.getXVel()) {
+ // ... and is either moving left, or moving right but not past the end of the map ...
+ if (player.getXVel() < 0 || (player.getXVel() > 0 && -cameraX + SCREEN_WIDTH < MAP_X * TILE_SIZE)) {
+ // ... then scroll the level
cameraX -= player.getXVel();
+ }
+ }
// Don't let the camera move to before the start of the level
if (cameraX > 0)
cameraX = 0;
+ // Blit player
+ screen->blit(player.getX() + cameraX, player.getY(), player.image, player.getClip());
+
// Draw each platform...
for (int i = 0; i < numPlatforms; i++) {
// ...if it is visible
@@ -176,7 +181,7 @@ bool Level::checkCollision(SDL_Rect r) {
// Loop through each platform
for (int i = 0; i < numPlatforms; i++) {
// Check for collisions in the x-axis ...
- if (r.x - cameraX > platform[i].getX() - r.w && r.x - cameraX < platform[i].getX() + TILE_SIZE)
+ if (r.x > platform[i].getX() - r.w && r.x < platform[i].getX() + TILE_SIZE)
// ... if found, check for collisions in the y-axis
if (r.y > platform[i].getY() - r.h && r.y < platform[i].getY() + TILE_SIZE)
return true;