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