Rev 12 | Rev 16 | 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 | { |
14 | muzer | 192 | // If not parallel |
193 | if(bike->linePoints[j].x() == bike->linePoints[j+1].x()) |
||
194 | { |
||
195 | // x equal |
||
196 | 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()){ |
||
197 | if((sign(linePoints[i-1].y() - bike->linePoints[j].y()) != sign(linePoints[i-1].y() - bike->linePoints[j+1].y()))) |
||
198 | return true; |
||
199 | } |
||
9 | muzer | 200 | } |
14 | muzer | 201 | else if(bike->linePoints[j].y() == bike->linePoints[j+1].y()) |
9 | muzer | 202 | { |
14 | muzer | 203 | 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()) |
204 | { |
||
205 | if((sign(linePoints[i-1].x() - bike->linePoints[j].x()) != sign(linePoints[i-1].x() - bike->linePoints[j+1].x()))) |
||
206 | return true; |
||
207 | } |
||
9 | muzer | 208 | } |
209 | } |
||
210 | } |
||
211 | } |
||
212 | return false; |
||
8 | tom | 213 | } |
214 | |||
1 | muzer | 215 | void Bike::reset() |
216 | { |
||
9 | muzer | 217 | x = rand() % 800; |
218 | y = rand() % 600; |
||
219 | linePoints.clear(); |
||
1 | muzer | 220 | |
9 | muzer | 221 | linePoints.append(QPoint(x, y)); |
1 | muzer | 222 | |
9 | muzer | 223 | velocity = 1; |
224 | angle = 0; |
||
225 | show = true; |
||
226 | isReady = true; |
||
227 | hadGo = false; |
||
228 | dead = false; |
||
229 | collided = false; |
||
1 | muzer | 230 | |
9 | muzer | 231 | socket->write("RESET\n"); |
1 | muzer | 232 | } |
233 | |||
234 | void Bike::readyRead() |
||
235 | { |
||
9 | muzer | 236 | while (socket->canReadLine()) |
237 | { |
||
238 | QByteArray data = socket->readLine(); |
||
239 | QString line = data.trimmed(); |
||
1 | muzer | 240 | |
9 | muzer | 241 | if (line == "L") |
242 | { |
||
243 | angle -= 90; |
||
1 | muzer | 244 | |
9 | muzer | 245 | if (angle >= 360) |
246 | { |
||
247 | angle -= 360; |
||
248 | } |
||
249 | if (angle < 0) |
||
250 | { |
||
251 | angle += 360; |
||
252 | } |
||
253 | hadGo = true; |
||
254 | } |
||
255 | else if (line == "R") |
||
256 | { |
||
257 | angle += 90; |
||
1 | muzer | 258 | |
9 | muzer | 259 | if (angle >= 360) |
260 | { |
||
261 | angle -= 360; |
||
262 | } |
||
263 | if (angle < 0) |
||
264 | { |
||
265 | angle += 360; |
||
266 | } |
||
267 | hadGo = true; |
||
268 | } |
||
269 | else if (line == "A") |
||
270 | { |
||
271 | velocity += 0.1; |
||
272 | hadGo = true; |
||
273 | } |
||
274 | else if (line == "D") |
||
275 | { |
||
276 | velocity -= 0.2; |
||
277 | hadGo = true; |
||
278 | } |
||
279 | else if (line == "N") |
||
280 | { |
||
281 | hadGo = true; |
||
282 | } |
||
283 | else if (line.startsWith("NAME ")) |
||
284 | { |
||
285 | name = line.remove(0, 5); |
||
286 | isReady = true; |
||
287 | show = true; |
||
288 | } |
||
289 | else if (line.startsWith("COLOUR ")) |
||
290 | { |
||
291 | QStringList list = line.split(" "); |
||
292 | if (list.count() >= 2) |
||
293 | { |
||
294 | colour.setRed(list[1].toInt()); |
||
295 | } |
||
296 | if (list.count() >= 3) |
||
297 | { |
||
298 | colour.setGreen(list[2].toInt()); |
||
299 | } |
||
300 | if (list.count() >= 4) |
||
301 | { |
||
302 | colour.setBlue(list[3].toInt()); |
||
303 | } |
||
304 | } |
||
305 | } |
||
1 | muzer | 306 | } |
307 | |||
308 | void Bike::disconnected() |
||
309 | { |
||
9 | muzer | 310 | dead = true; |
311 | isDisconnected = true; |
||
312 | cout << ":: Disconnected: " << socket->peerAddress().toString().toStdString() << endl; |
||
1 | muzer | 313 | } |