source: network-game/common/WorldMap.cpp@ 60940f8

Last change on this file since 60940f8 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
RevLine 
[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]9using namespace std;
10
[62ee2ce]11WorldMap::WorldMap(int width, int height)
[60b77d2]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
[62ee2ce]28WorldMap::~WorldMap()
[60b77d2]29{
30 for (int x=0; x<width; x++)
31 delete (*vctMap)[x];
32
33 delete vctMap;
34}
35
[62ee2ce]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)
[60b77d2]42{
[7b43385]43 cout << "getting element" << endl;
[60b77d2]44 (*(*vctMap)[x])[y] = t;
45}
46
[62ee2ce]47WorldMap* WorldMap::createDefaultMap()
[60b77d2]48{
[62ee2ce]49 WorldMap* m = new WorldMap(12l, 12);
[60b77d2]50
[62ee2ce]51 for(int x=0; x<12; x++)
[60b77d2]52 {
[62ee2ce]53 for(int y=0; y<12; y++)
[60b77d2]54 {
[62ee2ce]55 if (x ==0 || y == 0 || x == 11 || y == 11)
[60b77d2]56 m->setElement(x, y, TERRAIN_OCEAN);
57 else
58 m->setElement(x, y, TERRAIN_GRASS);
59 }
60 }
61
[62ee2ce]62 m->setElement(5, 5, TERRAIN_ROCK);
63
[60b77d2]64 return m;
65}
[384b7e0]66
67WorldMap* WorldMap::loadMapFromFile(string filename)
68{
69 WorldMap* m = new WorldMap(12l, 12);
70
[f401cac]71 ifstream file(filename.c_str());
[384b7e0]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.