diff options
author | Luke Bratch <l_bratch@yahoo.co.uk> | 2010-12-03 14:36:52 +0000 |
---|---|---|
committer | Luke Bratch <l_bratch@yahoo.co.uk> | 2010-12-03 14:36:52 +0000 |
commit | e06ab2415b39fdffe3cc86939b45a464ad4dc541 (patch) | |
tree | 0bc8e3c5a417ad34cf5da92db872729caff88469 | |
parent | ff9f965f9290d5e9985c695556c3164a6a751e75 (diff) |
Level: Make the camera nicer
-rw-r--r-- | Level.cpp | 21 |
1 files changed, 13 insertions, 8 deletions
@@ -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; |