Rev 19 | Rev 21 | 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]; |
||
| 189 | for(j = 0; j < bike->linePoints.count() - 1; j++) |
||
| 190 | { |
||
| 20 | muzer | 191 | int jx = bike->linePoints[j].x(); |
| 192 | int j1x = bike->linePoints[j+1].x(); |
||
| 193 | int jy = bike->linePoints[j].y(); |
||
| 194 | int j1y = bike->linePoints[j+1].y(); |
||
| 195 | int ix = linePoints[i-1].x(); |
||
| 196 | int i1x = linePoints[i].x(); |
||
| 197 | int iy = linePoints[i-1].y(); |
||
| 198 | int i1y = linePoints[i].y(); |
||
| 199 | if(jx > j1x){ |
||
| 200 | jx += 1; |
||
| 201 | j1x -= 1; |
||
| 202 | } |
||
| 203 | else if(j1x > jx){ |
||
| 204 | jx -= 1; |
||
| 205 | j1x += 1; |
||
| 206 | } |
||
| 207 | if(jy > j1y){ |
||
| 208 | jy += 1; |
||
| 209 | j1y -= 1; |
||
| 210 | } |
||
| 211 | else if(j1y > jy){ |
||
| 212 | jy += 1; |
||
| 213 | j1y -= 1; |
||
| 214 | } |
||
| 215 | if(angle == 0){ |
||
| 216 | iy += 1; |
||
| 217 | i1y -= 1; |
||
| 218 | } |
||
| 219 | if(angle == 90){ |
||
| 220 | ix -= 1; |
||
| 221 | i1x += 1; |
||
| 222 | } |
||
| 223 | if(angle == 180){ |
||
| 224 | iy -= 1; |
||
| 225 | i1y += 1; |
||
| 226 | } |
||
| 227 | if(angle == 270){ |
||
| 228 | ix += 1; |
||
| 229 | i1x -= 1; |
||
| 230 | } |
||
| 231 | if(!(jx == j1x && i1x == ix) && !(jy == j1y && i1y == iy)) |
||
| 18 | muzer | 232 | { |
| 16 | muzer | 233 | |
| 18 | muzer | 234 | // If not parallel |
| 20 | muzer | 235 | if(jx == j1x) |
| 18 | muzer | 236 | { |
| 237 | // x equal |
||
| 16 | muzer | 238 | |
| 20 | muzer | 239 | if(ix > jx && i1x < jx || ix < jx && i1x > jx) |
| 18 | muzer | 240 | { |
| 20 | muzer | 241 | if((sign(iy - jy) != sign(iy - j1y))) |
| 18 | muzer | 242 | return true; |
| 243 | } |
||
| 244 | } |
||
| 20 | muzer | 245 | if(jy == j1y) |
| 18 | muzer | 246 | { |
| 16 | muzer | 247 | |
| 20 | muzer | 248 | if(iy > jy && i1y < jy || iy < jy && i1y > jy) |
| 18 | muzer | 249 | { |
| 20 | muzer | 250 | if((sign(ix - jx) != sign(ix - j1x))) |
| 18 | muzer | 251 | return true; |
| 252 | } |
||
| 253 | } |
||
| 254 | } |
||
| 255 | } |
||
| 9 | muzer | 256 | } |
| 257 | return false; |
||
| 8 | tom | 258 | } |
| 259 | |||
| 1 | muzer | 260 | void Bike::reset() |
| 261 | { |
||
| 9 | muzer | 262 | x = rand() % 800; |
| 263 | y = rand() % 600; |
||
| 264 | linePoints.clear(); |
||
| 1 | muzer | 265 | |
| 9 | muzer | 266 | linePoints.append(QPoint(x, y)); |
| 1 | muzer | 267 | |
| 9 | muzer | 268 | velocity = 1; |
| 269 | angle = 0; |
||
| 19 | muzer | 270 | abpool = 0; |
| 9 | muzer | 271 | show = true; |
| 272 | isReady = true; |
||
| 273 | hadGo = false; |
||
| 274 | dead = false; |
||
| 275 | collided = false; |
||
| 1 | muzer | 276 | |
| 9 | muzer | 277 | socket->write("RESET\n"); |
| 1 | muzer | 278 | } |
| 279 | |||
| 280 | void Bike::readyRead() |
||
| 281 | { |
||
| 9 | muzer | 282 | while (socket->canReadLine()) |
| 283 | { |
||
| 284 | QByteArray data = socket->readLine(); |
||
| 285 | QString line = data.trimmed(); |
||
| 1 | muzer | 286 | |
| 9 | muzer | 287 | if (line == "L") |
| 288 | { |
||
| 289 | angle -= 90; |
||
| 1 | muzer | 290 | |
| 9 | muzer | 291 | if (angle >= 360) |
| 292 | { |
||
| 293 | angle -= 360; |
||
| 294 | } |
||
| 295 | if (angle < 0) |
||
| 296 | { |
||
| 297 | angle += 360; |
||
| 298 | } |
||
| 19 | muzer | 299 | if(velocity < 5) |
| 300 | velocity += 0.5; |
||
| 301 | else if(velocity > 5) |
||
| 302 | velocity -= 0.5; |
||
| 303 | if(abpool<10) |
||
| 304 | abpool += 0.2; |
||
| 9 | muzer | 305 | hadGo = true; |
| 306 | } |
||
| 307 | else if (line == "R") |
||
| 308 | { |
||
| 309 | angle += 90; |
||
| 1 | muzer | 310 | |
| 9 | muzer | 311 | if (angle >= 360) |
| 312 | { |
||
| 313 | angle -= 360; |
||
| 314 | } |
||
| 315 | if (angle < 0) |
||
| 316 | { |
||
| 317 | angle += 360; |
||
| 318 | } |
||
| 19 | muzer | 319 | if(velocity < 5) |
| 320 | velocity += 0.5; |
||
| 321 | else if(velocity > 5) |
||
| 322 | velocity -= 0.5; |
||
| 323 | if(abpool<10) |
||
| 324 | abpool += 0.2; |
||
| 9 | muzer | 325 | hadGo = true; |
| 326 | } |
||
| 327 | else if (line == "A") |
||
| 328 | { |
||
| 19 | muzer | 329 | if(abpool > 0){ |
| 330 | velocity += 0.1; |
||
| 331 | abpool -= 0.5; |
||
| 20 | muzer | 332 | } else { |
| 333 | if(velocity < 5) |
||
| 334 | velocity += 0.5; |
||
| 335 | else if(velocity > 5) |
||
| 336 | velocity -= 0.5; |
||
| 19 | muzer | 337 | } |
| 9 | muzer | 338 | hadGo = true; |
| 339 | } |
||
| 340 | else if (line == "D") |
||
| 341 | { |
||
| 19 | muzer | 342 | if(abpool > 0){ |
| 343 | velocity -= 0.2; |
||
| 344 | abpool -= 0.5; |
||
| 345 | } |
||
| 20 | muzer | 346 | else { |
| 347 | if(velocity < 5) |
||
| 348 | velocity += 0.5; |
||
| 349 | else if(velocity > 5) |
||
| 350 | velocity -= 0.5; |
||
| 351 | } |
||
| 9 | muzer | 352 | hadGo = true; |
| 353 | } |
||
| 354 | else if (line == "N") |
||
| 355 | { |
||
| 19 | muzer | 356 | if(velocity < 5) |
| 357 | velocity += 0.5; |
||
| 358 | else if(velocity > 5) |
||
| 359 | velocity -= 0.5; |
||
| 360 | if(abpool<10) |
||
| 361 | abpool += 0.2; |
||
| 9 | muzer | 362 | hadGo = true; |
| 363 | } |
||
| 364 | else if (line.startsWith("NAME ")) |
||
| 365 | { |
||
| 366 | name = line.remove(0, 5); |
||
| 367 | isReady = true; |
||
| 368 | show = true; |
||
| 369 | } |
||
| 370 | else if (line.startsWith("COLOUR ")) |
||
| 371 | { |
||
| 372 | QStringList list = line.split(" "); |
||
| 373 | if (list.count() >= 2) |
||
| 374 | { |
||
| 375 | colour.setRed(list[1].toInt()); |
||
| 376 | } |
||
| 377 | if (list.count() >= 3) |
||
| 378 | { |
||
| 379 | colour.setGreen(list[2].toInt()); |
||
| 380 | } |
||
| 381 | if (list.count() >= 4) |
||
| 382 | { |
||
| 383 | colour.setBlue(list[3].toInt()); |
||
| 384 | } |
||
| 385 | } |
||
| 386 | } |
||
| 1 | muzer | 387 | } |
| 388 | |||
| 389 | void Bike::disconnected() |
||
| 390 | { |
||
| 9 | muzer | 391 | dead = true; |
| 392 | isDisconnected = true; |
||
| 393 | cout << ":: Disconnected: " << socket->peerAddress().toString().toStdString() << endl; |
||
| 1 | muzer | 394 | } |