Rev 31 | Rev 33 | 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 | { |
||
46 | bikes[i]->speed += 0.02; |
||
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 | { |
||
66 | for (int i = 0; i < bikes.count(); i++) |
||
67 | { |
||
68 | if (bikes[i]->show) |
||
69 | { |
||
70 | bikes[i]->hasHadGo = false; |
||
71 | bikes[i]->run(bikes); |
||
72 | } |
||
73 | } |
||
74 | Bike *bike; |
||
75 | foreach(bike,bikes) |
||
76 | { |
||
77 | while(!bike->hasHadGo) |
||
78 | { |
||
79 | } |
||
80 | } |
||
13 | tom | 81 | |
14 | muzer | 82 | for (int i = 0; i < bikes.count(); i++) |
83 | { |
||
84 | if (bikes[i]->show) |
||
85 | { |
||
86 | bikes[i]->draw(&painter,bikes); |
||
87 | } |
||
88 | } |
||
9 | muzer | 89 | } |
1 | muzer | 90 | |
9 | muzer | 91 | checkClients(); |
1 | muzer | 92 | } |
93 | |||
94 | void MainWindow::newConnection() |
||
95 | { |
||
9 | muzer | 96 | while (server->hasPendingConnections()) |
97 | { |
||
98 | QTcpSocket *socket = server->nextPendingConnection(); |
||
99 | Bike *bike = new Bike(socket, id); |
||
31 | tom | 100 | connect(bike, SIGNAL(chat(QString,QString)), this, SLOT(chat(QString,QString))); |
9 | muzer | 101 | id += 1; |
102 | reset(); |
||
1 | muzer | 103 | |
9 | muzer | 104 | bikes.append(bike); |
105 | cout << ":: New Bike: " << socket->peerAddress().toString().toStdString() << endl; |
||
106 | } |
||
1 | muzer | 107 | } |
108 | |||
109 | void MainWindow::checkClients() |
||
110 | { |
||
27 | tom | 111 | bool everyoneDied = true; |
112 | |||
9 | muzer | 113 | for (int i = bikes.count() - 1; i >= 0; i--) |
114 | { |
||
115 | if (bikes[i]->isDisconnected) |
||
116 | { |
||
30 | tom | 117 | for (int j = 0; j < bikes.count(); j++) |
118 | { |
||
119 | bikes[j]->setText(QString("DISCO ") + bikes[i]->name + QString("\n")); |
||
120 | } |
||
121 | |||
9 | muzer | 122 | delete bikes[i]; |
123 | bikes.removeAt(i); |
||
124 | cout << ":: Removed Bike: " << i << endl; |
||
125 | } |
||
27 | tom | 126 | else |
127 | { |
||
128 | if (!bikes[i]->collided) |
||
129 | { |
||
130 | everyoneDied = false; |
||
131 | } |
||
29 | tom | 132 | else if (bikes[i]->dead) |
27 | tom | 133 | { |
134 | everyoneDied = false; |
||
135 | } |
||
136 | } |
||
9 | muzer | 137 | } |
27 | tom | 138 | |
139 | if (everyoneDied) |
||
140 | { |
||
141 | reset(); |
||
142 | } |
||
1 | muzer | 143 | } |
31 | tom | 144 | |
145 | void MainWindow::chat(QString name, QString message) |
||
146 | { |
||
147 | QString packet = "CHAT "; |
||
148 | packet.append(name); |
||
149 | packet.append(" "); |
||
150 | packet.append(message); |
||
151 | packet.append("\n"); |
||
152 | |||
153 | for (int i = 0; i < bikes.count(); i++) |
||
154 | { |
||
155 | bikes[i]->setText(packet); |
||
156 | } |
||
157 | } |
||
32 | tom | 158 | |
159 | void MainWindow::activateSuddenDeath() |
||
160 | { |
||
161 | if (!suddenDeath) |
||
162 | { |
||
163 | suddenDeath = true; |
||
164 | chat("Server", "SUDDEN DEATH ACTIVATED!"); |
||
165 | } |
||
166 | } |