From 61e47d523653e7a151d8ab17d6dced5864b5d280 Mon Sep 17 00:00:00 2001 From: Luke Bratch Date: Tue, 21 Dec 2010 18:39:05 +0000 Subject: Level: Implement hazards --- Level.cpp | 38 +++++++++++++++++++++++++++++++++++--- Level.h | 5 +++++ 2 files changed, 40 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; } diff --git a/Level.h b/Level.h index 0a38029..b212c67 100644 --- a/Level.h +++ b/Level.h @@ -4,6 +4,7 @@ #include "Player.h" #include "Screen.h" #include "Platform.h" +#include "Hazard.h" class Level { public: @@ -29,6 +30,10 @@ class Level { int numPlatforms; Platform platform[576]; + // Hazards + int numHazards; + Hazard hazard[576]; + // Reads in the map file void loadMap(int levelNo); -- cgit v1.2.3