From 7e046277d6a04e006d9653eda0a2b0990a454c2d Mon Sep 17 00:00:00 2001 From: Luke Bratch Date: Fri, 26 Nov 2010 16:41:46 +0000 Subject: Level: Implement horizontally scrolling levels/camera --- Level.cpp | 17 +++++++++++++++-- Level.h | 2 ++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/Level.cpp b/Level.cpp index ac860b3..7da0bf4 100644 --- a/Level.cpp +++ b/Level.cpp @@ -40,6 +40,8 @@ void Level::changeLevel(int levelNo) { // Read the map file (all level/player init should be done here, not above) loadMap(levelNo); + + cameraX = 0; } void Level::move() { @@ -79,11 +81,22 @@ void Level::draw(Screen *screen) { screen->blit(player.getX(), player.getY(), player.image, player.getClip()); + // Scroll level + if (player.getX() == 0 || player.getX() >= SCREEN_WIDTH - MARCUS_WIDTH - 10) { + if (player.getXVel() < 0 || player.getXVel() > 0) + cameraX -= player.getXVel(); + } + + // Don't let the camera move to before the start of the level + if (cameraX > 0) + cameraX = 0; + // Draw each platform... for (int i = 0; i < numPlatforms; i++) { // ...if it is visible - if (platform[i].getX() + TILE_SIZE >= 0 && platform[i].getX() <= SCREEN_WIDTH) { - screen->blit(platform[i].getX(), platform[i].getY(), + if (platform[i].getX() + TILE_SIZE + cameraX >= 0 && platform[i].getX() + + cameraX <= SCREEN_WIDTH) { + screen->blit(platform[i].getX() + cameraX, platform[i].getY(), platform[0].image, platform[i].getClip()); } } diff --git a/Level.h b/Level.h index 774164e..b6ca8c6 100644 --- a/Level.h +++ b/Level.h @@ -31,6 +31,8 @@ class Level { // Reads in the map file void loadMap(int levelNo); + + int cameraX; }; #endif -- cgit v1.2.3