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