summaryrefslogtreecommitdiff
path: root/Level.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'Level.cpp')
-rw-r--r--Level.cpp38
1 files changed, 35 insertions, 3 deletions
diff --git a/Level.cpp b/Level.cpp
index 8e9def4..6655777 100644
--- a/Level.cpp
+++ b/Level.cpp
@@ -102,8 +102,8 @@ void Level::move() {
player.setY(rect.y);
- // Kill the player if they fall off the screen
- if (player.getY() > SCREEN_HEIGHT) {
+ // Kill the player if they fall off the screen or touch a hazard
+ if (player.getY() > SCREEN_HEIGHT || checkCollision(rect) == 2) {
player.kill();
player.setX(startX);
player.setY(startY);
@@ -147,12 +147,23 @@ void Level::draw(Screen *screen) {
platform[0].image, platform[i].getClip());
}
}
+
+ // Draw each hazard...
+ for (int i = 0; i < numHazards; i++) {
+ // ...if it is visible
+ if (hazard[i].getX() + TILE_SIZE + cameraX >= 0 && hazard[i].getX()
+ + cameraX <= SCREEN_WIDTH) {
+ screen->blit(hazard[i].getX() + cameraX, hazard[i].getY(),
+ hazard[0].image, hazard[i].getClip());
+ }
+ }
}
void Level::loadMap(int LevelNo) {
numPlatforms = 0;
// TODO: Let each Platform have a reference a single tiles image
platform[0].setImage("tiles.png");
+ hazard[0].setImage("tiles.png");
// Open map file
std::ifstream map("level01.map");
@@ -180,6 +191,15 @@ void Level::loadMap(int LevelNo) {
platform[numPlatforms].setClip(0, TILE_SIZE * (type - 1), 0, TILE_SIZE, TILE_SIZE);
numPlatforms++;
+ } else if (type == 3) {
+ // Set x/y-coordinates
+ hazard[numHazards].setX((i % MAP_X) * TILE_SIZE);
+ hazard[numHazards].setY((i / MAP_X) * TILE_SIZE);
+
+ // Set the correct sprite sheet clip
+ hazard[numHazards].setClip(0, TILE_SIZE * (type - 1), 0, TILE_SIZE, TILE_SIZE);
+
+ numHazards++;
} else if (type == 'P') {
// Set the player starting position
startX = (i % MAP_X) * TILE_SIZE;
@@ -193,8 +213,11 @@ void Level::loadMap(int LevelNo) {
/* Return values:
0: No collision
- 1: Platform */
+ 1: Platform
+ 2: Hazard */
int Level::checkCollision(SDL_Rect r) {
+ // TODO: Try to avoid separate loops for each type of MapObject
+
// Loop through each platform
for (int i = 0; i < numPlatforms; i++) {
// Check for collisions in the x-axis ...
@@ -204,5 +227,14 @@ int Level::checkCollision(SDL_Rect r) {
return 1;
}
+ // Loop through each hazard
+ for (int i = 0; i < numHazards; i++) {
+ // Check for collisions in the x-axis ...
+ if (r.x > hazard[i].getX() - r.w && r.x < hazard[i].getX() + TILE_SIZE)
+ // ... if found, check for collisions in the y-axis
+ if (r.y > hazard[i].getY() - r.h && r.y < hazard[i].getY() + TILE_SIZE)
+ return 2;
+ }
+
return 0;
}