Rev 20 | Rev 22 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1 | muzer | 1 | #include "bike.h" |
2 | |||
3 | Bike::Bike(QTcpSocket *sock, int i) |
||
4 | { |
||
9 | muzer | 5 | socket = sock; |
6 | id = i; |
||
1 | muzer | 7 | |
9 | muzer | 8 | connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead())); |
9 | connect(socket, SIGNAL(disconnected()), this, SLOT(disconnected())); |
||
1 | muzer | 10 | |
9 | muzer | 11 | isDisconnected = false; |
1 | muzer | 12 | |
9 | muzer | 13 | x = rand() % 800; |
14 | y = rand() % 600; |
||
15 | linePoints.append(QPoint(x, y)); |
||
1 | muzer | 16 | |
9 | muzer | 17 | velocity = 1; |
18 | angle = 0; |
||
19 | muzer | 19 | abpool = 0; |
9 | muzer | 20 | name = ""; |
21 | show = false; |
||
22 | isReady = false; |
||
23 | hadGo = false; |
||
24 | dead = false; |
||
25 | collided = false; |
||
1 | muzer | 26 | |
9 | muzer | 27 | colour.setBlue(0); |
28 | colour.setRed(0); |
||
29 | colour.setGreen(0); |
||
1 | muzer | 30 | |
9 | muzer | 31 | if (!socket->waitForReadyRead(2000)) |
32 | { |
||
33 | socket->disconnectFromHost(); |
||
34 | show = false; |
||
35 | isReady = true; |
||
36 | hadGo = false; |
||
37 | } |
||
1 | muzer | 38 | } |
39 | |||
14 | muzer | 40 | void Bike::draw(QPainter *painter, QList<Bike *> bikes) |
1 | muzer | 41 | { |
9 | muzer | 42 | if (show) |
43 | { |
||
44 | painter->setPen(colour); |
||
45 | painter->setFont(QFont("sans", 12)); |
||
1 | muzer | 46 | |
9 | muzer | 47 | for (int i = 0; i < linePoints.count(); i++) |
48 | { |
||
49 | QPoint point1; |
||
50 | QPoint point2; |
||
51 | if (i == 0) |
||
52 | { |
||
53 | point1 = linePoints[0]; |
||
54 | } |
||
55 | else |
||
56 | { |
||
57 | point1 = linePoints[i - 1]; |
||
58 | } |
||
1 | muzer | 59 | |
9 | muzer | 60 | point2 = linePoints[i]; |
1 | muzer | 61 | |
9 | muzer | 62 | painter->drawLine(point1, point2); |
63 | } |
||
1 | muzer | 64 | |
9 | muzer | 65 | if (!collided) |
66 | { |
||
14 | muzer | 67 | collided = hasCollided(bikes); |
9 | muzer | 68 | if (angle == 0) |
69 | { |
||
70 | y -= velocity; |
||
71 | } |
||
72 | else if (angle == 90) |
||
73 | { |
||
74 | x += velocity; |
||
75 | } |
||
76 | else if (angle == 180) |
||
77 | { |
||
78 | y += velocity; |
||
79 | } |
||
80 | else if (angle == 270) |
||
81 | { |
||
82 | x -= velocity; |
||
83 | } |
||
84 | } |
||
85 | else |
||
86 | { |
||
87 | linePoints.clear(); |
||
88 | } |
||
1 | muzer | 89 | |
9 | muzer | 90 | if (angle == 0 || angle == 180) |
91 | { |
||
92 | painter->fillRect(x - 5, y - 15, 10, 30, colour); |
||
93 | } |
||
94 | else |
||
95 | { |
||
96 | painter->fillRect(x - 15, y - 5, 30, 10, colour); |
||
97 | } |
||
1 | muzer | 98 | |
9 | muzer | 99 | painter->drawText(x, y - 20, name); |
100 | } |
||
1 | muzer | 101 | } |
102 | |||
103 | void Bike::run(QList<Bike *> bikes) |
||
104 | { |
||
9 | muzer | 105 | for (int i = 0; i < bikes.count(); i++) |
106 | { |
||
107 | Bike *bike = bikes[i]; |
||
108 | if (bike->isReady && bike->show) |
||
109 | { |
||
14 | muzer | 110 | if (bike->dead || bike->collided) |
9 | muzer | 111 | { |
112 | socket->write("DEAD "); |
||
113 | socket->write(bike->name.toAscii()); |
||
114 | socket->write("\n"); |
||
115 | } |
||
116 | else |
||
117 | { |
||
118 | socket->write("BIKE "); |
||
119 | socket->write(bike->name.toAscii()); |
||
120 | socket->write(" "); |
||
121 | socket->write(QString::number(bike->x).toAscii()); |
||
122 | socket->write(" "); |
||
123 | socket->write(QString::number(bike->y).toAscii()); |
||
124 | socket->write(" "); |
||
125 | socket->write(QString::number(bike->colour.red()).toAscii()); |
||
126 | socket->write(" "); |
||
127 | socket->write(QString::number(bike->colour.green()).toAscii()); |
||
128 | socket->write(" "); |
||
129 | socket->write(QString::number(bike->colour.blue()).toAscii()); |
||
130 | socket->write("\n"); |
||
131 | } |
||
132 | } |
||
133 | } |
||
1 | muzer | 134 | |
9 | muzer | 135 | linePoints.append(QPoint(x, y)); |
4 | tom | 136 | |
9 | muzer | 137 | if (dead) |
138 | { |
||
139 | show = false; |
||
140 | isReady = true; |
||
141 | hadGo = false; |
||
142 | dead = false; |
||
143 | } |
||
1 | muzer | 144 | |
9 | muzer | 145 | if (!dead) |
146 | { |
||
147 | socket->write("G\n"); |
||
148 | socket->flush(); |
||
1 | muzer | 149 | |
9 | muzer | 150 | hadGo = false; |
151 | if (!socket->waitForReadyRead(2000)) |
||
152 | { |
||
153 | socket->disconnectFromHost(); |
||
154 | dead = true; |
||
155 | } |
||
156 | if (!hadGo) |
||
157 | { |
||
158 | socket->disconnectFromHost(); |
||
159 | dead = true; |
||
160 | } |
||
161 | } |
||
162 | hasHadGo = true; |
||
1 | muzer | 163 | } |
164 | |||
19 | muzer | 165 | int sign(int x){ |
166 | if(x > 0){ |
||
9 | muzer | 167 | return 1; |
168 | } |
||
19 | muzer | 169 | if(x < 0){ |
9 | muzer | 170 | return -1; |
171 | } |
||
172 | return 0; |
||
173 | } |
||
174 | |||
14 | muzer | 175 | bool Bike::hasCollided(QList<Bike *> bikes) |
8 | tom | 176 | { |
9 | muzer | 177 | // Do collision detection here |
178 | // use linePoints |
||
179 | int i = linePoints.count() - 1; |
||
180 | if(linePoints[i-1].x() < 0 || linePoints[i-1].x() > 800 || linePoints[i-1].y() < 0 || linePoints[i-1].y() > 600) |
||
181 | return true; |
||
182 | if(linePoints[i].x() < 0 || linePoints[i].x() > 800 || linePoints[i].y() < 0 || linePoints[i].y() > 600) |
||
183 | return true; |
||
18 | muzer | 184 | int j, r; |
14 | muzer | 185 | Bike *bike; |
19 | muzer | 186 | for(r = 0; r < bikes.count(); r++) |
18 | muzer | 187 | { |
188 | bike = bikes[r]; |
||
21 | muzer | 189 | int forsubtract = 1; |
190 | if(bike->name == name){ |
||
191 | forsubtract = 3; |
||
192 | } |
||
193 | for(j = 0; j < bike->linePoints.count() - forsubtract; j++) |
||
18 | muzer | 194 | { |
20 | muzer | 195 | int jx = bike->linePoints[j].x(); |
196 | int j1x = bike->linePoints[j+1].x(); |
||
197 | int jy = bike->linePoints[j].y(); |
||
198 | int j1y = bike->linePoints[j+1].y(); |
||
199 | int ix = linePoints[i-1].x(); |
||
200 | int i1x = linePoints[i].x(); |
||
201 | int iy = linePoints[i-1].y(); |
||
202 | int i1y = linePoints[i].y(); |
||
21 | muzer | 203 | |
20 | muzer | 204 | if(angle == 0){ |
205 | iy += 1; |
||
206 | i1y -= 1; |
||
207 | } |
||
208 | if(angle == 90){ |
||
209 | ix -= 1; |
||
210 | i1x += 1; |
||
211 | } |
||
212 | if(angle == 180){ |
||
213 | iy -= 1; |
||
214 | i1y += 1; |
||
215 | } |
||
216 | if(angle == 270){ |
||
217 | ix += 1; |
||
218 | i1x -= 1; |
||
219 | } |
||
220 | if(!(jx == j1x && i1x == ix) && !(jy == j1y && i1y == iy)) |
||
18 | muzer | 221 | { |
16 | muzer | 222 | |
18 | muzer | 223 | // If not parallel |
20 | muzer | 224 | if(jx == j1x) |
18 | muzer | 225 | { |
226 | // x equal |
||
16 | muzer | 227 | |
20 | muzer | 228 | if(ix > jx && i1x < jx || ix < jx && i1x > jx) |
18 | muzer | 229 | { |
20 | muzer | 230 | if((sign(iy - jy) != sign(iy - j1y))) |
18 | muzer | 231 | return true; |
232 | } |
||
233 | } |
||
20 | muzer | 234 | if(jy == j1y) |
18 | muzer | 235 | { |
16 | muzer | 236 | |
20 | muzer | 237 | if(iy > jy && i1y < jy || iy < jy && i1y > jy) |
18 | muzer | 238 | { |
20 | muzer | 239 | if((sign(ix - jx) != sign(ix - j1x))) |
18 | muzer | 240 | return true; |
241 | } |
||
242 | } |
||
243 | } |
||
244 | } |
||
9 | muzer | 245 | } |
246 | return false; |
||
8 | tom | 247 | } |
248 | |||
1 | muzer | 249 | void Bike::reset() |
250 | { |
||
9 | muzer | 251 | x = rand() % 800; |
252 | y = rand() % 600; |
||
253 | linePoints.clear(); |
||
1 | muzer | 254 | |
9 | muzer | 255 | linePoints.append(QPoint(x, y)); |
1 | muzer | 256 | |
9 | muzer | 257 | velocity = 1; |
258 | angle = 0; |
||
19 | muzer | 259 | abpool = 0; |
9 | muzer | 260 | show = true; |
261 | isReady = true; |
||
262 | hadGo = false; |
||
263 | dead = false; |
||
264 | collided = false; |
||
1 | muzer | 265 | |
9 | muzer | 266 | socket->write("RESET\n"); |
1 | muzer | 267 | } |
268 | |||
269 | void Bike::readyRead() |
||
270 | { |
||
9 | muzer | 271 | while (socket->canReadLine()) |
272 | { |
||
273 | QByteArray data = socket->readLine(); |
||
274 | QString line = data.trimmed(); |
||
1 | muzer | 275 | |
9 | muzer | 276 | if (line == "L") |
277 | { |
||
278 | angle -= 90; |
||
1 | muzer | 279 | |
9 | muzer | 280 | if (angle >= 360) |
281 | { |
||
282 | angle -= 360; |
||
283 | } |
||
284 | if (angle < 0) |
||
285 | { |
||
286 | angle += 360; |
||
287 | } |
||
19 | muzer | 288 | if(velocity < 5) |
289 | velocity += 0.5; |
||
290 | else if(velocity > 5) |
||
291 | velocity -= 0.5; |
||
292 | if(abpool<10) |
||
293 | abpool += 0.2; |
||
9 | muzer | 294 | hadGo = true; |
295 | } |
||
296 | else if (line == "R") |
||
297 | { |
||
298 | angle += 90; |
||
1 | muzer | 299 | |
9 | muzer | 300 | if (angle >= 360) |
301 | { |
||
302 | angle -= 360; |
||
303 | } |
||
304 | if (angle < 0) |
||
305 | { |
||
306 | angle += 360; |
||
307 | } |
||
19 | muzer | 308 | if(velocity < 5) |
309 | velocity += 0.5; |
||
310 | else if(velocity > 5) |
||
311 | velocity -= 0.5; |
||
312 | if(abpool<10) |
||
313 | abpool += 0.2; |
||
9 | muzer | 314 | hadGo = true; |
315 | } |
||
316 | else if (line == "A") |
||
317 | { |
||
19 | muzer | 318 | if(abpool > 0){ |
319 | velocity += 0.1; |
||
320 | abpool -= 0.5; |
||
20 | muzer | 321 | } else { |
322 | if(velocity < 5) |
||
323 | velocity += 0.5; |
||
324 | else if(velocity > 5) |
||
325 | velocity -= 0.5; |
||
19 | muzer | 326 | } |
9 | muzer | 327 | hadGo = true; |
328 | } |
||
329 | else if (line == "D") |
||
330 | { |
||
19 | muzer | 331 | if(abpool > 0){ |
332 | velocity -= 0.2; |
||
333 | abpool -= 0.5; |
||
334 | } |
||
20 | muzer | 335 | else { |
336 | if(velocity < 5) |
||
337 | velocity += 0.5; |
||
338 | else if(velocity > 5) |
||
339 | velocity -= 0.5; |
||
340 | } |
||
9 | muzer | 341 | hadGo = true; |
342 | } |
||
343 | else if (line == "N") |
||
344 | { |
||
19 | muzer | 345 | if(velocity < 5) |
346 | velocity += 0.5; |
||
347 | else if(velocity > 5) |
||
348 | velocity -= 0.5; |
||
349 | if(abpool<10) |
||
350 | abpool += 0.2; |
||
9 | muzer | 351 | hadGo = true; |
352 | } |
||
353 | else if (line.startsWith("NAME ")) |
||
354 | { |
||
355 | name = line.remove(0, 5); |
||
356 | isReady = true; |
||
357 | show = true; |
||
358 | } |
||
359 | else if (line.startsWith("COLOUR ")) |
||
360 | { |
||
361 | QStringList list = line.split(" "); |
||
362 | if (list.count() >= 2) |
||
363 | { |
||
364 | colour.setRed(list[1].toInt()); |
||
365 | } |
||
366 | if (list.count() >= 3) |
||
367 | { |
||
368 | colour.setGreen(list[2].toInt()); |
||
369 | } |
||
370 | if (list.count() >= 4) |
||
371 | { |
||
372 | colour.setBlue(list[3].toInt()); |
||
373 | } |
||
374 | } |
||
375 | } |
||
1 | muzer | 376 | } |
377 | |||
378 | void Bike::disconnected() |
||
379 | { |
||
9 | muzer | 380 | dead = true; |
381 | isDisconnected = true; |
||
382 | cout << ":: Disconnected: " << socket->peerAddress().toString().toStdString() << endl; |
||
1 | muzer | 383 | } |