-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap.cpp
More file actions
165 lines (138 loc) · 4.27 KB
/
Copy pathmap.cpp
File metadata and controls
165 lines (138 loc) · 4.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include "map.h"
#include <fstream>
#include <sstream>
void Map::addBlock(Block & block)
{
itsBlocks.push_back(block);
}
int Map::getItsHeightMap() const
{
return itsHeightMap;
}
int Map::getItsWidthMap() const
{
return itsWidthMap;
}
Map::Map()
{
}
Map::~Map()
{
}
vector<string> Map::split(string s,string delimeter)
{
vector<string> result;
int start = 0;
int end = s.find(delimeter);
while (end != -1) {
result.push_back(s.substr(start, end - start));
start = end + delimeter.size();
end = s.find(delimeter, start);
}
result.push_back(s.substr(start, end - start));
return result;
}
std::vector<Block> & Map::getItsBlocks()
{
return itsBlocks;
}
bool Map::replace(std::string& str, const std::string& from, const std::string& to) {
size_t start_pos = str.find(from);
if(start_pos == std::string::npos)
return false;
str.replace(start_pos, from.length(), to);
return true;
}
vector<Personnage*> Map::loadMap(std::string mapFile)
{
itsWidthMap =0;
vector<Personnage*> result;
Block flag(1);
this->itsMapFile = mapFile;
ifstream stream(mapFile);
int y = -1;
string line;
while(!stream.eof())
{
getline(stream,line);
if(line[0] == '(' )
{
// round of enemies
while(replace(line," ", ""));
while(replace(line,"(", ""));
while(replace(line,")", ""));
vector<string> rounds = split(line,",");
for(int i = 0;i < (int)rounds.size()-1;i++)
{
vector<string>round = split(rounds[i],":");
if(round.size()== 2)
{
int x1 = stoi(round[0]);
int y1 = stoi(round[1])-1;
IA * ia = new IA(x1,x1,y1);
ia->setItsWidth(sizeBlock);
ia->setItsHeight(sizeBlock*2);
result.push_back(ia);
}
else if(round.size())
{
int x1 = stoi(round[0]);
int x2 = stoi(round[1]);
int y1 = stoi(round[2])-1;
IA * ia = new IA(x1,x2,y1);
ia->setItsWidth(sizeBlock);
ia->setItsHeight(sizeBlock*2);
result.push_back(ia);
}
}
}
else
{
// map
char c;
y++;
for(int i = 0; i<(int)line.size();i++,c = line[i])
{
// récupère le plus grand x pour définir la longueur de la carte
itsWidthMap = itsWidthMap < i ? i : itsWidthMap;
if(line[i] >= '2' && line[i] <= '5')
{
Block block(line[i]- '0',i,y);
block.setItsWidth(sizeBlock);
block.setItsHeight(sizeBlock);
itsBlocks.push_back(block);
}
else
{
if(line[i] == '*')
{
Player * player = new Player();
player->setItsBlockX(i);
player->setItsBlockY(y-1);
player->setItsWidth(sizeBlock);
player->setItsHeight(2*sizeBlock);
result.push_back(player);
}
if(line[i] == '.'|| line[i] == '*')
{
Block block(0,i,y);
block.setItsHeight(sizeBlock);
block.setItsWidth(sizeBlock);
itsBlocks.push_back(block);
}
if(line[i] == '1')
{
flag.setItsBlockX(i);
flag.setItsBlockY(y);
flag.setItsWidth(sizeBlock);
flag.setItsHeight(2*sizeBlock);
}
}
}
}
}
itsHeightMap = y;
stream.close();
itsBlocks.push_back(flag);
return result;
}