Rev 7 | Rev 9 | 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 | { |
||
5 | socket = sock; |
||
6 | id = i; |
||
7 | |||
8 | connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead())); |
||
9 | connect(socket, SIGNAL(disconnected()), this, SLOT(disconnected())); |
||
10 | |||
11 | isDisconnected = false; |
||
12 | |||
13 | x = rand() % 800; |
||
14 | y = rand() % 600; |
||
15 | linePoints.append(QPoint(x, y)); |
||
16 | |||
17 | velocity = 1; |
||
18 | angle = 0; |
||
19 | name = ""; |
||
20 | show = false; |
||
21 | isReady = false; |
||
22 | hadGo = false; |
||
23 | dead = false; |
||
8 | tom | 24 | collided = false; |
1 | muzer | 25 | |
26 | colour.setBlue(0); |
||
27 | colour.setRed(0); |
||
28 | colour.setGreen(0); |
||
29 | |||
30 | if (!socket->waitForReadyRead(2000)) |
||
31 | { |
||
32 | socket->disconnectFromHost(); |
||
33 | show = false; |
||
34 | isReady = true; |
||
35 | hadGo = false; |
||
36 | } |
||
37 | } |
||
38 | |||
39 | void Bike::draw(QPainter *painter) |
||
40 | { |
||
41 | if (show) |
||
42 | { |
||
43 | painter->setPen(colour); |
||
44 | painter->setFont(QFont("sans", 12)); |
||
45 | |||
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 | } |
||
58 | |||
59 | point2 = linePoints[i]; |
||
60 | |||
61 | painter->drawLine(point1, point2); |
||
62 | } |
||
63 | |||
8 | tom | 64 | if (!collided) |
1 | muzer | 65 | { |
8 | tom | 66 | collided = hasCollided(); |
67 | |||
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 | } |
||
1 | muzer | 84 | } |
8 | tom | 85 | else |
1 | muzer | 86 | { |
8 | tom | 87 | linePoints.clear(); |
1 | muzer | 88 | } |
89 | |||
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 | } |
||
98 | |||
99 | painter->drawText(x, y - 20, name); |
||
100 | } |
||
101 | } |
||
102 | |||
103 | void Bike::run(QList<Bike *> bikes) |
||
104 | { |
||
105 | for (int i = 0; i < bikes.count(); i++) |
||
106 | { |
||
107 | Bike *bike = bikes[i]; |
||
108 | if (bike->isReady && bike->show) |
||
109 | { |
||
110 | if (bike->dead) |
||
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()); |
||
7 | tom | 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()); |
||
1 | muzer | 130 | socket->write("\n"); |
131 | } |
||
132 | } |
||
133 | } |
||
134 | |||
4 | tom | 135 | linePoints.append(QPoint(x, y)); |
136 | |||
1 | muzer | 137 | if (dead) |
138 | { |
||
139 | show = false; |
||
140 | isReady = true; |
||
141 | hadGo = false; |
||
142 | dead = false; |
||
143 | } |
||
144 | |||
145 | if (!dead) |
||
146 | { |
||
147 | socket->write("G\n"); |
||
148 | socket->flush(); |
||
149 | |||
150 | hadGo = false; |
||
7 | tom | 151 | if (!socket->waitForReadyRead(2000)) |
1 | muzer | 152 | { |
153 | socket->disconnectFromHost(); |
||
154 | dead = true; |
||
155 | } |
||
156 | if (!hadGo) |
||
157 | { |
||
158 | socket->disconnectFromHost(); |
||
159 | dead = true; |
||
160 | } |
||
161 | } |
||
4 | tom | 162 | hasHadGo = true; |
1 | muzer | 163 | } |
164 | |||
8 | tom | 165 | bool Bike::hasCollided() |
166 | { |
||
167 | // Do collision detection here |
||
168 | // use linePoints |
||
169 | |||
170 | return false; |
||
171 | } |
||
172 | |||
1 | muzer | 173 | void Bike::reset() |
174 | { |
||
175 | x = rand() % 800; |
||
176 | y = rand() % 600; |
||
177 | linePoints.clear(); |
||
178 | |||
179 | linePoints.append(QPoint(x, y)); |
||
180 | |||
181 | velocity = 1; |
||
182 | angle = 0; |
||
183 | show = true; |
||
184 | isReady = true; |
||
185 | hadGo = false; |
||
186 | dead = false; |
||
8 | tom | 187 | collided = false; |
1 | muzer | 188 | |
189 | socket->write("RESET\n"); |
||
190 | } |
||
191 | |||
192 | void Bike::readyRead() |
||
193 | { |
||
194 | while (socket->canReadLine()) |
||
195 | { |
||
196 | QByteArray data = socket->readLine(); |
||
197 | QString line = data.trimmed(); |
||
198 | |||
199 | if (line == "L") |
||
200 | { |
||
201 | angle -= 90; |
||
202 | |||
203 | if (angle >= 360) |
||
204 | { |
||
205 | angle -= 360; |
||
206 | } |
||
207 | if (angle < 0) |
||
208 | { |
||
209 | angle += 360; |
||
210 | } |
||
211 | hadGo = true; |
||
212 | } |
||
213 | else if (line == "R") |
||
214 | { |
||
215 | angle += 90; |
||
216 | |||
217 | if (angle >= 360) |
||
218 | { |
||
219 | angle -= 360; |
||
220 | } |
||
221 | if (angle < 0) |
||
222 | { |
||
223 | angle += 360; |
||
224 | } |
||
225 | hadGo = true; |
||
226 | } |
||
227 | else if (line == "A") |
||
228 | { |
||
229 | velocity += 0.1; |
||
230 | hadGo = true; |
||
231 | } |
||
232 | else if (line == "D") |
||
233 | { |
||
234 | velocity -= 0.2; |
||
235 | hadGo = true; |
||
236 | } |
||
237 | else if (line == "N") |
||
238 | { |
||
239 | hadGo = true; |
||
240 | } |
||
241 | else if (line.startsWith("NAME ")) |
||
242 | { |
||
243 | name = line.remove(0, 5); |
||
244 | isReady = true; |
||
245 | show = true; |
||
246 | } |
||
247 | else if (line.startsWith("COLOUR ")) |
||
248 | { |
||
249 | QStringList list = line.split(" "); |
||
250 | if (list.count() >= 2) |
||
251 | { |
||
252 | colour.setRed(list[1].toInt()); |
||
253 | } |
||
7 | tom | 254 | if (list.count() >= 3) |
1 | muzer | 255 | { |
256 | colour.setGreen(list[2].toInt()); |
||
257 | } |
||
7 | tom | 258 | if (list.count() >= 4) |
1 | muzer | 259 | { |
260 | colour.setBlue(list[3].toInt()); |
||
261 | } |
||
262 | } |
||
263 | } |
||
264 | } |
||
265 | |||
266 | void Bike::disconnected() |
||
267 | { |
||
268 | dead = true; |
||
269 | isDisconnected = true; |
||
270 | cout << ":: Disconnected: " << socket->peerAddress().toString().toStdString() << endl; |
||
271 | } |