source: network-game/common/WorldMap.cpp@ 054b50b

Last change on this file since 054b50b was 7b43385, checked in by dportnoy <dmp1488@…>, 12 years ago

Smooth player movement now works, albeit poorly.

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