summaryrefslogtreecommitdiff
path: root/Level.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'Level.cpp')
-rw-r--r--Level.cpp51
1 files changed, 51 insertions, 0 deletions
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 <iostream>
+#include <fstream>
#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