Rev 34 | Rev 36 | 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 | |||
35 | muzer | 40 | void *runtherun(void *bike){ |
41 | Bike *realbike = (Bike *)bike; |
||
42 | realbike->run(); |
||
43 | pthread_exit(NULL); |
||
44 | } |
||
45 | |||
1 | muzer | 46 | void MainWindow::paintEvent(QPaintEvent *e) |
47 | { |
||
32 | tom | 48 | if (suddenDeath) |
49 | { |
||
50 | for (int i = 0; i < bikes.count(); i++) |
||
51 | { |
||
33 | tom | 52 | bikes[i]->speed *= 1.01; |
32 | tom | 53 | } |
54 | } |
||
55 | |||
9 | muzer | 56 | e->accept(); |
57 | QPainter painter(this); |
||
1 | muzer | 58 | |
9 | muzer | 59 | painter.fillRect(0, 0, 800, 600, Qt::white); |
1 | muzer | 60 | |
9 | muzer | 61 | bool ready = true; |
62 | for (int i = 0; i < bikes.count(); i++) |
||
63 | { |
||
64 | if (!bikes[i]->isReady) |
||
65 | { |
||
66 | ready = false; |
||
67 | } |
||
68 | } |
||
1 | muzer | 69 | |
9 | muzer | 70 | if (ready) |
71 | { |
||
34 | tom | 72 | cout << ":: Sent move request..." << endl; |
35 | muzer | 73 | pthread_t threads[bikes.count()]; |
9 | muzer | 74 | for (int i = 0; i < bikes.count(); i++) |
75 | { |
||
76 | if (bikes[i]->show) |
||
77 | { |
||
78 | bikes[i]->hasHadGo = false; |
||
34 | tom | 79 | bikes[i]->bikes = bikes; |
35 | muzer | 80 | pthread_create(&threads[i],NULL,runtherun,(void *)bikes[i]); |
9 | muzer | 81 | } |
82 | } |
||
83 | Bike *bike; |
||
84 | foreach(bike,bikes) |
||
85 | { |
||
86 | while(!bike->hasHadGo) |
||
87 | { |
||
88 | } |
||
89 | } |
||
13 | tom | 90 | |
14 | muzer | 91 | for (int i = 0; i < bikes.count(); i++) |
92 | { |
||
93 | if (bikes[i]->show) |
||
94 | { |
||
95 | bikes[i]->draw(&painter,bikes); |
||
96 | } |
||
97 | } |
||
9 | muzer | 98 | } |
1 | muzer | 99 | |
9 | muzer | 100 | checkClients(); |
1 | muzer | 101 | } |
102 | |||
103 | void MainWindow::newConnection() |
||
104 | { |
||
9 | muzer | 105 | while (server->hasPendingConnections()) |
106 | { |
||
107 | QTcpSocket *socket = server->nextPendingConnection(); |
||
108 | Bike *bike = new Bike(socket, id); |
||
31 | tom | 109 | connect(bike, SIGNAL(chat(QString,QString)), this, SLOT(chat(QString,QString))); |
9 | muzer | 110 | id += 1; |
111 | reset(); |
||
1 | muzer | 112 | |
9 | muzer | 113 | bikes.append(bike); |
114 | cout << ":: New Bike: " << socket->peerAddress().toString().toStdString() << endl; |
||
115 | } |
||
1 | muzer | 116 | } |
117 | |||
118 | void MainWindow::checkClients() |
||
119 | { |
||
27 | tom | 120 | bool everyoneDied = true; |
121 | |||
9 | muzer | 122 | for (int i = bikes.count() - 1; i >= 0; i--) |
123 | { |
||
124 | if (bikes[i]->isDisconnected) |
||
125 | { |
||
30 | tom | 126 | for (int j = 0; j < bikes.count(); j++) |
127 | { |
||
128 | bikes[j]->setText(QString("DISCO ") + bikes[i]->name + QString("\n")); |
||
129 | } |
||
130 | |||
9 | muzer | 131 | delete bikes[i]; |
132 | bikes.removeAt(i); |
||
133 | cout << ":: Removed Bike: " << i << endl; |
||
134 | } |
||
27 | tom | 135 | else |
136 | { |
||
137 | if (!bikes[i]->collided) |
||
138 | { |
||
139 | everyoneDied = false; |
||
140 | } |
||
29 | tom | 141 | else if (bikes[i]->dead) |
27 | tom | 142 | { |
143 | everyoneDied = false; |
||
144 | } |
||
145 | } |
||
9 | muzer | 146 | } |
27 | tom | 147 | |
148 | if (everyoneDied) |
||
149 | { |
||
150 | reset(); |
||
151 | } |
||
1 | muzer | 152 | } |
31 | tom | 153 | |
154 | void MainWindow::chat(QString name, QString message) |
||
155 | { |
||
156 | QString packet = "CHAT "; |
||
157 | packet.append(name); |
||
158 | packet.append(" "); |
||
159 | packet.append(message); |
||
160 | packet.append("\n"); |
||
161 | |||
162 | for (int i = 0; i < bikes.count(); i++) |
||
163 | { |
||
164 | bikes[i]->setText(packet); |
||
165 | } |
||
166 | } |
||
32 | tom | 167 | |
168 | void MainWindow::activateSuddenDeath() |
||
169 | { |
||
170 | if (!suddenDeath) |
||
171 | { |
||
172 | suddenDeath = true; |
||
173 | chat("Server", "SUDDEN DEATH ACTIVATED!"); |
||
174 | } |
||
175 | } |