Subversion Repositories QTron

Compare Revisions

Ignore whitespace Rev 3 → Rev 14

/bike.cpp
2,250 → 2,312
 
Bike::Bike(QTcpSocket *sock, int i)
{
socket = sock;
id = i;
socket = sock;
id = i;
 
connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()));
connect(socket, SIGNAL(disconnected()), this, SLOT(disconnected()));
connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()));
connect(socket, SIGNAL(disconnected()), this, SLOT(disconnected()));
 
isDisconnected = false;
isDisconnected = false;
 
x = rand() % 800;
y = rand() % 600;
linePoints.append(QPoint(x, y));
linePoints.append(QPoint(x, y));
x = rand() % 800;
y = rand() % 600;
linePoints.append(QPoint(x, y));
 
velocity = 1;
angle = 0;
name = "";
show = false;
isReady = false;
hadGo = false;
dead = false;
velocity = 1;
angle = 0;
name = "";
show = false;
isReady = false;
hadGo = false;
dead = false;
collided = false;
 
colour.setBlue(0);
colour.setRed(0);
colour.setGreen(0);
colour.setBlue(0);
colour.setRed(0);
colour.setGreen(0);
 
if (!socket->waitForReadyRead(2000))
{
socket->disconnectFromHost();
show = false;
isReady = true;
hadGo = false;
}
if (!socket->waitForReadyRead(2000))
{
socket->disconnectFromHost();
show = false;
isReady = true;
hadGo = false;
}
}
 
void Bike::draw(QPainter *painter)
void Bike::draw(QPainter *painter, QList<Bike *> bikes)
{
if (show)
{
painter->setPen(colour);
painter->setFont(QFont("sans", 12));
if (show)
{
painter->setPen(colour);
painter->setFont(QFont("sans", 12));
 
for (int i = 0; i < linePoints.count(); i++)
{
QPoint point1;
QPoint point2;
if (i == 0)
{
point1 = linePoints[0];
}
else
{
point1 = linePoints[i - 1];
}
for (int i = 0; i < linePoints.count(); i++)
{
QPoint point1;
QPoint point2;
if (i == 0)
{
point1 = linePoints[0];
}
else
{
point1 = linePoints[i - 1];
}
 
point2 = linePoints[i];
point2 = linePoints[i];
 
painter->drawLine(point1, point2);
}
painter->drawLine(point1, point2);
}
 
if (angle == 0)
{
y -= velocity;
}
else if (angle == 90)
{
x += velocity;
}
else if (angle == 180)
{
y += velocity;
}
else if (angle == 270)
{
x -= velocity;
}
if (!collided)
{
collided = hasCollided(bikes);
 
if (angle == 0 || angle == 180)
{
painter->fillRect(x - 5, y - 15, 10, 30, colour);
}
else
{
painter->fillRect(x - 15, y - 5, 30, 10, colour);
}
if (angle == 0)
{
y -= velocity;
}
else if (angle == 90)
{
x += velocity;
}
else if (angle == 180)
{
y += velocity;
}
else if (angle == 270)
{
x -= velocity;
}
}
else
{
linePoints.clear();
}
 
painter->drawText(x, y - 20, name);
}
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);
}
}
 
void Bike::run(QList<Bike *> bikes)
{
for (int i = 0; i < bikes.count(); i++)
{
Bike *bike = bikes[i];
if (bike->isReady && bike->show)
{
if (bike->dead)
{
socket->write("DEAD ");
socket->write(bike->name.toAscii());
socket->write("\n");
}
else
{
socket->write("BIKE ");
socket->write(bike->name.toAscii());
socket->write(" ");
socket->write(QString::number(bike->x).toAscii());
socket->write(" ");
socket->write(QString::number(bike->y).toAscii());
socket->write("\n");
}
}
}
for (int i = 0; i < bikes.count(); i++)
{
Bike *bike = bikes[i];
if (bike->isReady && bike->show)
{
if (bike->dead || bike->collided)
{
socket->write("DEAD ");
socket->write(bike->name.toAscii());
socket->write("\n");
}
else
{
socket->write("BIKE ");
socket->write(bike->name.toAscii());
socket->write(" ");
socket->write(QString::number(bike->x).toAscii());
socket->write(" ");
socket->write(QString::number(bike->y).toAscii());
socket->write(" ");
socket->write(QString::number(bike->colour.red()).toAscii());
socket->write(" ");
socket->write(QString::number(bike->colour.green()).toAscii());
socket->write(" ");
socket->write(QString::number(bike->colour.blue()).toAscii());
socket->write("\n");
}
}
}
 
if (dead)
{
show = false;
isReady = true;
hadGo = false;
dead = false;
}
linePoints.append(QPoint(x, y));
 
if (!dead)
{
socket->write("G\n");
socket->flush();
if (dead)
{
show = false;
isReady = true;
hadGo = false;
dead = false;
}
 
hadGo = false;
if (!socket->waitForReadyRead(2000))
{
socket->disconnectFromHost();
dead = true;
}
if (!hadGo)
{
socket->disconnectFromHost();
dead = true;
}
}
hasHadGo = true;
if (!dead)
{
socket->write("G\n");
socket->flush();
 
hadGo = false;
if (!socket->waitForReadyRead(2000))
{
socket->disconnectFromHost();
dead = true;
}
if (!hadGo)
{
socket->disconnectFromHost();
dead = true;
}
}
hasHadGo = true;
}
 
int sign(float x){
if(x > 0){
return 1;
}
if(x < 0){
return -1;
}
return 0;
}
 
bool Bike::hasCollided(QList<Bike *> bikes)
{
// 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)
return true;
if(linePoints[i].x() < 0 || linePoints[i].x() > 800 || linePoints[i].y() < 0 || linePoints[i].y() > 600)
return true;
int j;
Bike *bike;
foreach(bike,bikes)
{
for(j = 0; j < bike->linePoints.count() - 2; j++)
{
if(!(bike->linePoints[j].x() == bike->linePoints[j+1].x() && linePoints[i].x() == linePoints[i-1].x()) && !(bike->linePoints[j].y() == bike->linePoints[j+1].y() && linePoints[i].y() == linePoints[i-1].y()))
{
// If not parallel
if(bike->linePoints[j].x() == bike->linePoints[j+1].x())
{
// x equal
if(linePoints[i-1].x() > bike->linePoints[j].x() && linePoints[i].x() < bike->linePoints[j].x() || linePoints[i-1].x() < bike->linePoints[j].x() && linePoints[i].x() > bike->linePoints[j].x()){
if((sign(linePoints[i-1].y() - bike->linePoints[j].y()) != sign(linePoints[i-1].y() - bike->linePoints[j+1].y())))
return true;
}
}
else if(bike->linePoints[j].y() == bike->linePoints[j+1].y())
{
if(linePoints[i-1].y() > bike->linePoints[j].y() && linePoints[i].y() < bike->linePoints[j].y() || linePoints[i-1].y() < bike->linePoints[j].y() && linePoints[i].y() > bike->linePoints[j].y())
{
if((sign(linePoints[i-1].x() - bike->linePoints[j].x()) != sign(linePoints[i-1].x() - bike->linePoints[j+1].x())))
return true;
}
}
}
}
}
return false;
}
 
void Bike::reset()
{
x = rand() % 800;
y = rand() % 600;
linePoints.clear();
x = rand() % 800;
y = rand() % 600;
linePoints.clear();
 
linePoints.append(QPoint(x, y));
linePoints.append(QPoint(x, y));
linePoints.append(QPoint(x, y));
 
velocity = 1;
angle = 0;
show = true;
isReady = true;
hadGo = false;
dead = false;
velocity = 1;
angle = 0;
show = true;
isReady = true;
hadGo = false;
dead = false;
collided = false;
 
socket->write("RESET\n");
socket->write("RESET\n");
}
 
void Bike::readyRead()
{
while (socket->canReadLine())
{
QByteArray data = socket->readLine();
QString line = data.trimmed();
while (socket->canReadLine())
{
QByteArray data = socket->readLine();
QString line = data.trimmed();
 
if (line == "L")
{
linePoints.append(QPoint(x, y));
angle -= 90;
if (line == "L")
{
angle -= 90;
 
if (angle >= 360)
{
angle -= 360;
}
if (angle < 0)
{
angle += 360;
}
hadGo = true;
}
else if (line == "R")
{
linePoints.append(QPoint(x, y));
angle += 90;
if (angle >= 360)
{
angle -= 360;
}
if (angle < 0)
{
angle += 360;
}
hadGo = true;
}
else if (line == "R")
{
angle += 90;
 
if (angle >= 360)
{
angle -= 360;
}
if (angle < 0)
{
angle += 360;
}
hadGo = true;
}
else if (line == "A")
{
linePoints[linePoints.size() - 1] = QPoint(x, y);
velocity += 0.1;
hadGo = true;
}
else if (line == "D")
{
linePoints[linePoints.size() - 1] = QPoint(x, y);
velocity -= 0.2;
hadGo = true;
}
else if (line == "N")
{
linePoints[linePoints.size() - 1] = QPoint(x, y);
hadGo = true;
}
else if (line.startsWith("NAME "))
{
name = line.remove(0, 5);
isReady = true;
show = true;
}
else if (line.startsWith("COLOUR "))
{
QStringList list = line.split(" ");
if (list.count() >= 2)
{
colour.setRed(list[1].toInt());
}
else if (list.count() >= 3)
{
colour.setGreen(list[2].toInt());
}
else if (list.count() >= 4)
{
colour.setBlue(list[3].toInt());
}
}
}
if (angle >= 360)
{
angle -= 360;
}
if (angle < 0)
{
angle += 360;
}
hadGo = true;
}
else if (line == "A")
{
velocity += 0.1;
hadGo = true;
}
else if (line == "D")
{
velocity -= 0.2;
hadGo = true;
}
else if (line == "N")
{
hadGo = true;
}
else if (line.startsWith("NAME "))
{
name = line.remove(0, 5);
isReady = true;
show = true;
}
else if (line.startsWith("COLOUR "))
{
QStringList list = line.split(" ");
if (list.count() >= 2)
{
colour.setRed(list[1].toInt());
}
if (list.count() >= 3)
{
colour.setGreen(list[2].toInt());
}
if (list.count() >= 4)
{
colour.setBlue(list[3].toInt());
}
}
}
}
 
void Bike::disconnected()
{
dead = true;
isDisconnected = true;
cout << ":: Disconnected: " << socket->peerAddress().toString().toStdString() << endl;
dead = true;
isDisconnected = true;
cout << ":: Disconnected: " << socket->peerAddress().toString().toStdString() << endl;
}
/mainwindow.cpp
3,94 → 3,101
MainWindow::MainWindow(QWidget *parent) :
QGLWidget(parent)
{
this->setFixedSize(800, 600);
this->setFixedSize(800, 600);
 
server = new QTcpServer(this);
connect(server, SIGNAL(newConnection()), this, SLOT(newConnection()));
server->listen(QHostAddress::Any, 4567);
server = new QTcpServer(this);
connect(server, SIGNAL(newConnection()), this, SLOT(newConnection()));
server->listen(QHostAddress::Any, 4567);
 
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(update()));
timer->start(50);
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(update()));
timer->start(50);
 
id = 0;
id = 0;
}
 
MainWindow::~MainWindow()
{
delete server;
delete server;
}
 
void MainWindow::reset()
{
for (int i = 0; i < bikes.count(); i++)
{
bikes[i]->reset();
}
for (int i = 0; i < bikes.count(); i++)
{
bikes[i]->reset();
}
}
 
void MainWindow::paintEvent(QPaintEvent *e)
{
e->accept();
QPainter painter(this);
e->accept();
QPainter painter(this);
 
painter.fillRect(0, 0, 800, 600, Qt::white);
painter.fillRect(0, 0, 800, 600, Qt::white);
 
bool ready = true;
for (int i = 0; i < bikes.count(); i++)
{
if (!bikes[i]->isReady)
{
ready = false;
}
}
bool ready = true;
for (int i = 0; i < bikes.count(); i++)
{
if (!bikes[i]->isReady)
{
ready = false;
}
}
 
if (ready)
{
for (int i = 0; i < bikes.count(); i++)
{
if (bikes[i]->show)
{
bikes[i]->hasHadGo = false;
bikes[i]->run(bikes);
bikes[i]->draw(&painter);
}
}
Bike *bike;
foreach(bike,bikes)
{
while(!bike->hasHadGo)
{
}
}
}
if (ready)
{
for (int i = 0; i < bikes.count(); i++)
{
if (bikes[i]->show)
{
bikes[i]->hasHadGo = false;
bikes[i]->run(bikes);
}
}
Bike *bike;
foreach(bike,bikes)
{
while(!bike->hasHadGo)
{
}
}
 
checkClients();
for (int i = 0; i < bikes.count(); i++)
{
if (bikes[i]->show)
{
bikes[i]->draw(&painter,bikes);
}
}
}
 
checkClients();
}
 
void MainWindow::newConnection()
{
while (server->hasPendingConnections())
{
QTcpSocket *socket = server->nextPendingConnection();
Bike *bike = new Bike(socket, id);
id += 1;
reset();
while (server->hasPendingConnections())
{
QTcpSocket *socket = server->nextPendingConnection();
Bike *bike = new Bike(socket, id);
id += 1;
reset();
 
bikes.append(bike);
cout << ":: New Bike: " << socket->peerAddress().toString().toStdString() << endl;
}
bikes.append(bike);
cout << ":: New Bike: " << socket->peerAddress().toString().toStdString() << endl;
}
}
 
void MainWindow::checkClients()
{
for (int i = bikes.count() - 1; i >= 0; i--)
{
if (bikes[i]->isDisconnected)
{
delete bikes[i];
bikes.removeAt(i);
cout << ":: Removed Bike: " << i << endl;
}
}
for (int i = bikes.count() - 1; i >= 0; i--)
{
if (bikes[i]->isDisconnected)
{
delete bikes[i];
bikes.removeAt(i);
cout << ":: Removed Bike: " << i << endl;
}
}
}
/bike.h
13,34 → 13,36
 
class Bike : public QThread
{
Q_OBJECT
Q_OBJECT
 
public:
Bike(QTcpSocket *sock, int i);
void draw(QPainter *painter);
void run(QList<Bike *> bikes);
void reset();
bool isReady;
bool dead;
bool show;
bool hadGo;
float x;
float y;
QString name;
int angle;
QList<QPoint> linePoints;
int id;
bool isDisconnected;
bool hasHadGo;
public:
Bike(QTcpSocket *sock, int i);
void draw(QPainter *painter);
void run(QList<Bike *> bikes);
bool hasCollided();
void reset();
bool isReady;
bool dead;
bool show;
bool hadGo;
float x;
float y;
QString name;
int angle;
QList<QPoint> linePoints;
int id;
bool isDisconnected;
bool hasHadGo;
QColor colour;
bool collided;
 
private:
QTcpSocket *socket;
float velocity;
QColor colour;
private:
QTcpSocket *socket;
float velocity;
 
private slots:
void readyRead();
void disconnected();
void readyRead();
void disconnected();
};
 
#endif // BIKE_H
/mainwindow.h
14,23 → 14,23
{
Q_OBJECT
 
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
void reset();
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
void reset();
 
protected:
void paintEvent(QPaintEvent *e);
protected:
void paintEvent(QPaintEvent *e);
 
private:
QTcpServer *server;
QList<Bike *> bikes;
QTimer *timer;
int id;
private:
QTcpServer *server;
QList<Bike *> bikes;
QTimer *timer;
int id;
 
private slots:
void newConnection();
void checkClients();
void newConnection();
void checkClients();
};
 
#endif // MAINWINDOW_H