Subversion Repositories QTron

Compare Revisions

Ignore whitespace Rev 13 → Rev 22

/bike.cpp
16,6 → 16,7
 
velocity = 1;
angle = 0;
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;
107,7 → 107,7
Bike *bike = bikes[i];
if (bike->isReady && bike->show)
{
if (bike->dead || bike->collided)
if (bike->dead || bike->collided)
{
socket->write("DEAD ");
socket->write(bike->name.toAscii());
162,7 → 162,7
hasHadGo = true;
}
 
int sign(float x){
int sign(int x){
if(x > 0){
return 1;
}
172,7 → 172,7
return 0;
}
 
bool Bike::hasCollided()
bool Bike::hasCollided(QList<Bike *> bikes)
{
// Do collision detection here
// use linePoints
181,26 → 181,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;
}
}
}
}
}
217,6 → 256,7
 
velocity = 1;
angle = 0;
abpool = 0;
show = true;
isReady = true;
hadGo = false;
245,6 → 285,14
{
angle += 360;
}
if(velocity < 5)
velocity += 0.5;
else if(velocity > 5)
velocity -= 0.5;
if(abs(5-velocity)<0.5)
velocity = 5;
if(abpool<10)
abpool += 0.2;
hadGo = true;
}
else if (line == "R")
259,20 → 307,57
{
angle += 360;
}
if(velocity < 5)
velocity += 0.5;
else if(velocity > 5)
velocity -= 0.5;
if(abs(5-velocity)<0.5)
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.5;
else if(velocity > 5)
velocity -= 0.5;
if(abs(5-velocity)<0.5)
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.5;
else if(velocity > 5)
velocity -= 0.5;
if(abs(5-velocity)<0.5)
velocity = 5;
}
hadGo = true;
}
else if (line == "N")
{
if(velocity < 5)
velocity += 0.5;
else if(velocity > 5)
velocity -= 0.5;
if(abs(5-velocity)<0.5)
velocity = 5;
if(abpool<10)
abpool += 0.2;
hadGo = true;
}
else if (line.startsWith("NAME "))
/bike.h
17,9 → 17,9
 
public:
Bike(QTcpSocket *sock, int i);
void draw(QPainter *painter);
void draw(QPainter *painter,QList<Bike *> bikes);
void run(QList<Bike *> bikes);
bool hasCollided();
bool hasCollided(QList<Bike *> bikes);
void reset();
bool isReady;
bool dead;
35,6 → 35,7
bool hasHadGo;
QColor colour;
bool collided;
float abpool;
 
private:
QTcpSocket *socket;
/mainwindow.cpp
63,13 → 63,13
}
}
 
for (int i = 0; i < bikes.count(); i++)
{
if (bikes[i]->show)
{
bikes[i]->draw(&painter);
}
}
for (int i = 0; i < bikes.count(); i++)
{
if (bikes[i]->show)
{
bikes[i]->draw(&painter,bikes);
}
}
}
 
checkClients();