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