Rev 14 | Rev 17 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1 | muzer | 1 | #include "bike.h" |
2 | |||
3 | Bike::Bike(QTcpSocket *sock, int i) |
||
4 | { |
||
9 | muzer | 5 | socket = sock; |
6 | id = i; |
||
1 | muzer | 7 | |
9 | muzer | 8 | connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead())); |
9 | connect(socket, SIGNAL(disconnected()), this, SLOT(disconnected())); |
||
1 | muzer | 10 | |
9 | muzer | 11 | isDisconnected = false; |
1 | muzer | 12 | |
9 | muzer | 13 | x = rand() % 800; |
14 | y = rand() % 600; |
||
15 | linePoints.append(QPoint(x, y)); |
||
1 | muzer | 16 | |
9 | muzer | 17 | velocity = 1; |
18 | angle = 0; |
||
19 | name = ""; |
||
20 | show = false; |
||
21 | isReady = false; |
||
22 | hadGo = false; |
||
23 | dead = false; |
||
24 | collided = false; |
||
1 | muzer | 25 | |
9 | muzer | 26 | colour.setBlue(0); |
27 | colour.setRed(0); |
||
28 | colour.setGreen(0); |
||
1 | muzer | 29 | |
9 | muzer | 30 | if (!socket->waitForReadyRead(2000)) |
31 | { |
||
32 | socket->disconnectFromHost(); |
||
33 | show = false; |
||
34 | isReady = true; |
||
35 | hadGo = false; |
||
36 | } |
||
1 | muzer | 37 | } |
38 | |||
14 | muzer | 39 | void Bike::draw(QPainter *painter, QList<Bike *> bikes) |
1 | muzer | 40 | { |
9 | muzer | 41 | if (show) |
42 | { |
||
43 | painter->setPen(colour); |
||
44 | painter->setFont(QFont("sans", 12)); |
||
1 | muzer | 45 | |
9 | muzer | 46 | for (int i = 0; i < linePoints.count(); i++) |
47 | { |
||
48 | QPoint point1; |
||
49 | QPoint point2; |
||
50 | if (i == 0) |
||
51 | { |
||
52 | point1 = linePoints[0]; |
||
53 | } |
||
54 | else |
||
55 | { |
||
56 | point1 = linePoints[i - 1]; |
||
57 | } |
||
1 | muzer | 58 | |
9 | muzer | 59 | point2 = linePoints[i]; |
1 | muzer | 60 | |
9 | muzer | 61 | painter->drawLine(point1, point2); |
62 | } |
||
1 | muzer | 63 | |
9 | muzer | 64 | if (!collided) |
65 | { |
||
14 | muzer | 66 | collided = hasCollided(bikes); |
8 | tom | 67 | |
9 | muzer | 68 | if (angle == 0) |
69 | { |
||
70 | y -= velocity; |
||
71 | } |
||
72 | else if (angle == 90) |
||
73 | { |
||
74 | x += velocity; |
||
75 | } |
||
76 | else if (angle == 180) |
||
77 | { |
||
78 | y += velocity; |
||
79 | } |
||
80 | else if (angle == 270) |
||
81 | { |
||
82 | x -= velocity; |
||
83 | } |
||
84 | } |
||
85 | else |
||
86 | { |
||
87 | linePoints.clear(); |
||
88 | } |
||
1 | muzer | 89 | |
9 | muzer | 90 | if (angle == 0 || angle == 180) |
91 | { |
||
92 | painter->fillRect(x - 5, y - 15, 10, 30, colour); |
||
93 | } |
||
94 | else |
||
95 | { |
||
96 | painter->fillRect(x - 15, y - 5, 30, 10, colour); |
||
97 | } |
||
1 | muzer | 98 | |
9 | muzer | 99 | painter->drawText(x, y - 20, name); |
100 | } |
||
1 | muzer | 101 | } |
102 | |||
103 | void Bike::run(QList<Bike *> bikes) |
||
104 | { |
||
9 | muzer | 105 | for (int i = 0; i < bikes.count(); i++) |
106 | { |
||
107 | Bike *bike = bikes[i]; |
||
108 | if (bike->isReady && bike->show) |
||
109 | { |
||
14 | muzer | 110 | if (bike->dead || bike->collided) |
9 | muzer | 111 | { |
112 | socket->write("DEAD "); |
||
113 | socket->write(bike->name.toAscii()); |
||
114 | socket->write("\n"); |
||
115 | } |
||
116 | else |
||
117 | { |
||
118 | socket->write("BIKE "); |
||
119 | socket->write(bike->name.toAscii()); |
||
120 | socket->write(" "); |
||
121 | socket->write(QString::number(bike->x).toAscii()); |
||
122 | socket->write(" "); |
||
123 | socket->write(QString::number(bike->y).toAscii()); |
||
124 | socket->write(" "); |
||
125 | socket->write(QString::number(bike->colour.red()).toAscii()); |
||
126 | socket->write(" "); |
||
127 | socket->write(QString::number(bike->colour.green()).toAscii()); |
||
128 | socket->write(" "); |
||
129 | socket->write(QString::number(bike->colour.blue()).toAscii()); |
||
130 | socket->write("\n"); |
||
131 | } |
||
132 | } |
||
133 | } |
||
1 | muzer | 134 | |
9 | muzer | 135 | linePoints.append(QPoint(x, y)); |
4 | tom | 136 | |
9 | muzer | 137 | if (dead) |
138 | { |
||
139 | show = false; |
||
140 | isReady = true; |
||
141 | hadGo = false; |
||
142 | dead = false; |
||
143 | } |
||
1 | muzer | 144 | |
9 | muzer | 145 | if (!dead) |
146 | { |
||
147 | socket->write("G\n"); |
||
148 | socket->flush(); |
||
1 | muzer | 149 | |
9 | muzer | 150 | hadGo = false; |
151 | if (!socket->waitForReadyRead(2000)) |
||
152 | { |
||
153 | socket->disconnectFromHost(); |
||
154 | dead = true; |
||
155 | } |
||
156 | if (!hadGo) |
||
157 | { |
||
158 | socket->disconnectFromHost(); |
||
159 | dead = true; |
||
160 | } |
||
161 | } |
||
162 | hasHadGo = true; |
||
1 | muzer | 163 | } |
164 | |||
9 | muzer | 165 | int sign(float x){ |
166 | if(x > 0){ |
||
167 | return 1; |
||
168 | } |
||
169 | if(x < 0){ |
||
170 | return -1; |
||
171 | } |
||
172 | return 0; |
||
173 | } |
||
174 | |||
14 | muzer | 175 | bool Bike::hasCollided(QList<Bike *> bikes) |
8 | tom | 176 | { |
9 | muzer | 177 | // Do collision detection here |
178 | // use linePoints |
||
179 | int i = linePoints.count() - 1; |
||
180 | if(linePoints[i-1].x() < 0 || linePoints[i-1].x() > 800 || linePoints[i-1].y() < 0 || linePoints[i-1].y() > 600) |
||
181 | return true; |
||
182 | if(linePoints[i].x() < 0 || linePoints[i].x() > 800 || linePoints[i].y() < 0 || linePoints[i].y() > 600) |
||
183 | return true; |
||
14 | muzer | 184 | int j; |
185 | Bike *bike; |
||
186 | foreach(bike,bikes) |
||
9 | muzer | 187 | { |
14 | muzer | 188 | for(j = 0; j < bike->linePoints.count() - 2; j++) |
9 | muzer | 189 | { |
14 | muzer | 190 | if(!(bike->linePoints[j].x() == bike->linePoints[j+1].x() && linePoints[i].x() == linePoints[i-1].x()) && !(bike->linePoints[j].y() == bike->linePoints[j+1].y() && linePoints[i].y() == linePoints[i-1].y())) |
9 | muzer | 191 | { |
16 | muzer | 192 | |
14 | muzer | 193 | // If not parallel |
194 | if(bike->linePoints[j].x() == bike->linePoints[j+1].x()) |
||
195 | { |
||
196 | // x equal |
||
16 | muzer | 197 | |
198 | if(linePoints[i-1].x() > bike->linePoints[j].x() && linePoints[i].x() < bike->linePoints[j].x() || linePoints[i-1].x() < bike->linePoints[j].x() && linePoints[i].x() > bike->linePoints[j].x()) |
||
199 | { |
||
14 | muzer | 200 | if((sign(linePoints[i-1].y() - bike->linePoints[j].y()) != sign(linePoints[i-1].y() - bike->linePoints[j+1].y()))) |
201 | return true; |
||
202 | } |
||
9 | muzer | 203 | } |
14 | muzer | 204 | else if(bike->linePoints[j].y() == bike->linePoints[j+1].y()) |
9 | muzer | 205 | { |
16 | muzer | 206 | |
14 | muzer | 207 | if(linePoints[i-1].y() > bike->linePoints[j].y() && linePoints[i].y() < bike->linePoints[j].y() || linePoints[i-1].y() < bike->linePoints[j].y() && linePoints[i].y() > bike->linePoints[j].y()) |
208 | { |
||
209 | if((sign(linePoints[i-1].x() - bike->linePoints[j].x()) != sign(linePoints[i-1].x() - bike->linePoints[j+1].x()))) |
||
210 | return true; |
||
211 | } |
||
9 | muzer | 212 | } |
213 | } |
||
214 | } |
||
215 | } |
||
216 | return false; |
||
8 | tom | 217 | } |
218 | |||
1 | muzer | 219 | void Bike::reset() |
220 | { |
||
9 | muzer | 221 | x = rand() % 800; |
222 | y = rand() % 600; |
||
223 | linePoints.clear(); |
||
1 | muzer | 224 | |
9 | muzer | 225 | linePoints.append(QPoint(x, y)); |
1 | muzer | 226 | |
9 | muzer | 227 | velocity = 1; |
228 | angle = 0; |
||
229 | show = true; |
||
230 | isReady = true; |
||
231 | hadGo = false; |
||
232 | dead = false; |
||
233 | collided = false; |
||
1 | muzer | 234 | |
9 | muzer | 235 | socket->write("RESET\n"); |
1 | muzer | 236 | } |
237 | |||
238 | void Bike::readyRead() |
||
239 | { |
||
9 | muzer | 240 | while (socket->canReadLine()) |
241 | { |
||
242 | QByteArray data = socket->readLine(); |
||
243 | QString line = data.trimmed(); |
||
1 | muzer | 244 | |
9 | muzer | 245 | if (line == "L") |
246 | { |
||
247 | angle -= 90; |
||
1 | muzer | 248 | |
9 | muzer | 249 | if (angle >= 360) |
250 | { |
||
251 | angle -= 360; |
||
252 | } |
||
253 | if (angle < 0) |
||
254 | { |
||
255 | angle += 360; |
||
256 | } |
||
257 | hadGo = true; |
||
258 | } |
||
259 | else if (line == "R") |
||
260 | { |
||
261 | angle += 90; |
||
1 | muzer | 262 | |
9 | muzer | 263 | if (angle >= 360) |
264 | { |
||
265 | angle -= 360; |
||
266 | } |
||
267 | if (angle < 0) |
||
268 | { |
||
269 | angle += 360; |
||
270 | } |
||
271 | hadGo = true; |
||
272 | } |
||
273 | else if (line == "A") |
||
274 | { |
||
275 | velocity += 0.1; |
||
276 | hadGo = true; |
||
277 | } |
||
278 | else if (line == "D") |
||
279 | { |
||
280 | velocity -= 0.2; |
||
281 | hadGo = true; |
||
282 | } |
||
283 | else if (line == "N") |
||
284 | { |
||
285 | hadGo = true; |
||
286 | } |
||
287 | else if (line.startsWith("NAME ")) |
||
288 | { |
||
289 | name = line.remove(0, 5); |
||
290 | isReady = true; |
||
291 | show = true; |
||
292 | } |
||
293 | else if (line.startsWith("COLOUR ")) |
||
294 | { |
||
295 | QStringList list = line.split(" "); |
||
296 | if (list.count() >= 2) |
||
297 | { |
||
298 | colour.setRed(list[1].toInt()); |
||
299 | } |
||
300 | if (list.count() >= 3) |
||
301 | { |
||
302 | colour.setGreen(list[2].toInt()); |
||
303 | } |
||
304 | if (list.count() >= 4) |
||
305 | { |
||
306 | colour.setBlue(list[3].toInt()); |
||
307 | } |
||
308 | } |
||
309 | } |
||
1 | muzer | 310 | } |
311 | |||
312 | void Bike::disconnected() |
||
313 | { |
||
9 | muzer | 314 | dead = true; |
315 | isDisconnected = true; |
||
316 | cout << ":: Disconnected: " << socket->peerAddress().toString().toStdString() << endl; |
||
1 | muzer | 317 | } |