[62ee2ce] | 1 | #include "WorldMap.h"
|
---|
[60b77d2] | 2 |
|
---|
[384b7e0] | 3 | #include <string>
|
---|
| 4 | #include <iostream>
|
---|
| 5 | #include <fstream>
|
---|
| 6 | #include <sstream>
|
---|
[f401cac] | 7 | #include <cstdlib>
|
---|
[384b7e0] | 8 |
|
---|
[60b77d2] | 9 | using namespace std;
|
---|
| 10 |
|
---|
[62ee2ce] | 11 | WorldMap::WorldMap(int width, int height)
|
---|
[60b77d2] | 12 | {
|
---|
| 13 | this->width = width;
|
---|
| 14 | this->height = height;
|
---|
| 15 |
|
---|
| 16 | vctMap = new vector<vector<TerrainType>*>(width);
|
---|
[a1a3bd5] | 17 | vctObjects = new vector<vector<ObjectType>*>(width);
|
---|
[60b77d2] | 18 |
|
---|
| 19 | for (int x=0; x<width; x++) {
|
---|
[a1a3bd5] | 20 | vector<TerrainType>* newMapVector = new vector<TerrainType>(height);
|
---|
| 21 | vector<ObjectType>* newObjectVector = new vector<ObjectType>(height);
|
---|
[60b77d2] | 22 |
|
---|
[a1a3bd5] | 23 | for (int y=0; y<height; y++) {
|
---|
| 24 | (*newMapVector)[y] = TERRAIN_NONE;
|
---|
| 25 | (*newObjectVector)[y] = OBJECT_NONE;
|
---|
| 26 | }
|
---|
[60b77d2] | 27 |
|
---|
[a1a3bd5] | 28 | (*vctMap)[x] = newMapVector;
|
---|
| 29 | (*vctObjects)[x] = newObjectVector;
|
---|
[60b77d2] | 30 | }
|
---|
| 31 | }
|
---|
| 32 |
|
---|
[62ee2ce] | 33 | WorldMap::~WorldMap()
|
---|
[60b77d2] | 34 | {
|
---|
[a1a3bd5] | 35 | for (int x=0; x<width; x++) {
|
---|
[60b77d2] | 36 | delete (*vctMap)[x];
|
---|
[a1a3bd5] | 37 | delete (*vctObjects)[x];
|
---|
| 38 | }
|
---|
[60b77d2] | 39 |
|
---|
| 40 | delete vctMap;
|
---|
[a1a3bd5] | 41 | delete vctObjects;
|
---|
[60b77d2] | 42 | }
|
---|
| 43 |
|
---|
[62ee2ce] | 44 | WorldMap::TerrainType WorldMap::getElement(int x, int y)
|
---|
| 45 | {
|
---|
| 46 | return (*(*vctMap)[x])[y];
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | void WorldMap::setElement(int x, int y, TerrainType t)
|
---|
[60b77d2] | 50 | {
|
---|
[7b43385] | 51 | cout << "getting element" << endl;
|
---|
[60b77d2] | 52 | (*(*vctMap)[x])[y] = t;
|
---|
| 53 | }
|
---|
| 54 |
|
---|
[a1a3bd5] | 55 | WorldMap::ObjectType WorldMap::getObject(int x, int y)
|
---|
| 56 | {
|
---|
| 57 | return (*(*vctObjects)[x])[y];
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | void WorldMap::setObject(int x, int y, ObjectType t)
|
---|
| 61 | {
|
---|
| 62 | cout << "getting object" << endl;
|
---|
| 63 | (*(*vctObjects)[x])[y] = t;
|
---|
| 64 | }
|
---|
| 65 |
|
---|
[62ee2ce] | 66 | WorldMap* WorldMap::createDefaultMap()
|
---|
[60b77d2] | 67 | {
|
---|
[62ee2ce] | 68 | WorldMap* m = new WorldMap(12l, 12);
|
---|
[60b77d2] | 69 |
|
---|
[62ee2ce] | 70 | for(int x=0; x<12; x++)
|
---|
[60b77d2] | 71 | {
|
---|
[62ee2ce] | 72 | for(int y=0; y<12; y++)
|
---|
[60b77d2] | 73 | {
|
---|
[62ee2ce] | 74 | if (x ==0 || y == 0 || x == 11 || y == 11)
|
---|
[60b77d2] | 75 | m->setElement(x, y, TERRAIN_OCEAN);
|
---|
| 76 | else
|
---|
| 77 | m->setElement(x, y, TERRAIN_GRASS);
|
---|
[a1a3bd5] | 78 |
|
---|
| 79 | m->setObject(x, y, OBJECT_NONE);
|
---|
[60b77d2] | 80 | }
|
---|
| 81 | }
|
---|
| 82 |
|
---|
[62ee2ce] | 83 | m->setElement(5, 5, TERRAIN_ROCK);
|
---|
| 84 |
|
---|
[60b77d2] | 85 | return m;
|
---|
| 86 | }
|
---|
[384b7e0] | 87 |
|
---|
| 88 | WorldMap* WorldMap::loadMapFromFile(string filename)
|
---|
| 89 | {
|
---|
| 90 | WorldMap* m = new WorldMap(12l, 12);
|
---|
| 91 |
|
---|
[f401cac] | 92 | ifstream file(filename.c_str());
|
---|
[384b7e0] | 93 |
|
---|
| 94 | if (file.is_open())
|
---|
| 95 | {
|
---|
| 96 | string line;
|
---|
| 97 | int width, height;
|
---|
| 98 |
|
---|
| 99 | // read the map dimensions
|
---|
| 100 | getline(file, line);
|
---|
| 101 | if (line.size() > 0)
|
---|
| 102 | {
|
---|
| 103 | istringstream iss(line);
|
---|
| 104 | string token;
|
---|
| 105 | getline(iss, token, 'x');
|
---|
| 106 | width = atoi(token.c_str());
|
---|
| 107 | getline(iss, token, 'x');
|
---|
| 108 | height = atoi(token.c_str());
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | cout << "width: " << width << endl;
|
---|
| 112 | cout << "height: " << height << endl;
|
---|
| 113 |
|
---|
| 114 | // read the map contents
|
---|
| 115 | int row = 0;
|
---|
| 116 | while ( file.good() )
|
---|
| 117 | {
|
---|
| 118 | getline(file, line);
|
---|
| 119 | if (line.size() > 0)
|
---|
| 120 | {
|
---|
| 121 | cout << "line: " << line << endl;
|
---|
| 122 |
|
---|
| 123 | istringstream iss(line);
|
---|
| 124 | string token;
|
---|
| 125 |
|
---|
[a1a3bd5] | 126 | if (row < height) {
|
---|
| 127 | // load terrain
|
---|
| 128 |
|
---|
| 129 | int type;
|
---|
| 130 | TerrainType terrain;
|
---|
| 131 |
|
---|
| 132 | for(int x=0; x<width; x++)
|
---|
| 133 | {
|
---|
| 134 | getline(iss, token, ',');
|
---|
| 135 | cout << "token: " << token << endl;
|
---|
| 136 | type = atoi(token.c_str());
|
---|
| 137 | cout << "type: " << type << endl;
|
---|
| 138 |
|
---|
| 139 | switch(type) {
|
---|
| 140 | case 1:
|
---|
| 141 | terrain = TERRAIN_GRASS;
|
---|
| 142 | break;
|
---|
| 143 | case 2:
|
---|
| 144 | terrain = TERRAIN_OCEAN;
|
---|
| 145 | break;
|
---|
| 146 | case 3:
|
---|
| 147 | terrain = TERRAIN_ROCK;
|
---|
| 148 | break;
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 | cout << "About to set element" << endl;
|
---|
| 152 | cout << "x: " << x << endl;
|
---|
| 153 | cout << "row: " << row << endl;
|
---|
| 154 | m->setElement(x, row, terrain);
|
---|
| 155 | }
|
---|
| 156 | }else {
|
---|
| 157 | // load objects
|
---|
| 158 |
|
---|
| 159 | int x, y, type;
|
---|
| 160 | ObjectType object;
|
---|
| 161 |
|
---|
| 162 | getline(iss, token, ',');
|
---|
| 163 | cout << "token(x): " << token << endl;
|
---|
| 164 | x = atoi(token.c_str());
|
---|
| 165 |
|
---|
[384b7e0] | 166 | getline(iss, token, ',');
|
---|
[a1a3bd5] | 167 | cout << "token(y): " << token << endl;
|
---|
| 168 | y = atoi(token.c_str());
|
---|
| 169 |
|
---|
| 170 | getline(iss, token, ',');
|
---|
| 171 | cout << "token(type): " << token << endl;
|
---|
[384b7e0] | 172 | type = atoi(token.c_str());
|
---|
| 173 |
|
---|
| 174 | switch(type) {
|
---|
[a1a3bd5] | 175 | case 0:
|
---|
| 176 | object = OBJECT_NONE;
|
---|
| 177 | break;
|
---|
[384b7e0] | 178 | case 1:
|
---|
[a1a3bd5] | 179 | object = OBJECT_RED_FLAG;
|
---|
[384b7e0] | 180 | break;
|
---|
| 181 | case 2:
|
---|
[a1a3bd5] | 182 | object = OBJECT_BLUE_FLAG;
|
---|
[384b7e0] | 183 | break;
|
---|
| 184 | }
|
---|
| 185 |
|
---|
[a1a3bd5] | 186 | m->setObject(x, y, object);
|
---|
[384b7e0] | 187 | }
|
---|
| 188 | }
|
---|
| 189 |
|
---|
| 190 | row++;
|
---|
| 191 | }
|
---|
| 192 | file.close();
|
---|
| 193 | }
|
---|
| 194 | else
|
---|
| 195 | cout << "Could not open the file" << endl;
|
---|
| 196 |
|
---|
| 197 | return m;
|
---|
| 198 | }
|
---|