Rev 30 | Rev 32 | 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); |
||
31 | tom | 83 | connect(bike, SIGNAL(chat(QString,QString)), this, SLOT(chat(QString,QString))); |
9 | muzer | 84 | id += 1; |
85 | reset(); |
||
1 | muzer | 86 | |
9 | muzer | 87 | bikes.append(bike); |
88 | cout << ":: New Bike: " << socket->peerAddress().toString().toStdString() << endl; |
||
89 | } |
||
1 | muzer | 90 | } |
91 | |||
92 | void MainWindow::checkClients() |
||
93 | { |
||
27 | tom | 94 | bool everyoneDied = true; |
95 | |||
9 | muzer | 96 | for (int i = bikes.count() - 1; i >= 0; i--) |
97 | { |
||
98 | if (bikes[i]->isDisconnected) |
||
99 | { |
||
30 | tom | 100 | for (int j = 0; j < bikes.count(); j++) |
101 | { |
||
102 | bikes[j]->setText(QString("DISCO ") + bikes[i]->name + QString("\n")); |
||
103 | } |
||
104 | |||
9 | muzer | 105 | delete bikes[i]; |
106 | bikes.removeAt(i); |
||
107 | cout << ":: Removed Bike: " << i << endl; |
||
108 | } |
||
27 | tom | 109 | else |
110 | { |
||
111 | if (!bikes[i]->collided) |
||
112 | { |
||
113 | everyoneDied = false; |
||
114 | } |
||
29 | tom | 115 | else if (bikes[i]->dead) |
27 | tom | 116 | { |
117 | everyoneDied = false; |
||
118 | } |
||
119 | } |
||
9 | muzer | 120 | } |
27 | tom | 121 | |
122 | if (everyoneDied) |
||
123 | { |
||
124 | reset(); |
||
125 | } |
||
1 | muzer | 126 | } |
31 | tom | 127 | |
128 | void MainWindow::chat(QString name, QString message) |
||
129 | { |
||
130 | QString packet = "CHAT "; |
||
131 | packet.append(name); |
||
132 | packet.append(" "); |
||
133 | packet.append(message); |
||
134 | packet.append("\n"); |
||
135 | |||
136 | for (int i = 0; i < bikes.count(); i++) |
||
137 | { |
||
138 | bikes[i]->setText(packet); |
||
139 | } |
||
140 | } |