source: network-game/common/WorldMap.cpp@ 7efed11

Last change on this file since 7efed11 was a1a3bd5, checked in by dportnoy <dmp1488@…>, 11 years ago

Made client changes for smooth player movement, changed the player move method to check the map for obstacles and return a bool indicating success or failure.

  • Property mode set to 100644
File size: 4.5 KB
Line 
1#include "WorldMap.h"
2
3#include <string>
4#include <iostream>
5#include <fstream>
6#include <sstream>
7#include <cstdlib>
8
9using namespace std;
10
11WorldMap::WorldMap(int width, int height)
12{
13 this->width = width;
14 this->height = height;
15
16 vctMap = new vector<vector<TerrainType>*>(width);
17 vctObjects = new vector<vector<ObjectType>*>(width);
18
19 for (int x=0; x<width; x++) {
20 vector<TerrainType>* newMapVector = new vector<TerrainType>(height);
21 vector<ObjectType>* newObjectVector = new vector<ObjectType>(height);
22
23 for (int y=0; y<height; y++) {
24 (*newMapVector)[y] = TERRAIN_NONE;
25 (*newObjectVector)[y] = OBJECT_NONE;
26 }
27
28 (*vctMap)[x] = newMapVector;
29 (*vctObjects)[x] = newObjectVector;
30 }
31}
32
33WorldMap::~WorldMap()
34{
35 for (int x=0; x<width; x++) {
36 delete (*vctMap)[x];
37 delete (*vctObjects)[x];
38 }
39
40 delete vctMap;
41 delete vctObjects;
42}
43
44WorldMap::TerrainType WorldMap::getElement(int x, int y)
45{
46 return (*(*vctMap)[x])[y];
47}
48
49void WorldMap::setElement(int x, int y, TerrainType t)
50{
51 cout << "getting element" << endl;
52 (*(*vctMap)[x])[y] = t;
53}
54
55WorldMap::ObjectType WorldMap::getObject(int x, int y)
56{
57 return (*(*vctObjects)[x])[y];
58}
59
60void WorldMap::setObject(int x, int y, ObjectType t)
61{
62 cout << "getting object" << endl;
63 (*(*vctObjects)[x])[y] = t;
64}
65
66WorldMap* WorldMap::createDefaultMap()
67{
68 WorldMap* m = new WorldMap(12l, 12);
69
70 for(int x=0; x<12; x++)
71 {
72 for(int y=0; y<12; y++)
73 {
74 if (x ==0 || y == 0 || x == 11 || y == 11)
75 m->setElement(x, y, TERRAIN_OCEAN);
76 else
77 m->setElement(x, y, TERRAIN_GRASS);
78
79 m->setObject(x, y, OBJECT_NONE);
80 }
81 }
82
83 m->setElement(5, 5, TERRAIN_ROCK);
84
85 return m;
86}
87
88WorldMap* WorldMap::loadMapFromFile(string filename)
89{
90 WorldMap* m = new WorldMap(12l, 12);
91
92 ifstream file(filename.c_str());
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
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
166 getline(iss, token, ',');
167 cout << "token(y): " << token << endl;
168 y = atoi(token.c_str());
169
170 getline(iss, token, ',');
171 cout << "token(type): " << token << endl;
172 type = atoi(token.c_str());
173
174 switch(type) {
175 case 0:
176 object = OBJECT_NONE;
177 break;
178 case 1:
179 object = OBJECT_RED_FLAG;
180 break;
181 case 2:
182 object = OBJECT_BLUE_FLAG;
183 break;
184 }
185
186 m->setObject(x, y, object);
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}
Note: See TracBrowser for help on using the repository browser.