#include "WorldMap.h" #include #include #include #include #include using namespace std; WorldMap::WorldMap(int width, int height) { this->width = width; this->height = height; vctMap = new vector*>(width); vctObjects = new vector*>(width); for (int x=0; x* newMapVector = new vector(height); vector* newObjectVector = new vector(height); for (int y=0; ysetElement(x, y, TERRAIN_OCEAN); else m->setElement(x, y, TERRAIN_GRASS); m->setObject(x, y, OBJECT_NONE); } } m->setElement(5, 5, TERRAIN_ROCK); return m; } WorldMap* WorldMap::loadMapFromFile(string filename) { WorldMap* m = new WorldMap(12l, 12); ifstream file(filename.c_str()); if (file.is_open()) { string line; int width, height; // read the map dimensions getline(file, line); if (line.size() > 0) { istringstream iss(line); string token; getline(iss, token, 'x'); width = atoi(token.c_str()); getline(iss, token, 'x'); height = atoi(token.c_str()); } cout << "width: " << width << endl; cout << "height: " << height << endl; // read the map contents int row = 0; while ( file.good() ) { getline(file, line); if (line.size() > 0) { cout << "line: " << line << endl; istringstream iss(line); string token; if (row < height) { // load terrain int type; TerrainType terrain; for(int x=0; xsetElement(x, row, terrain); } }else { // load objects int x, y, type; ObjectType object; getline(iss, token, ','); cout << "token(x): " << token << endl; x = atoi(token.c_str()); getline(iss, token, ','); cout << "token(y): " << token << endl; y = atoi(token.c_str()); getline(iss, token, ','); cout << "token(type): " << token << endl; type = atoi(token.c_str()); switch(type) { case 0: object = OBJECT_NONE; break; case 1: object = OBJECT_BLUE_FLAG; break; case 2: object = OBJECT_RED_FLAG; break; } m->setObject(x, y, object); } } row++; } file.close(); } else cout << "Could not open the file" << endl; return m; }