-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfollower.cpp
More file actions
219 lines (204 loc) · 4.21 KB
/
follower.cpp
File metadata and controls
219 lines (204 loc) · 4.21 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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#include "follower.h"
Follower::Follower(Player *p, FollowerType t, OccupationMapper *om) : parent(p), type(t), occupationMapper(om) {
placement = NULL;
playable = true;
pigs = false;
fairy = false;
QFile *file;
QString filePath(qApp->applicationDirPath());
switch(t) {
case normal:
case big:
filePath.append("/graphics/follower.svg");
file = new QFile(filePath); // Deleted at and of function.
break;
case builder:
filePath.append("/graphics/builder.svg");
file = new QFile(filePath);
break;
case pig:
filePath.append("/graphics/pig.svg");
file = new QFile(filePath);
break;
}
file->open(QIODevice::ReadOnly);
QByteArray data = file->readAll();
// All followers are red in the xml file - here we just do a string
// substitution to get different colors. Yellow is handled differently
// because it's actually encoded as goldenrod (#daa520). This is because
// colors are also used for player text, and yellow text is unreadable.
// Goldenrod followers not very visible, though, so special handling needed.
if (parent->getColor() == yellow) {
data.replace("#ff0000", "#ffff00");
}
else {
data.replace("#ff0000", htmlColorMap()[parent->getColor()].c_str());
}
imageLayer = new ImageLayer(); // Deleted in destructor
imageLayer->renderer = new QSvgRenderer(data); // Deleted in destructor
imageLayer->scale = 0.2;
occupationLayer.scale = 0.1;
if (t == big) {
imageLayer->scale = 0.3;
occupationLayer.scale = 0.15;
}
delete file;
}
Follower::~Follower() {
delete imageLayer->renderer;
imageLayer->renderer = NULL;
delete imageLayer;
imageLayer = NULL;
}
FollowerType Follower::getType() {
return type;
}
Player* Follower::getParent() {
return parent;
}
Subtile* Follower::getPlacement() {
return placement;
}
void Follower::setPigs(bool p) {
pigs = p;
}
void Follower::setFairy(bool f) {
fairy = f;
}
void Follower::setPlacement(Subtile *s) {
placement = s;
if (placement != NULL) {
playable = false;
translateSubtileToCoordinates(placement->getPosition(), &(imageLayer->x), &(imageLayer->y));
if (type == builder || type == pig) {
occupation = not_applicable;
}
else if (placement->getTerrainAddon() == monastery) {
occupation = monk;
}
else {
switch(placement->getTerrain()) {
case city:
occupation = knight;
break;
case road:
occupation = robber;
break;
case field:
occupation = farmer;
break;
default: // We should never get here.
return;
}
}
occupationLayer.renderer = occupationMapper->getRenderer(occupation);
occupationLayer.rotation = imageLayer->rotation;
occupationLayer.x = imageLayer->x + 20;
occupationLayer.y = imageLayer->y + 20;
if (type == big) {
occupationLayer.x += 10;
}
}
else {
playable = true;
occupation = not_applicable;
pigs = false;
fairy = false;
}
}
bool Follower::isPlayable() {
return playable;
}
bool Follower::hasPigs() {
return pigs;
}
bool Follower::hasFairy() {
return fairy;
}
Occupation Follower::getOccupation() {
return occupation;
}
ImageLayer* Follower::getImage() {
return imageLayer;
}
ImageLayer* Follower::getOccupationImage() {
if (type == normal || type == big) {
return &occupationLayer;
}
return NULL;
}
void Follower::translateSubtileToCoordinates(int subtile, int *x, int *y) {
double scale = 1;
if (type == big) {
scale = 0.8;
}
switch(subtile) {
case 0:
*x = 40 * scale;
*y = 0;
return;
case 1:
*x = 160 * scale;
*y = 0;
return;
case 2:
*x = 300 * scale;
*y = 0;
return;
case 3:
*x = 330 * scale;
*y = 30 * scale;
return;
case 4:
*x = 330 * scale;
*y = 160 * scale;
return;
case 5:
*x = 330 * scale;
*y = 300 * scale;
return;
case 6:
*x = 300 * scale;
*y = 320 * scale;
return;
case 7:
*x = 160 * scale;
*y = 320 * scale;
return;
case 8:
*x = 40 * scale;
*y = 320 * scale;
return;
case 9:
*x = 0;
*y = 300 * scale;
return;
case 10:
*x = 0;
*y = 160 * scale;
return;
case 11:
*x = 0;
*y = 30 * scale;
return;
case 12:
*x = 160 * scale;
*y = 100 * scale;
return;
case 13:
*x = 220 * scale;
*y = 160 * scale;
return;
case 14:
*x = 160 * scale;
*y = 220 * scale;
return;
case 15:
*x = 110 * scale;
*y = 160 * scale;
return;
default:
*x = 0;
*y = 0;
}
}