-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsettings.cpp
executable file
·190 lines (151 loc) · 5.35 KB
/
settings.cpp
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include "settings.h"
#include "xmlutils.h"
#include "filepath.h"
const TiXmlElement* wrapMissingElementException(const TiXmlNode* root, const char* name) {
const TiXmlElement* elem = root->FirstChildElement(name);
if (elem == NULL) {
throw MissingElementException(name);
}
return elem;
}
app_settings::~app_settings() {
if (svg != NULL) {
delete svg;
}
}
app_settings::app_settings(const char* filename) {
// TODO: What is RAII again?
FILE* f = fopen(filename, "r");
if (f == NULL) {
string msg = "Could not open file: ";
msg += filename;
throw IOException(msg);
}
try {
TiXmlDocument doc;
if (doc.LoadFile(f)) {
read(wrapMissingElementException(&doc, "aplicacao"));
parseSVG();
findElements();
normalize();
debugFields();
} else {
throw IOException(doc.ErrorDesc());
}
fclose(f);
} catch (...) {
if (f != NULL) {
// printf("Closing file\n");
fclose(f);
}
throw;
}
}
void app_settings::normalize() {
// let the position of the arena be take out of the other objects
double dx = -arena->cx;
double dy = -arena->cy;
translate(dx, dy, player);
translate(dx, dy, arena); // too lazy to do it bruh
translate(dx, dy, flyingEnemies);
translate(dx, dy, groundEnemies);
airstrip->translate(dx, dy);
// In the previous work, the radius increased until it reaches twice its
// original radius. In 3D though, there is none of the increase feature,
// so we just use the final radius.
player->radius *= 2;
}
void app_settings::translate(int dx, int dy, vector<simple_svg_circle*>& circles) {
for (int i = 0; i < circles.size(); i++) {
translate(dx, dy, circles[i]);
}
}
void app_settings::translate(int dx, int dy, simple_svg_circle* circle) {
circle->cx += dx;
circle->cy += dy;
}
void app_settings::findElements() {
for (unsigned int i = 0; i < svg->circles.size(); i++) {
simple_svg_circle* c = &svg->circles[i];
const string& fill = c->fillName;
if (fill == "blue") {
// arena
arena = c;
} else if (fill == "green") {
// player
player = c;
} else if (fill == "red") {
// flying enemy
flyingEnemies.push_back(c);
} else if (fill == "orange") {
// ground enemy
groundEnemies.push_back(c);
} else {
printf("[WARNING] Ignoring circle with color \"%s\"\n", fill.c_str());
}
}
switch (svg->lines.size()) {
case 0:
throw MissingSVGShapeException("line");
default:
printf("[WARNING] There are too many lines in SVG. Only the first will be used.");
case 1:
airstrip = &svg->lines[0];
break;
}
}
void app_settings::debugFields() {
printf(" *** Loaded config file\n");
printf("svg file to load: %s\n", filenameSVG.c_str());
// player
printf("Player\n");
printf("velocity factor = %f\n", vel);
printf("bullet velocity factor = %f\n", bulletVel);
// enemy
printf("Enemy\n");
printf("enemy velocity factor = %f\n", eVel);
printf("enemy bullet velocity factor = %f\n", eBulletVel);
printf("bullet frequency (bullets/s) = %f\n", eBulletF);
// SVG
printf("Arena: %p\n", arena);
printf("Player: %p\n", player);
printf("Airstrip: %p\n", airstrip);
printf("Count of ground enemies: %ld\n", groundEnemies.size());
printf("Count of flying enemies: %ld\n", flyingEnemies.size());
printf(" ***\n");
}
void app_settings::parseSVG() {
svg = new simple_svg(filenameSVG.c_str());
printf("There are %u lines\n", (unsigned int) svg->lines.size());
printf("There are %u circles\n", (unsigned int) svg->circles.size());
svg->lines[0].debug();
}
void app_settings::read(const TiXmlElement* root) {
// query the element for the player
readPlayer(wrapMissingElementException(root, "jogador"));
readEnemy(wrapMissingElementException(root, "inimigo"));
// query for the location element
readSVG(wrapMissingElementException(root, "arquivoDaArena"));
}
void app_settings::readPlayer(const TiXmlElement* root) {
// tries to read the vel attribute
vel = readDoubleAttribute(root, "vel");
bulletVel = readDoubleAttribute(root, "velTiro");
}
void app_settings::readEnemy(const TiXmlElement* root) {
// tries to read the vel attribute
eVel = readDoubleAttribute(root, "vel");
eBulletVel = readDoubleAttribute(root, "velTiro");
eBulletF = readDoubleAttribute(root, "freqTiro");
}
string findTextElement(const TiXmlElement* parent, const char* name) {
return getTextOrThrow(wrapMissingElementException(parent, name));
}
void app_settings::readSVG(const TiXmlElement* root) {
string name = findTextElement(root, "nome");
string extension = findTextElement(root, "tipo");
string folder = findTextElement(root, "caminho");
file_path path(folder.c_str());
path.append(name + "." + extension);
filenameSVG = string(path.toString());
}