Subversion Repositories QTron

Compare Revisions

Ignore whitespace Rev 9 → Rev 31

/bike.h
8,6 → 8,7
#include <iostream>
#include <QHostAddress>
#include <QThread>
#include <cmath>
 
using namespace std;
 
17,9 → 18,10
 
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 setText(QString text);
void reset();
bool isReady;
bool dead;
35,14 → 37,18
bool hasHadGo;
QColor colour;
bool collided;
float abpool;
 
private:
QTcpSocket *socket;
float velocity;
float velocity;
 
private slots:
private slots:
void readyRead();
void disconnected();
 
signals:
void chat(QString name, QString message);
};
 
#endif // BIKE_H
/mainwindow.h
31,6 → 31,7
private slots:
void newConnection();
void checkClients();
void chat(QString name, QString message);
};
 
#endif // MAINWINDOW_H
/bike.cpp
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;
}
}
}
 
/mainwindow.cpp
4,7 → 4,6
QGLWidget(parent)
{
this->setFixedSize(800, 600);
 
server = new QTcpServer(this);
connect(server, SIGNAL(newConnection()), this, SLOT(newConnection()));
server->listen(QHostAddress::Any, 4567);
53,7 → 52,6
{
bikes[i]->hasHadGo = false;
bikes[i]->run(bikes);
bikes[i]->draw(&painter);
}
}
Bike *bike;
63,6 → 61,14
{
}
}
 
for (int i = 0; i < bikes.count(); i++)
{
if (bikes[i]->show)
{
bikes[i]->draw(&painter,bikes);
}
}
}
 
checkClients();
74,6 → 80,7
{
QTcpSocket *socket = server->nextPendingConnection();
Bike *bike = new Bike(socket, id);
connect(bike, SIGNAL(chat(QString,QString)), this, SLOT(chat(QString,QString)));
id += 1;
reset();
 
84,13 → 91,50
 
void MainWindow::checkClients()
{
bool everyoneDied = true;
 
for (int i = bikes.count() - 1; i >= 0; i--)
{
if (bikes[i]->isDisconnected)
{
for (int j = 0; j < bikes.count(); j++)
{
bikes[j]->setText(QString("DISCO ") + bikes[i]->name + QString("\n"));
}
 
delete bikes[i];
bikes.removeAt(i);
cout << ":: Removed Bike: " << i << endl;
}
else
{
if (!bikes[i]->collided)
{
everyoneDied = false;
}
else if (bikes[i]->dead)
{
everyoneDied = false;
}
}
}
 
if (everyoneDied)
{
reset();
}
}
 
void MainWindow::chat(QString name, QString message)
{
QString packet = "CHAT ";
packet.append(name);
packet.append(" ");
packet.append(message);
packet.append("\n");
 
for (int i = 0; i < bikes.count(); i++)
{
bikes[i]->setText(packet);
}
}