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