Rev 25 | Rev 28 | 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; |
26 | muzer | 18 | angle = (rand() % 4) * 90; |
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 | { |
||
25 | muzer | 110 | if (bike->collided) |
9 | muzer | 111 | { |
112 | socket->write("DEAD "); |
||
113 | socket->write(bike->name.toAscii()); |
||
114 | socket->write("\n"); |
||
115 | } |
||
26 | muzer | 116 | else if (bike->dead) |
25 | muzer | 117 | { |
118 | socket->write("DISCO "); |
||
119 | socket->write(bike->name.toAscii()); |
||
120 | socket->write("\n"); |
||
121 | } |
||
9 | muzer | 122 | else |
123 | { |
||
124 | socket->write("BIKE "); |
||
125 | socket->write(bike->name.toAscii()); |
||
126 | socket->write(" "); |
||
127 | socket->write(QString::number(bike->x).toAscii()); |
||
128 | socket->write(" "); |
||
129 | socket->write(QString::number(bike->y).toAscii()); |
||
130 | socket->write(" "); |
||
131 | socket->write(QString::number(bike->colour.red()).toAscii()); |
||
132 | socket->write(" "); |
||
133 | socket->write(QString::number(bike->colour.green()).toAscii()); |
||
134 | socket->write(" "); |
||
135 | socket->write(QString::number(bike->colour.blue()).toAscii()); |
||
136 | socket->write("\n"); |
||
137 | } |
||
138 | } |
||
139 | } |
||
1 | muzer | 140 | |
9 | muzer | 141 | linePoints.append(QPoint(x, y)); |
4 | tom | 142 | |
9 | muzer | 143 | if (dead) |
144 | { |
||
145 | show = false; |
||
146 | isReady = true; |
||
147 | hadGo = false; |
||
148 | dead = false; |
||
149 | } |
||
1 | muzer | 150 | |
9 | muzer | 151 | if (!dead) |
152 | { |
||
153 | socket->write("G\n"); |
||
154 | socket->flush(); |
||
1 | muzer | 155 | |
9 | muzer | 156 | hadGo = false; |
157 | if (!socket->waitForReadyRead(2000)) |
||
158 | { |
||
159 | socket->disconnectFromHost(); |
||
160 | dead = true; |
||
161 | } |
||
162 | if (!hadGo) |
||
163 | { |
||
164 | socket->disconnectFromHost(); |
||
165 | dead = true; |
||
166 | } |
||
167 | } |
||
168 | hasHadGo = true; |
||
1 | muzer | 169 | } |
170 | |||
19 | muzer | 171 | int sign(int x){ |
172 | if(x > 0){ |
||
9 | muzer | 173 | return 1; |
174 | } |
||
19 | muzer | 175 | if(x < 0){ |
9 | muzer | 176 | return -1; |
177 | } |
||
178 | return 0; |
||
179 | } |
||
180 | |||
14 | muzer | 181 | bool Bike::hasCollided(QList<Bike *> bikes) |
8 | tom | 182 | { |
9 | muzer | 183 | // Do collision detection here |
184 | // use linePoints |
||
185 | int i = linePoints.count() - 1; |
||
186 | if(linePoints[i-1].x() < 0 || linePoints[i-1].x() > 800 || linePoints[i-1].y() < 0 || linePoints[i-1].y() > 600) |
||
187 | return true; |
||
188 | if(linePoints[i].x() < 0 || linePoints[i].x() > 800 || linePoints[i].y() < 0 || linePoints[i].y() > 600) |
||
189 | return true; |
||
18 | muzer | 190 | int j, r; |
14 | muzer | 191 | Bike *bike; |
19 | muzer | 192 | for(r = 0; r < bikes.count(); r++) |
18 | muzer | 193 | { |
194 | bike = bikes[r]; |
||
21 | muzer | 195 | int forsubtract = 1; |
196 | if(bike->name == name){ |
||
197 | forsubtract = 3; |
||
198 | } |
||
199 | for(j = 0; j < bike->linePoints.count() - forsubtract; j++) |
||
18 | muzer | 200 | { |
20 | muzer | 201 | int jx = bike->linePoints[j].x(); |
202 | int j1x = bike->linePoints[j+1].x(); |
||
203 | int jy = bike->linePoints[j].y(); |
||
204 | int j1y = bike->linePoints[j+1].y(); |
||
205 | int ix = linePoints[i-1].x(); |
||
206 | int i1x = linePoints[i].x(); |
||
207 | int iy = linePoints[i-1].y(); |
||
208 | int i1y = linePoints[i].y(); |
||
21 | muzer | 209 | |
20 | muzer | 210 | if(angle == 0){ |
211 | iy += 1; |
||
212 | i1y -= 1; |
||
213 | } |
||
214 | if(angle == 90){ |
||
215 | ix -= 1; |
||
216 | i1x += 1; |
||
217 | } |
||
218 | if(angle == 180){ |
||
219 | iy -= 1; |
||
220 | i1y += 1; |
||
221 | } |
||
222 | if(angle == 270){ |
||
223 | ix += 1; |
||
224 | i1x -= 1; |
||
225 | } |
||
226 | if(!(jx == j1x && i1x == ix) && !(jy == j1y && i1y == iy)) |
||
18 | muzer | 227 | { |
16 | muzer | 228 | |
18 | muzer | 229 | // If not parallel |
20 | muzer | 230 | if(jx == j1x) |
18 | muzer | 231 | { |
232 | // x equal |
||
16 | muzer | 233 | |
20 | muzer | 234 | if(ix > jx && i1x < jx || ix < jx && i1x > jx) |
18 | muzer | 235 | { |
20 | muzer | 236 | if((sign(iy - jy) != sign(iy - j1y))) |
18 | muzer | 237 | return true; |
238 | } |
||
239 | } |
||
20 | muzer | 240 | if(jy == j1y) |
18 | muzer | 241 | { |
16 | muzer | 242 | |
20 | muzer | 243 | if(iy > jy && i1y < jy || iy < jy && i1y > jy) |
18 | muzer | 244 | { |
20 | muzer | 245 | if((sign(ix - jx) != sign(ix - j1x))) |
18 | muzer | 246 | return true; |
247 | } |
||
248 | } |
||
249 | } |
||
250 | } |
||
9 | muzer | 251 | } |
252 | return false; |
||
8 | tom | 253 | } |
254 | |||
1 | muzer | 255 | void Bike::reset() |
256 | { |
||
9 | muzer | 257 | x = rand() % 800; |
258 | y = rand() % 600; |
||
259 | linePoints.clear(); |
||
1 | muzer | 260 | |
9 | muzer | 261 | linePoints.append(QPoint(x, y)); |
1 | muzer | 262 | |
9 | muzer | 263 | velocity = 1; |
264 | angle = 0; |
||
19 | muzer | 265 | abpool = 0; |
9 | muzer | 266 | show = true; |
267 | isReady = true; |
||
268 | hadGo = false; |
||
269 | dead = false; |
||
270 | collided = false; |
||
1 | muzer | 271 | |
9 | muzer | 272 | socket->write("RESET\n"); |
1 | muzer | 273 | } |
274 | |||
275 | void Bike::readyRead() |
||
276 | { |
||
9 | muzer | 277 | while (socket->canReadLine()) |
278 | { |
||
279 | QByteArray data = socket->readLine(); |
||
280 | QString line = data.trimmed(); |
||
1 | muzer | 281 | |
9 | muzer | 282 | if (line == "L") |
283 | { |
||
284 | angle -= 90; |
||
1 | muzer | 285 | |
9 | muzer | 286 | if (angle >= 360) |
287 | { |
||
288 | angle -= 360; |
||
289 | } |
||
290 | if (angle < 0) |
||
291 | { |
||
292 | angle += 360; |
||
293 | } |
||
19 | muzer | 294 | if(velocity < 5) |
24 | muzer | 295 | velocity += 0.3; |
19 | muzer | 296 | else if(velocity > 5) |
24 | muzer | 297 | velocity -= 0.3; |
298 | if(abs(5-velocity)<0.3) |
||
22 | muzer | 299 | velocity = 5; |
19 | muzer | 300 | if(abpool<10) |
301 | abpool += 0.2; |
||
9 | muzer | 302 | hadGo = true; |
303 | } |
||
304 | else if (line == "R") |
||
305 | { |
||
306 | angle += 90; |
||
1 | muzer | 307 | |
9 | muzer | 308 | if (angle >= 360) |
309 | { |
||
310 | angle -= 360; |
||
311 | } |
||
312 | if (angle < 0) |
||
313 | { |
||
314 | angle += 360; |
||
315 | } |
||
19 | muzer | 316 | if(velocity < 5) |
24 | muzer | 317 | velocity += 0.3; |
19 | muzer | 318 | else if(velocity > 5) |
24 | muzer | 319 | velocity -= 0.3; |
320 | if(abs(5-velocity)<0.3) |
||
22 | muzer | 321 | velocity = 5; |
19 | muzer | 322 | if(abpool<10) |
323 | abpool += 0.2; |
||
9 | muzer | 324 | hadGo = true; |
325 | } |
||
326 | else if (line == "A") |
||
327 | { |
||
19 | muzer | 328 | if(abpool > 0){ |
329 | velocity += 0.1; |
||
330 | abpool -= 0.5; |
||
20 | muzer | 331 | } else { |
332 | if(velocity < 5) |
||
24 | muzer | 333 | velocity += 0.3; |
20 | muzer | 334 | else if(velocity > 5) |
24 | muzer | 335 | velocity -= 0.3; |
336 | if(abs(5-velocity)<0.3) |
||
22 | muzer | 337 | velocity = 5; |
19 | muzer | 338 | } |
9 | muzer | 339 | hadGo = true; |
340 | } |
||
341 | else if (line == "D") |
||
342 | { |
||
19 | muzer | 343 | if(abpool > 0){ |
344 | velocity -= 0.2; |
||
345 | abpool -= 0.5; |
||
346 | } |
||
20 | muzer | 347 | else { |
348 | if(velocity < 5) |
||
24 | muzer | 349 | velocity += 0.3; |
20 | muzer | 350 | else if(velocity > 5) |
24 | muzer | 351 | velocity -= 0.3; |
352 | if(abs(5-velocity)<0.3) |
||
22 | muzer | 353 | velocity = 5; |
20 | muzer | 354 | } |
9 | muzer | 355 | hadGo = true; |
356 | } |
||
357 | else if (line == "N") |
||
358 | { |
||
19 | muzer | 359 | if(velocity < 5) |
24 | muzer | 360 | velocity += 0.3; |
19 | muzer | 361 | else if(velocity > 5) |
24 | muzer | 362 | velocity -= 0.3; |
363 | if(abs(5-velocity)<0.3) |
||
22 | muzer | 364 | velocity = 5; |
19 | muzer | 365 | if(abpool<10) |
366 | abpool += 0.2; |
||
9 | muzer | 367 | hadGo = true; |
368 | } |
||
369 | else if (line.startsWith("NAME ")) |
||
370 | { |
||
371 | name = line.remove(0, 5); |
||
372 | isReady = true; |
||
373 | show = true; |
||
374 | } |
||
375 | else if (line.startsWith("COLOUR ")) |
||
376 | { |
||
377 | QStringList list = line.split(" "); |
||
378 | if (list.count() >= 2) |
||
379 | { |
||
380 | colour.setRed(list[1].toInt()); |
||
381 | } |
||
382 | if (list.count() >= 3) |
||
383 | { |
||
384 | colour.setGreen(list[2].toInt()); |
||
385 | } |
||
386 | if (list.count() >= 4) |
||
387 | { |
||
388 | colour.setBlue(list[3].toInt()); |
||
389 | } |
||
390 | } |
||
391 | } |
||
1 | muzer | 392 | } |
393 | |||
394 | void Bike::disconnected() |
||
395 | { |
||
9 | muzer | 396 | dead = true; |
397 | isDisconnected = true; |
||
398 | cout << ":: Disconnected: " << socket->peerAddress().toString().toStdString() << endl; |
||
1 | muzer | 399 | } |