[62ee2ce] | 1 | #include "WorldMap.h"
|
---|
[60b77d2] | 2 |
|
---|
[384b7e0] | 3 | #include <string>
|
---|
| 4 | #include <iostream>
|
---|
| 5 | #include <fstream>
|
---|
| 6 | #include <sstream>
|
---|
| 7 |
|
---|
[60b77d2] | 8 | using namespace std;
|
---|
| 9 |
|
---|
[62ee2ce] | 10 | WorldMap::WorldMap(int width, int height)
|
---|
[60b77d2] | 11 | {
|
---|
| 12 | this->width = width;
|
---|
| 13 | this->height = height;
|
---|
| 14 |
|
---|
| 15 | vctMap = new vector<vector<TerrainType>*>(width);
|
---|
| 16 |
|
---|
| 17 | for (int x=0; x<width; x++) {
|
---|
| 18 | vector<TerrainType>* newVector = new vector<TerrainType>(height);
|
---|
| 19 |
|
---|
| 20 | for (int y=0; y<height; y++)
|
---|
| 21 | (*newVector)[y] = TERRAIN_NONE;
|
---|
| 22 |
|
---|
| 23 | (*vctMap)[x] = newVector;
|
---|
| 24 | }
|
---|
| 25 | }
|
---|
| 26 |
|
---|
[62ee2ce] | 27 | WorldMap::~WorldMap()
|
---|
[60b77d2] | 28 | {
|
---|
| 29 | for (int x=0; x<width; x++)
|
---|
| 30 | delete (*vctMap)[x];
|
---|
| 31 |
|
---|
| 32 | delete vctMap;
|
---|
| 33 | }
|
---|
| 34 |
|
---|
[62ee2ce] | 35 | WorldMap::TerrainType WorldMap::getElement(int x, int y)
|
---|
| 36 | {
|
---|
| 37 | return (*(*vctMap)[x])[y];
|
---|
| 38 | }
|
---|
| 39 |
|
---|
| 40 | void WorldMap::setElement(int x, int y, TerrainType t)
|
---|
[60b77d2] | 41 | {
|
---|
| 42 | (*(*vctMap)[x])[y] = t;
|
---|
| 43 | }
|
---|
| 44 |
|
---|
[62ee2ce] | 45 | WorldMap* WorldMap::createDefaultMap()
|
---|
[60b77d2] | 46 | {
|
---|
[62ee2ce] | 47 | WorldMap* m = new WorldMap(12l, 12);
|
---|
[60b77d2] | 48 |
|
---|
[62ee2ce] | 49 | for(int x=0; x<12; x++)
|
---|
[60b77d2] | 50 | {
|
---|
[62ee2ce] | 51 | for(int y=0; y<12; y++)
|
---|
[60b77d2] | 52 | {
|
---|
[62ee2ce] | 53 | if (x ==0 || y == 0 || x == 11 || y == 11)
|
---|
[60b77d2] | 54 | m->setElement(x, y, TERRAIN_OCEAN);
|
---|
| 55 | else
|
---|
| 56 | m->setElement(x, y, TERRAIN_GRASS);
|
---|
| 57 | }
|
---|
| 58 | }
|
---|
| 59 |
|
---|
[62ee2ce] | 60 | m->setElement(5, 5, TERRAIN_ROCK);
|
---|
| 61 |
|
---|
[60b77d2] | 62 | return m;
|
---|
| 63 | }
|
---|
[384b7e0] | 64 |
|
---|
| 65 | WorldMap* WorldMap::loadMapFromFile(string filename)
|
---|
| 66 | {
|
---|
| 67 | WorldMap* m = new WorldMap(12l, 12);
|
---|
| 68 |
|
---|
| 69 | ifstream file(filename);
|
---|
| 70 |
|
---|
| 71 | if (file.is_open())
|
---|
| 72 | {
|
---|
| 73 | string line;
|
---|
| 74 | int width, height;
|
---|
| 75 |
|
---|
| 76 | // read the map dimensions
|
---|
| 77 | getline(file, line);
|
---|
| 78 | if (line.size() > 0)
|
---|
| 79 | {
|
---|
| 80 | istringstream iss(line);
|
---|
| 81 | string token;
|
---|
| 82 | getline(iss, token, 'x');
|
---|
| 83 | width = atoi(token.c_str());
|
---|
| 84 | getline(iss, token, 'x');
|
---|
| 85 | height = atoi(token.c_str());
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | cout << "width: " << width << endl;
|
---|
| 89 | cout << "height: " << height << endl;
|
---|
| 90 |
|
---|
| 91 | // read the map contents
|
---|
| 92 | int row = 0;
|
---|
| 93 | while ( file.good() )
|
---|
| 94 | {
|
---|
| 95 | getline(file, line);
|
---|
| 96 | if (line.size() > 0)
|
---|
| 97 | {
|
---|
| 98 | cout << "line: " << line << endl;
|
---|
| 99 |
|
---|
| 100 | istringstream iss(line);
|
---|
| 101 | string token;
|
---|
| 102 | int type;
|
---|
| 103 | TerrainType terrain;
|
---|
| 104 |
|
---|
| 105 | for(int x=0; x<width; x++)
|
---|
| 106 | {
|
---|
| 107 | getline(iss, token, ',');
|
---|
| 108 | cout << "token: " << token << endl;
|
---|
| 109 | type = atoi(token.c_str());
|
---|
| 110 | cout << "type: " << type << endl;
|
---|
| 111 |
|
---|
| 112 | switch(type) {
|
---|
| 113 | case 1:
|
---|
| 114 | terrain = TERRAIN_GRASS;
|
---|
| 115 | break;
|
---|
| 116 | case 2:
|
---|
| 117 | terrain = TERRAIN_OCEAN;
|
---|
| 118 | break;
|
---|
| 119 | case 3:
|
---|
| 120 | terrain = TERRAIN_ROCK;
|
---|
| 121 | break;
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | cout << "About to set element" << endl;
|
---|
| 125 | cout << "x: " << x << endl;
|
---|
| 126 | cout << "row: " << row << endl;
|
---|
| 127 | m->setElement(x, row, terrain);
|
---|
| 128 | }
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | row++;
|
---|
| 132 | }
|
---|
| 133 | file.close();
|
---|
| 134 | }
|
---|
| 135 | else
|
---|
| 136 | cout << "Could not open the file" << endl;
|
---|
| 137 |
|
---|
| 138 | return m;
|
---|
| 139 | }
|
---|