Subversion Repositories QTron

Compare Revisions

Ignore whitespace Rev 31 → Rev 9

/bike.h
8,7 → 8,6
#include <iostream>
#include <QHostAddress>
#include <QThread>
#include <cmath>
 
using namespace std;
 
18,10 → 17,9
 
public:
Bike(QTcpSocket *sock, int i);
void draw(QPainter *painter,QList<Bike *> bikes);
void draw(QPainter *painter);
void run(QList<Bike *> bikes);
bool hasCollided(QList<Bike *> bikes);
void setText(QString text);
bool hasCollided();
void reset();
bool isReady;
bool dead;
37,18 → 35,14
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,7 → 31,6
private slots:
void newConnection();
void checkClients();
void chat(QString name, QString message);
};
 
#endif // MAINWINDOW_H
/bike.cpp
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;
}
}
}
 
/mainwindow.cpp
4,6 → 4,7
QGLWidget(parent)
{
this->setFixedSize(800, 600);
 
server = new QTcpServer(this);
connect(server, SIGNAL(newConnection()), this, SLOT(newConnection()));
server->listen(QHostAddress::Any, 4567);
52,6 → 53,7
{
bikes[i]->hasHadGo = false;
bikes[i]->run(bikes);
bikes[i]->draw(&painter);
}
}
Bike *bike;
61,14 → 63,6
{
}
}
 
for (int i = 0; i < bikes.count(); i++)
{
if (bikes[i]->show)
{
bikes[i]->draw(&painter,bikes);
}
}
}
 
checkClients();
80,7 → 74,6
{
QTcpSocket *socket = server->nextPendingConnection();
Bike *bike = new Bike(socket, id);
connect(bike, SIGNAL(chat(QString,QString)), this, SLOT(chat(QString,QString)));
id += 1;
reset();
 
91,50 → 84,13
 
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);
}
}