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