Rev 33 | Rev 35 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1 | muzer | 1 | #include "mainwindow.h" |
2 | |||
3 | MainWindow::MainWindow(QWidget *parent) : |
||
3 | tom | 4 | QGLWidget(parent) |
1 | muzer | 5 | { |
9 | muzer | 6 | this->setFixedSize(800, 600); |
7 | server = new QTcpServer(this); |
||
8 | connect(server, SIGNAL(newConnection()), this, SLOT(newConnection())); |
||
9 | server->listen(QHostAddress::Any, 4567); |
||
1 | muzer | 10 | |
9 | muzer | 11 | timer = new QTimer(this); |
12 | connect(timer, SIGNAL(timeout()), this, SLOT(update())); |
||
13 | timer->start(50); |
||
1 | muzer | 14 | |
32 | tom | 15 | suddenDeath = false; |
16 | suddenDeathTimer = new QTimer(this); |
||
17 | connect(suddenDeathTimer, SIGNAL(timeout()), this, SLOT(activateSuddenDeath())); |
||
18 | suddenDeathTimer->start(300000); |
||
19 | |||
9 | muzer | 20 | id = 0; |
1 | muzer | 21 | } |
22 | |||
23 | MainWindow::~MainWindow() |
||
24 | { |
||
9 | muzer | 25 | delete server; |
1 | muzer | 26 | } |
27 | |||
28 | void MainWindow::reset() |
||
29 | { |
||
32 | tom | 30 | suddenDeathTimer->stop(); |
31 | suddenDeathTimer->start(300000); |
||
32 | suddenDeath = false; |
||
33 | |||
9 | muzer | 34 | for (int i = 0; i < bikes.count(); i++) |
35 | { |
||
36 | bikes[i]->reset(); |
||
37 | } |
||
1 | muzer | 38 | } |
39 | |||
40 | void MainWindow::paintEvent(QPaintEvent *e) |
||
41 | { |
||
32 | tom | 42 | if (suddenDeath) |
43 | { |
||
44 | for (int i = 0; i < bikes.count(); i++) |
||
45 | { |
||
33 | tom | 46 | bikes[i]->speed *= 1.01; |
32 | tom | 47 | } |
48 | } |
||
49 | |||
9 | muzer | 50 | e->accept(); |
51 | QPainter painter(this); |
||
1 | muzer | 52 | |
9 | muzer | 53 | painter.fillRect(0, 0, 800, 600, Qt::white); |
1 | muzer | 54 | |
9 | muzer | 55 | bool ready = true; |
56 | for (int i = 0; i < bikes.count(); i++) |
||
57 | { |
||
58 | if (!bikes[i]->isReady) |
||
59 | { |
||
60 | ready = false; |
||
61 | } |
||
62 | } |
||
1 | muzer | 63 | |
9 | muzer | 64 | if (ready) |
65 | { |
||
34 | tom | 66 | cout << ":: Sent move request..." << endl; |
9 | muzer | 67 | for (int i = 0; i < bikes.count(); i++) |
68 | { |
||
69 | if (bikes[i]->show) |
||
70 | { |
||
71 | bikes[i]->hasHadGo = false; |
||
34 | tom | 72 | bikes[i]->bikes = bikes; |
73 | bikes[i]->start(); |
||
9 | muzer | 74 | } |
75 | } |
||
76 | Bike *bike; |
||
77 | foreach(bike,bikes) |
||
78 | { |
||
79 | while(!bike->hasHadGo) |
||
80 | { |
||
81 | } |
||
82 | } |
||
13 | tom | 83 | |
14 | muzer | 84 | for (int i = 0; i < bikes.count(); i++) |
85 | { |
||
86 | if (bikes[i]->show) |
||
87 | { |
||
88 | bikes[i]->draw(&painter,bikes); |
||
89 | } |
||
90 | } |
||
9 | muzer | 91 | } |
1 | muzer | 92 | |
9 | muzer | 93 | checkClients(); |
1 | muzer | 94 | } |
95 | |||
96 | void MainWindow::newConnection() |
||
97 | { |
||
9 | muzer | 98 | while (server->hasPendingConnections()) |
99 | { |
||
100 | QTcpSocket *socket = server->nextPendingConnection(); |
||
101 | Bike *bike = new Bike(socket, id); |
||
31 | tom | 102 | connect(bike, SIGNAL(chat(QString,QString)), this, SLOT(chat(QString,QString))); |
9 | muzer | 103 | id += 1; |
104 | reset(); |
||
1 | muzer | 105 | |
9 | muzer | 106 | bikes.append(bike); |
107 | cout << ":: New Bike: " << socket->peerAddress().toString().toStdString() << endl; |
||
108 | } |
||
1 | muzer | 109 | } |
110 | |||
111 | void MainWindow::checkClients() |
||
112 | { |
||
27 | tom | 113 | bool everyoneDied = true; |
114 | |||
9 | muzer | 115 | for (int i = bikes.count() - 1; i >= 0; i--) |
116 | { |
||
117 | if (bikes[i]->isDisconnected) |
||
118 | { |
||
30 | tom | 119 | for (int j = 0; j < bikes.count(); j++) |
120 | { |
||
121 | bikes[j]->setText(QString("DISCO ") + bikes[i]->name + QString("\n")); |
||
122 | } |
||
123 | |||
9 | muzer | 124 | delete bikes[i]; |
125 | bikes.removeAt(i); |
||
126 | cout << ":: Removed Bike: " << i << endl; |
||
127 | } |
||
27 | tom | 128 | else |
129 | { |
||
130 | if (!bikes[i]->collided) |
||
131 | { |
||
132 | everyoneDied = false; |
||
133 | } |
||
29 | tom | 134 | else if (bikes[i]->dead) |
27 | tom | 135 | { |
136 | everyoneDied = false; |
||
137 | } |
||
138 | } |
||
9 | muzer | 139 | } |
27 | tom | 140 | |
141 | if (everyoneDied) |
||
142 | { |
||
143 | reset(); |
||
144 | } |
||
1 | muzer | 145 | } |
31 | tom | 146 | |
147 | void MainWindow::chat(QString name, QString message) |
||
148 | { |
||
149 | QString packet = "CHAT "; |
||
150 | packet.append(name); |
||
151 | packet.append(" "); |
||
152 | packet.append(message); |
||
153 | packet.append("\n"); |
||
154 | |||
155 | for (int i = 0; i < bikes.count(); i++) |
||
156 | { |
||
157 | bikes[i]->setText(packet); |
||
158 | } |
||
159 | } |
||
32 | tom | 160 | |
161 | void MainWindow::activateSuddenDeath() |
||
162 | { |
||
163 | if (!suddenDeath) |
||
164 | { |
||
165 | suddenDeath = true; |
||
166 | chat("Server", "SUDDEN DEATH ACTIVATED!"); |
||
167 | } |
||
168 | } |