15,8 → 15,7 |
linePoints.append(QPoint(x, y)); |
|
velocity = 1; |
angle = (rand() % 4) * 90; |
abpool = 0; |
angle = 0; |
name = ""; |
show = false; |
isReady = false; |
37,7 → 36,7 |
} |
} |
|
void Bike::draw(QPainter *painter, QList<Bike *> bikes) |
void Bike::draw(QPainter *painter) |
{ |
if (show) |
{ |
64,7 → 63,8 |
|
if (!collided) |
{ |
collided = hasCollided(bikes); |
collided = hasCollided(); |
|
if (angle == 0) |
{ |
y -= velocity; |
81,15 → 81,6 |
{ |
x -= velocity; |
} |
if (angle == 0 || angle == 180) |
{ |
painter->fillRect(x - 5, y - 15, 10, 30, colour); |
} |
else |
{ |
painter->fillRect(x - 15, y - 5, 30, 10, colour); |
} |
painter->drawText(x, y - 20, name); |
} |
else |
{ |
96,9 → 87,16 |
linePoints.clear(); |
} |
|
if (angle == 0 || angle == 180) |
{ |
painter->fillRect(x - 5, y - 15, 10, 30, colour); |
} |
else |
{ |
painter->fillRect(x - 15, y - 5, 30, 10, colour); |
} |
|
|
|
painter->drawText(x, y - 20, name); |
} |
} |
|
109,7 → 107,7 |
Bike *bike = bikes[i]; |
if (bike->isReady && bike->show) |
{ |
if (bike->collided) |
if (bike->dead) |
{ |
socket->write("DEAD "); |
socket->write(bike->name.toAscii()); |
150,7 → 148,7 |
socket->flush(); |
|
hadGo = false; |
if (!socket->waitForReadyRead(10000)) |
if (!socket->waitForReadyRead(2000)) |
{ |
socket->disconnectFromHost(); |
dead = true; |
164,7 → 162,7 |
hasHadGo = true; |
} |
|
int sign(int x){ |
int sign(float x){ |
if(x > 0){ |
return 1; |
} |
174,7 → 172,7 |
return 0; |
} |
|
bool Bike::hasCollided(QList<Bike *> bikes) |
bool Bike::hasCollided() |
{ |
// Do collision detection here |
// use linePoints |
183,65 → 181,26 |
return true; |
if(linePoints[i].x() < 0 || linePoints[i].x() > 800 || linePoints[i].y() < 0 || linePoints[i].y() > 600) |
return true; |
int j, r; |
Bike *bike; |
for(r = 0; r < bikes.count(); r++) |
for(int j = 0; j < linePoints.count() - 2; j++) |
{ |
bike = bikes[r]; |
int forsubtract = 1; |
if(bike->name == name){ |
forsubtract = 3; |
} |
for(j = 0; j < bike->linePoints.count() - forsubtract; j++) |
if(!(linePoints[j].x == linePoints[j+1].x() && linePoints[i].x() == linePoints[i-1].x()) && !(linePoints[j].y() == linePoints[j+1].y() && linePoints[i].y == linePoints[i-1].y())) |
{ |
int jx = bike->linePoints[j].x(); |
int j1x = bike->linePoints[j+1].x(); |
int jy = bike->linePoints[j].y(); |
int j1y = bike->linePoints[j+1].y(); |
int ix = linePoints[i-1].x(); |
int i1x = linePoints[i].x(); |
int iy = linePoints[i-1].y(); |
int i1y = linePoints[i].y(); |
|
if(angle == 0){ |
iy += 1; |
i1y -= 1; |
// If not parallel |
if(linePoints[j].x() == linePoints[j+1].x()) |
{ |
// x equal |
if(linePoints[i-1].x() > linePoints[j].x() && linePoints[i].x() < linePoints[j].x() || linePoints[i-1].x() < linePoints[j].x() && linePoints[i].x() > linePoints[j].x()){ |
if((sign(linePoints[i-1].y() - linePoints[j].y()) != sign(linePoints[i-1].y() - linePoints[j+1].y()))) |
return true; |
} |
} |
if(angle == 90){ |
ix -= 1; |
i1x += 1; |
} |
if(angle == 180){ |
iy -= 1; |
i1y += 1; |
} |
if(angle == 270){ |
ix += 1; |
i1x -= 1; |
} |
if(!(jx == j1x && i1x == ix) && !(jy == j1y && i1y == iy)) |
else if(linePoints[j].y() == linePoints[j+1].y()) |
{ |
|
// If not parallel |
if(jx == j1x) |
if(linePoints[i-1].y() > linePoints[j].y() && linePoints[i].y() < linePoints[j].y() || linePoints[i-1].y() < linePoints[j].y() && linePoints[i].y() > linePoints[j].y()) |
{ |
// x equal |
|
if(ix > jx && i1x < jx || ix < jx && i1x > jx) |
{ |
if((sign(iy - jy) != sign(iy - j1y))) |
return true; |
} |
if((sign(linePoints[i-1].x() - linePoints[j].x()) != sign(linePoints[i-1].x() - linePoints[j+1].x()))) |
return true; |
} |
if(jy == j1y) |
{ |
|
if(iy > jy && i1y < jy || iy < jy && i1y > jy) |
{ |
if((sign(ix - jx) != sign(ix - j1x))) |
return true; |
} |
} |
} |
} |
} |
248,12 → 207,6 |
return false; |
} |
|
void Bike::setText(QString text) |
{ |
socket->write(text.toAscii().data()); |
socket->flush(); |
} |
|
void Bike::reset() |
{ |
x = rand() % 800; |
264,7 → 217,6 |
|
velocity = 1; |
angle = 0; |
abpool = 0; |
show = true; |
isReady = true; |
hadGo = false; |
293,14 → 245,6 |
{ |
angle += 360; |
} |
if(velocity < 5) |
velocity += 0.3; |
else if(velocity > 5) |
velocity -= 0.3; |
if(abs(5-velocity)<0.3) |
velocity = 5; |
if(abpool<10) |
abpool += 0.2; |
hadGo = true; |
} |
else if (line == "R") |
315,58 → 259,20 |
{ |
angle += 360; |
} |
if(velocity < 5) |
velocity += 0.3; |
else if(velocity > 5) |
velocity -= 0.3; |
if(abs(5-velocity)<0.3) |
velocity = 5; |
if(abpool<10) |
abpool += 0.2; |
hadGo = true; |
} |
else if (line == "A") |
{ |
if(abpool > 0){ |
velocity += 0.1; |
abpool -= 0.5; |
} else { |
if(velocity < 5) |
velocity += 0.3; |
else if(velocity > 5) |
velocity -= 0.3; |
if(abs(5-velocity)<0.3) |
velocity = 5; |
} |
velocity += 0.1; |
hadGo = true; |
} |
else if (line == "D") |
{ |
if(abpool > 0){ |
velocity -= 0.2; |
abpool -= 0.5; |
} |
else { |
if(velocity < 5) |
velocity += 0.3; |
else if(velocity > 5) |
velocity -= 0.3; |
if(abs(5-velocity)<0.3) |
velocity = 5; |
} |
velocity -= 0.2; |
hadGo = true; |
} |
else if (line == "N") |
{ |
|
if(velocity < 5) |
velocity += 0.3; |
else if(velocity > 5) |
velocity -= 0.3; |
if(abs(5-velocity)<0.3) |
velocity = 5; |
if(abpool<10) |
abpool += 0.2; |
hadGo = true; |
} |
else if (line.startsWith("NAME ")) |
391,17 → 297,6 |
colour.setBlue(list[3].toInt()); |
} |
} |
else if (line.startsWith("CHAT ")) |
{ |
QString message = line.remove(0, 5); |
|
if (!message.isEmpty()) |
{ |
emit chat(name, message); |
} |
|
hadGo = true; |
} |
} |
} |
|