From b5483c66aebec91d5354b859f365b8b60b1d4819 Mon Sep 17 00:00:00 2001 From: Luke Bratch Date: Fri, 26 Nov 2010 16:15:18 +0000 Subject: Implement map reading and add tiles sprite sheet --- Level.cpp | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) (limited to 'Level.cpp') diff --git a/Level.cpp b/Level.cpp index c70181a..ac860b3 100644 --- a/Level.cpp +++ b/Level.cpp @@ -1,5 +1,6 @@ #include "Level.h" #include +#include #include "Monster.h" #include "Pickup.h" @@ -36,6 +37,9 @@ void Level::changeLevel(int levelNo) { player.setClip(ORIENT_BACK, MARCUS_WIDTH * 1, 0, MARCUS_WIDTH, MARCUS_HEIGHT); player.setClip(ORIENT_RIGHT, MARCUS_WIDTH * 2, 0, MARCUS_WIDTH, MARCUS_HEIGHT); player.setClip(ORIENT_LEFT, MARCUS_WIDTH * 3, 0, MARCUS_WIDTH, MARCUS_HEIGHT); + + // Read the map file (all level/player init should be done here, not above) + loadMap(levelNo); } void Level::move() { @@ -74,4 +78,51 @@ void Level::draw(Screen *screen) { screen->blit(0, 0, background); screen->blit(player.getX(), player.getY(), player.image, player.getClip()); + + // 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(), + platform[0].image, platform[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"); + + // Open map file + std::ifstream map("level01.map"); + + if (!map) + std::cout << "map is NULL.\n"; + + // Read each tile... + for (int i = 0; i < MAP_X * MAP_Y; i++) { + int type = -1; + + map >> type; + + if (map.fail()) { + std::cout << "map.fail().\n"; + } + + // ...and register it as a platform if it's a platform + if (type == 1 || type == 2) { + // Set x/y-coordinates + platform[numPlatforms].setX((i % MAP_X) * TILE_SIZE); + platform[numPlatforms].setY((i / MAP_X) * TILE_SIZE); + + // Set the correct sprite sheet clip + platform[numPlatforms].setClip(0, TILE_SIZE * (type - 1), 0, TILE_SIZE, TILE_SIZE); + + numPlatforms++; + } + } + + // Close map file + map.close(); +} \ No newline at end of file -- cgit v1.2.3