From e06ab2415b39fdffe3cc86939b45a464ad4dc541 Mon Sep 17 00:00:00 2001 From: Luke Bratch Date: Fri, 3 Dec 2010 14:36:52 +0000 Subject: Level: Make the camera nicer --- Level.cpp | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) (limited to 'Level.cpp') 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; -- cgit v1.2.3