/QTron.pro |
---|
6,8 → 6,10 |
TEMPLATE = app |
SOURCES += main.cpp \ |
mainwindow.cpp \ |
bike.cpp |
bike.cpp \ |
speccy.cpp |
HEADERS += mainwindow.h \ |
bike.h |
bike.h \ |
speccy.h |
FORMS += |
OTHER_FILES += docs.txt |
/mainwindow.h |
---|
11,6 → 11,7 |
#include "bike.h" |
#include "speccy.h" |
class MainWindow : public QGLWidget |
{ |
26,7 → 27,9 |
private: |
QTcpServer *server; |
QTcpServer *respec; |
QList<Bike *> bikes; |
QList<Speccy *> specs; |
QTimer *timer; |
int id; |
QTimer *suddenDeathTimer; |
35,6 → 38,7 |
private slots: |
void newConnection(); |
void newSpec(); |
void checkClients(); |
void chat(QString name, QString message); |
void activateSuddenDeath(); |
/bike.cpp |
---|
12,7 → 12,7 |
x = rand() % 80000; |
y = rand() % 60000; |
linePoints.append(QPoint(x, y)); |
//linePoints.append(QPoint(x, y)); |
velocity = 100; |
angle = (rand() % 4) * 90; |
35,7 → 35,7 |
{ |
socket->disconnectFromHost(); |
show = false; |
isReady = true; |
//isReady = true; |
hadGo = false; |
} |
} |
175,10 → 175,12 |
dead = true; |
} |
} |
hasHadGo = true; |
if(hadGo || dead) |
hasHadGo = true; |
time_t result = time(NULL); |
cout << result << ": Recieved next move from " << name.toStdString().c_str() << endl; |
if(hasHadGo) |
cout << result << ": Recieved next move from " << name.toStdString().c_str() << endl; |
} |
int sign(int x){ |
196,6 → 198,8 |
// Do collision detection here |
// use linePoints |
int i = linePoints.count() - 1; |
if(i <= 0) |
return false; |
if(linePoints[i-1].x() < 0 || linePoints[i-1].x() >= 80000 || linePoints[i-1].y() < 0 || linePoints[i-1].y() >= 60000) |
return true; |
if(linePoints[i].x() < 0 || linePoints[i].x() >= 80000 || linePoints[i].y() < 0 || linePoints[i].y() >= 60000) |
276,9 → 280,10 |
cout << "Reset was called\n"; |
x = rand() % 80000; |
y = rand() % 60000; |
cout << "x: " << x << " y: " << y << endl; |
linePoints.clear(); |
linePoints.append(QPoint(x, y)); |
//linePoints.append(QPoint(x, y)); |
velocity = 100; |
angle = 0; |
305,6 → 310,8 |
{ |
QByteArray data = socket->readLine(); |
QString line = data.trimmed(); |
std::cout << "Bike " << name.toStdString() << " " << line.toStdString() |
<< std::endl; |
couldReadLine = true; |
if (line == "L") |
/mainwindow.cpp |
---|
8,6 → 8,10 |
connect(server, SIGNAL(newConnection()), this, SLOT(newConnection())); |
server->listen(QHostAddress::Any, 4567); |
respec = new QTcpServer(this); |
connect(respec, SIGNAL(newConnection()), this, SLOT(newSpec())); |
respec->listen(QHostAddress::Any, 4568); |
timer = new QTimer(this); |
connect(timer, SIGNAL(timeout()), this, SLOT(update())); |
timer->start(50); |
57,6 → 61,11 |
{ |
bikes[i]->reset(); |
} |
foreach (Speccy *spec, specs) |
{ |
spec->reset(); |
} |
} |
void MainWindow::paintEvent(QPaintEvent *e) |
90,6 → 99,11 |
// cout << ":: Sent move request..." << endl; |
bool alldone = false; |
time_t start = time(NULL); |
foreach (Speccy *spec, specs) |
{ |
spec->bikes = bikes; |
spec->run(); |
} |
while(!alldone){ |
for (int i = 0; i < bikes.count(); i++) |
{ |
110,11 → 124,14 |
} |
} |
for (int i = 0; i < bikes.count(); i++) |
if(ready) |
{ |
if (bikes[i]->show) |
for (int i = 0; i < bikes.count(); i++) |
{ |
bikes[i]->draw(&painter,bikes); |
if (bikes[i]->show) |
{ |
bikes[i]->draw(&painter,bikes); |
} |
} |
} |
137,10 → 154,26 |
} |
} |
void MainWindow::newSpec() |
{ |
while(respec->hasPendingConnections()) |
{ |
QTcpSocket *socket = respec->nextPendingConnection(); |
Speccy *speccy = new Speccy(socket); |
specs.append(speccy); |
cout << ":: RESPEC!!!!!!! " << socket->peerAddress().toString().toStdString() << endl; |
} |
} |
void MainWindow::checkClients() |
{ |
bool everyoneDied = true; |
foreach (Speccy *spec, specs) |
{ |
if(spec->isDisconnected) |
specs.removeAll(spec); |
} |
for (int i = bikes.count() - 1; i >= 0; i--) |
{ |
if (bikes[i]->isDisconnected) |
147,7 → 180,8 |
{ |
for (int j = 0; j < bikes.count(); j++) |
{ |
bikes[j]->setText(QString("DISCO ") + bikes[i]->name + QString("\n")); |
if(bikes[i]->isReady) |
bikes[j]->setText(QString("DISCO ") + bikes[i]->name + QString("\n")); |
} |
delete bikes[i]; |
182,6 → 216,11 |
packet.append(message); |
packet.append("\n"); |
foreach (Speccy *spec, specs) |
{ |
spec->setText(packet); |
} |
for (int i = 0; i < bikes.count(); i++) |
{ |
bikes[i]->setText(packet); |