10,11 → 10,11 |
|
isDisconnected = false; |
|
x = rand() % 800; |
y = rand() % 600; |
linePoints.append(QPoint(x, y)); |
x = rand() % 80000; |
y = rand() % 60000; |
//linePoints.append(QPoint(x, y)); |
|
velocity = 1; |
velocity = 100; |
angle = (rand() % 4) * 90; |
abpool = 0; |
name = ""; |
23,17 → 23,19 |
hadGo = false; |
dead = false; |
collided = false; |
speed = 5; |
speed = 500; |
|
colour.setBlue(0); |
colour.setRed(0); |
colour.setGreen(0); |
|
greet(); |
|
if (!socket->waitForReadyRead(2000)) |
{ |
socket->disconnectFromHost(); |
show = false; |
isReady = true; |
//isReady = true; |
hadGo = false; |
} |
} |
60,7 → 62,7 |
|
point2 = linePoints[i]; |
|
painter->drawLine(point1, point2); |
painter->drawLine(point1/100., point2/100.); |
} |
|
if (!collided) |
84,13 → 86,13 |
} |
if (angle == 0 || angle == 180) |
{ |
painter->fillRect(x - 5, y - 15, 10, 30, colour); |
painter->fillRect(x/100. - 5, y/100. - 15, 10, 30, colour); |
} |
else |
{ |
painter->fillRect(x - 15, y - 5, 30, 10, colour); |
painter->fillRect(x/100. - 15, y/100. - 5, 30, 10, colour); |
} |
painter->drawText(x, y - 20, name); |
painter->drawText(x/100., y/100. - 20, name); |
} |
else |
{ |
158,6 → 160,7 |
} |
|
hadGo = false; |
couldReadLine = false; |
if (!socket->waitForReadyRead(1)) |
{ |
// socket->disconnectFromHost(); |
165,16 → 168,19 |
haveSentAlready = true; |
return; |
} |
if (!hadGo) |
if (!hadGo && couldReadLine) |
{ |
cout << "KILLING BECAUSE HE DIDN'T HAVE A GO\n"; |
socket->disconnectFromHost(); |
dead = true; |
} |
} |
hasHadGo = true; |
if(hadGo || dead) |
hasHadGo = true; |
|
time_t result = time(NULL); |
cout << result << ": Recieved next move from " << name.toStdString().c_str() << endl; |
if(hasHadGo) |
cout << result << ": Recieved next move from " << name.toStdString().c_str() << endl; |
} |
|
int sign(int x){ |
192,9 → 198,11 |
// Do collision detection here |
// use linePoints |
int i = linePoints.count() - 1; |
if(linePoints[i-1].x() < 0 || linePoints[i-1].x() > 800 || linePoints[i-1].y() < 0 || linePoints[i-1].y() > 600) |
if(i <= 0) |
return false; |
if(linePoints[i-1].x() < 0 || linePoints[i-1].x() >= 80000 || linePoints[i-1].y() < 0 || linePoints[i-1].y() >= 60000) |
return true; |
if(linePoints[i].x() < 0 || linePoints[i].x() > 800 || linePoints[i].y() < 0 || linePoints[i].y() > 600) |
if(linePoints[i].x() < 0 || linePoints[i].x() >= 80000 || linePoints[i].y() < 0 || linePoints[i].y() >= 60000) |
return true; |
int j, r; |
Bike *bike; |
240,7 → 248,7 |
{ |
// x equal |
|
if(ix > jx && i1x < jx || ix < jx && i1x > jx) |
if((ix > jx && i1x < jx) || (ix < jx && i1x > jx)) |
{ |
if((sign(iy - jy) != sign(iy - j1y))) |
return true; |
249,7 → 257,7 |
if(jy == j1y) |
{ |
|
if(iy > jy && i1y < jy || iy < jy && i1y > jy) |
if((iy > jy && i1y < jy) || (iy < jy && i1y > jy)) |
{ |
if((sign(ix - jx) != sign(ix - j1x))) |
return true; |
270,13 → 278,14 |
void Bike::reset() |
{ |
cout << "Reset was called\n"; |
x = rand() % 800; |
y = rand() % 600; |
x = rand() % 80000; |
y = rand() % 60000; |
cout << "x: " << x << " y: " << y << endl; |
linePoints.clear(); |
|
linePoints.append(QPoint(x, y)); |
//linePoints.append(QPoint(x, y)); |
|
velocity = 1; |
velocity = 100; |
angle = 0; |
abpool = 0; |
show = true; |
284,11 → 293,17 |
hadGo = false; |
dead = false; |
collided = false; |
speed = 5; |
speed = 500; |
|
socket->write("RESET\n"); |
} |
|
void Bike::greet() |
{ |
cout << "Greeting\n"; |
socket->write("ARENA 80000 60000\n"); |
} |
|
void Bike::readyRead() |
{ |
while (socket->canReadLine()) |
295,6 → 310,9 |
{ |
QByteArray data = socket->readLine(); |
QString line = data.trimmed(); |
std::cout << "Bike " << name.toStdString() << " " << line.toStdString() |
<< std::endl; |
couldReadLine = true; |
|
if (line == "L") |
{ |
309,10 → 327,10 |
angle += 360; |
} |
if(velocity < speed) |
velocity += 0.3; |
velocity += 30; |
else if(velocity > speed) |
velocity -= 0.3; |
if(abs(speed-velocity)<0.3) |
velocity -= 30; |
if(abs(speed-velocity)<30) |
velocity = speed; |
if(abpool<10) |
abpool += 0.2; |
331,10 → 349,10 |
angle += 360; |
} |
if(velocity < speed) |
velocity += 0.3; |
velocity += 30; |
else if(velocity > speed) |
velocity -= 0.3; |
if(abs(speed-velocity)<0.3) |
velocity -= 30; |
if(abs(speed-velocity)<30) |
velocity = speed; |
if(abpool<10) |
abpool += 0.2; |
343,14 → 361,14 |
else if (line == "A") |
{ |
if(abpool > 0){ |
velocity += 0.1; |
velocity += 20; |
abpool -= 0.5; |
} else { |
if(velocity < speed) |
velocity += 0.3; |
velocity += 30; |
else if(velocity > speed) |
velocity -= 0.3; |
if(abs(speed-velocity)<0.3) |
velocity -= 30; |
if(abs(speed-velocity)<30) |
velocity = speed; |
} |
hadGo = true; |
358,15 → 376,15 |
else if (line == "D") |
{ |
if(abpool > 0){ |
velocity -= 0.2; |
velocity -= 20; |
abpool -= 0.5; |
} |
else { |
if(velocity < speed) |
velocity += 0.3; |
velocity += 30; |
else if(velocity > speed) |
velocity -= 0.3; |
if(abs(speed-velocity)<0.3) |
velocity -= 30; |
if(abs(speed-velocity)<30) |
velocity = speed; |
} |
hadGo = true; |
375,10 → 393,10 |
{ |
|
if(velocity < speed) |
velocity += 0.3; |
velocity += 30; |
else if(velocity > speed) |
velocity -= 0.3; |
if(abs(speed-velocity)<0.3) |
velocity -= 30; |
if(abs(speed-velocity)<30) |
velocity = speed; |
if(abpool<10) |
abpool += 0.2; |
415,6 → 433,15 |
emit chat(name, message); |
} |
|
if(velocity < speed) |
velocity += 30; |
else if(velocity > speed) |
velocity -= 30; |
if(abs(speed-velocity)<30) |
velocity = speed; |
if(abpool<10) |
abpool += 0.2; |
|
hadGo = true; |
} |
} |