Rev 24 | Rev 30 | 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 | |
9 | muzer | 15 | id = 0; |
1 | muzer | 16 | } |
17 | |||
18 | MainWindow::~MainWindow() |
||
19 | { |
||
9 | muzer | 20 | delete server; |
1 | muzer | 21 | } |
22 | |||
23 | void MainWindow::reset() |
||
24 | { |
||
9 | muzer | 25 | for (int i = 0; i < bikes.count(); i++) |
26 | { |
||
27 | bikes[i]->reset(); |
||
28 | } |
||
1 | muzer | 29 | } |
30 | |||
31 | void MainWindow::paintEvent(QPaintEvent *e) |
||
32 | { |
||
9 | muzer | 33 | e->accept(); |
34 | QPainter painter(this); |
||
1 | muzer | 35 | |
9 | muzer | 36 | painter.fillRect(0, 0, 800, 600, Qt::white); |
1 | muzer | 37 | |
9 | muzer | 38 | bool ready = true; |
39 | for (int i = 0; i < bikes.count(); i++) |
||
40 | { |
||
41 | if (!bikes[i]->isReady) |
||
42 | { |
||
43 | ready = false; |
||
44 | } |
||
45 | } |
||
1 | muzer | 46 | |
9 | muzer | 47 | if (ready) |
48 | { |
||
49 | for (int i = 0; i < bikes.count(); i++) |
||
50 | { |
||
51 | if (bikes[i]->show) |
||
52 | { |
||
53 | bikes[i]->hasHadGo = false; |
||
54 | bikes[i]->run(bikes); |
||
55 | } |
||
56 | } |
||
57 | Bike *bike; |
||
58 | foreach(bike,bikes) |
||
59 | { |
||
60 | while(!bike->hasHadGo) |
||
61 | { |
||
62 | } |
||
63 | } |
||
13 | tom | 64 | |
14 | muzer | 65 | for (int i = 0; i < bikes.count(); i++) |
66 | { |
||
67 | if (bikes[i]->show) |
||
68 | { |
||
69 | bikes[i]->draw(&painter,bikes); |
||
70 | } |
||
71 | } |
||
9 | muzer | 72 | } |
1 | muzer | 73 | |
9 | muzer | 74 | checkClients(); |
1 | muzer | 75 | } |
76 | |||
77 | void MainWindow::newConnection() |
||
78 | { |
||
9 | muzer | 79 | while (server->hasPendingConnections()) |
80 | { |
||
81 | QTcpSocket *socket = server->nextPendingConnection(); |
||
82 | Bike *bike = new Bike(socket, id); |
||
83 | id += 1; |
||
84 | reset(); |
||
1 | muzer | 85 | |
9 | muzer | 86 | bikes.append(bike); |
87 | cout << ":: New Bike: " << socket->peerAddress().toString().toStdString() << endl; |
||
88 | } |
||
1 | muzer | 89 | } |
90 | |||
91 | void MainWindow::checkClients() |
||
92 | { |
||
27 | tom | 93 | bool everyoneDied = true; |
94 | |||
9 | muzer | 95 | for (int i = bikes.count() - 1; i >= 0; i--) |
96 | { |
||
97 | if (bikes[i]->isDisconnected) |
||
98 | { |
||
99 | delete bikes[i]; |
||
100 | bikes.removeAt(i); |
||
101 | cout << ":: Removed Bike: " << i << endl; |
||
102 | } |
||
27 | tom | 103 | else |
104 | { |
||
105 | if (!bikes[i]->collided) |
||
106 | { |
||
107 | everyoneDied = false; |
||
108 | } |
||
109 | else if (!bikes[i]->dead) |
||
110 | { |
||
111 | everyoneDied = false; |
||
112 | } |
||
113 | } |
||
9 | muzer | 114 | } |
27 | tom | 115 | |
116 | if (everyoneDied) |
||
117 | { |
||
118 | reset(); |
||
119 | } |
||
1 | muzer | 120 | } |