Rev 4 | Go to most recent revision | Details | 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 | linePoints.append(QPoint(x, y)); |
||
17 | |||
18 | velocity = 1; |
||
19 | angle = 0; |
||
20 | name = ""; |
||
21 | show = false; |
||
22 | isReady = false; |
||
23 | hadGo = false; |
||
24 | dead = false; |
||
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 | |||
64 | if (angle == 0) |
||
65 | { |
||
66 | y -= velocity; |
||
67 | } |
||
68 | else if (angle == 90) |
||
69 | { |
||
70 | x += velocity; |
||
71 | } |
||
72 | else if (angle == 180) |
||
73 | { |
||
74 | y += velocity; |
||
75 | } |
||
76 | else if (angle == 270) |
||
77 | { |
||
78 | x -= velocity; |
||
79 | } |
||
80 | |||
81 | if (angle == 0 || angle == 180) |
||
82 | { |
||
83 | painter->fillRect(x - 5, y - 15, 10, 30, colour); |
||
84 | } |
||
85 | else |
||
86 | { |
||
87 | painter->fillRect(x - 15, y - 5, 30, 10, colour); |
||
88 | } |
||
89 | |||
90 | painter->drawText(x, y - 20, name); |
||
91 | } |
||
92 | } |
||
93 | |||
94 | void Bike::run(QList<Bike *> bikes) |
||
95 | { |
||
96 | for (int i = 0; i < bikes.count(); i++) |
||
97 | { |
||
98 | Bike *bike = bikes[i]; |
||
99 | if (bike->isReady && bike->show) |
||
100 | { |
||
101 | if (bike->dead) |
||
102 | { |
||
103 | socket->write("DEAD "); |
||
104 | socket->write(bike->name.toAscii()); |
||
105 | socket->write("\n"); |
||
106 | } |
||
107 | else |
||
108 | { |
||
109 | socket->write("BIKE "); |
||
110 | socket->write(bike->name.toAscii()); |
||
111 | socket->write(" "); |
||
112 | socket->write(QString::number(bike->x).toAscii()); |
||
113 | socket->write(" "); |
||
114 | socket->write(QString::number(bike->y).toAscii()); |
||
115 | socket->write("\n"); |
||
116 | } |
||
117 | } |
||
118 | } |
||
119 | |||
120 | if (dead) |
||
121 | { |
||
122 | show = false; |
||
123 | isReady = true; |
||
124 | hadGo = false; |
||
125 | dead = false; |
||
126 | } |
||
127 | |||
128 | if (!dead) |
||
129 | { |
||
130 | socket->write("G\n"); |
||
131 | socket->flush(); |
||
132 | |||
133 | hadGo = false; |
||
134 | if (!socket->waitForReadyRead(2000)) |
||
135 | { |
||
136 | socket->disconnectFromHost(); |
||
137 | dead = true; |
||
138 | } |
||
139 | if (!hadGo) |
||
140 | { |
||
141 | socket->disconnectFromHost(); |
||
142 | dead = true; |
||
143 | } |
||
144 | } |
||
145 | hasHadGo = true; |
||
146 | } |
||
147 | |||
148 | void Bike::reset() |
||
149 | { |
||
150 | x = rand() % 800; |
||
151 | y = rand() % 600; |
||
152 | linePoints.clear(); |
||
153 | |||
154 | linePoints.append(QPoint(x, y)); |
||
155 | linePoints.append(QPoint(x, y)); |
||
156 | |||
157 | velocity = 1; |
||
158 | angle = 0; |
||
159 | show = true; |
||
160 | isReady = true; |
||
161 | hadGo = false; |
||
162 | dead = false; |
||
163 | |||
164 | socket->write("RESET\n"); |
||
165 | } |
||
166 | |||
167 | void Bike::readyRead() |
||
168 | { |
||
169 | while (socket->canReadLine()) |
||
170 | { |
||
171 | QByteArray data = socket->readLine(); |
||
172 | QString line = data.trimmed(); |
||
173 | |||
174 | if (line == "L") |
||
175 | { |
||
176 | linePoints.append(QPoint(x, y)); |
||
177 | angle -= 90; |
||
178 | |||
179 | if (angle >= 360) |
||
180 | { |
||
181 | angle -= 360; |
||
182 | } |
||
183 | if (angle < 0) |
||
184 | { |
||
185 | angle += 360; |
||
186 | } |
||
187 | hadGo = true; |
||
188 | } |
||
189 | else if (line == "R") |
||
190 | { |
||
191 | linePoints.append(QPoint(x, y)); |
||
192 | angle += 90; |
||
193 | |||
194 | if (angle >= 360) |
||
195 | { |
||
196 | angle -= 360; |
||
197 | } |
||
198 | if (angle < 0) |
||
199 | { |
||
200 | angle += 360; |
||
201 | } |
||
202 | hadGo = true; |
||
203 | } |
||
204 | else if (line == "A") |
||
205 | { |
||
206 | linePoints[linePoints.size() - 1] = QPoint(x, y); |
||
207 | velocity += 0.1; |
||
208 | hadGo = true; |
||
209 | } |
||
210 | else if (line == "D") |
||
211 | { |
||
212 | linePoints[linePoints.size() - 1] = QPoint(x, y); |
||
213 | velocity -= 0.2; |
||
214 | hadGo = true; |
||
215 | } |
||
216 | else if (line == "N") |
||
217 | { |
||
218 | linePoints[linePoints.size() - 1] = QPoint(x, y); |
||
219 | hadGo = true; |
||
220 | } |
||
221 | else if (line.startsWith("NAME ")) |
||
222 | { |
||
223 | name = line.remove(0, 5); |
||
224 | isReady = true; |
||
225 | show = true; |
||
226 | } |
||
227 | else if (line.startsWith("COLOUR ")) |
||
228 | { |
||
229 | QStringList list = line.split(" "); |
||
230 | if (list.count() >= 2) |
||
231 | { |
||
232 | colour.setRed(list[1].toInt()); |
||
233 | } |
||
234 | else if (list.count() >= 3) |
||
235 | { |
||
236 | colour.setGreen(list[2].toInt()); |
||
237 | } |
||
238 | else if (list.count() >= 4) |
||
239 | { |
||
240 | colour.setBlue(list[3].toInt()); |
||
241 | } |
||
242 | } |
||
243 | } |
||
244 | } |
||
245 | |||
246 | void Bike::disconnected() |
||
247 | { |
||
248 | dead = true; |
||
249 | isDisconnected = true; |
||
250 | cout << ":: Disconnected: " << socket->peerAddress().toString().toStdString() << endl; |
||
251 | } |