Rev 1 | Rev 5 | 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; |
||
24 | |||
25 | colour.setBlue(0); |
||
26 | colour.setRed(0); |
||
27 | colour.setGreen(0); |
||
28 | |||
29 | if (!socket->waitForReadyRead(2000)) |
||
30 | { |
||
31 | socket->disconnectFromHost(); |
||
32 | show = false; |
||
33 | isReady = true; |
||
34 | hadGo = false; |
||
35 | } |
||
36 | } |
||
37 | |||
38 | void Bike::draw(QPainter *painter) |
||
39 | { |
||
40 | if (show) |
||
41 | { |
||
42 | painter->setPen(colour); |
||
43 | painter->setFont(QFont("sans", 12)); |
||
44 | |||
45 | for (int i = 0; i < linePoints.count(); i++) |
||
46 | { |
||
47 | QPoint point1; |
||
48 | QPoint point2; |
||
49 | if (i == 0) |
||
50 | { |
||
51 | point1 = linePoints[0]; |
||
52 | } |
||
53 | else |
||
54 | { |
||
55 | point1 = linePoints[i - 1]; |
||
56 | } |
||
57 | |||
58 | point2 = linePoints[i]; |
||
59 | |||
60 | painter->drawLine(point1, point2); |
||
61 | } |
||
62 | |||
63 | if (angle == 0) |
||
64 | { |
||
65 | y -= velocity; |
||
66 | } |
||
67 | else if (angle == 90) |
||
68 | { |
||
69 | x += velocity; |
||
70 | } |
||
71 | else if (angle == 180) |
||
72 | { |
||
73 | y += velocity; |
||
74 | } |
||
75 | else if (angle == 270) |
||
76 | { |
||
77 | x -= velocity; |
||
78 | } |
||
79 | |||
80 | if (angle == 0 || angle == 180) |
||
81 | { |
||
82 | painter->fillRect(x - 5, y - 15, 10, 30, colour); |
||
83 | } |
||
84 | else |
||
85 | { |
||
86 | painter->fillRect(x - 15, y - 5, 30, 10, colour); |
||
87 | } |
||
88 | |||
89 | painter->drawText(x, y - 20, name); |
||
90 | } |
||
91 | } |
||
92 | |||
93 | void Bike::run(QList<Bike *> bikes) |
||
94 | { |
||
95 | for (int i = 0; i < bikes.count(); i++) |
||
96 | { |
||
97 | Bike *bike = bikes[i]; |
||
98 | if (bike->isReady && bike->show) |
||
99 | { |
||
100 | if (bike->dead) |
||
101 | { |
||
102 | socket->write("DEAD "); |
||
103 | socket->write(bike->name.toAscii()); |
||
104 | socket->write("\n"); |
||
105 | } |
||
106 | else |
||
107 | { |
||
108 | socket->write("BIKE "); |
||
109 | socket->write(bike->name.toAscii()); |
||
110 | socket->write(" "); |
||
111 | socket->write(QString::number(bike->x).toAscii()); |
||
112 | socket->write(" "); |
||
113 | socket->write(QString::number(bike->y).toAscii()); |
||
114 | socket->write("\n"); |
||
115 | } |
||
116 | } |
||
117 | } |
||
118 | |||
4 | tom | 119 | linePoints.append(QPoint(x, y)); |
120 | |||
1 | muzer | 121 | if (dead) |
122 | { |
||
123 | show = false; |
||
124 | isReady = true; |
||
125 | hadGo = false; |
||
126 | dead = false; |
||
127 | } |
||
128 | |||
129 | if (!dead) |
||
130 | { |
||
131 | socket->write("G\n"); |
||
132 | socket->flush(); |
||
133 | |||
134 | hadGo = false; |
||
135 | if (!socket->waitForReadyRead(2000)) |
||
136 | { |
||
137 | socket->disconnectFromHost(); |
||
138 | dead = true; |
||
139 | } |
||
140 | if (!hadGo) |
||
141 | { |
||
142 | socket->disconnectFromHost(); |
||
143 | dead = true; |
||
144 | } |
||
145 | } |
||
4 | tom | 146 | hasHadGo = true; |
1 | muzer | 147 | } |
148 | |||
149 | void Bike::reset() |
||
150 | { |
||
151 | x = rand() % 800; |
||
152 | y = rand() % 600; |
||
153 | linePoints.clear(); |
||
154 | |||
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 | angle -= 90; |
||
177 | |||
178 | if (angle >= 360) |
||
179 | { |
||
180 | angle -= 360; |
||
181 | } |
||
182 | if (angle < 0) |
||
183 | { |
||
184 | angle += 360; |
||
185 | } |
||
186 | hadGo = true; |
||
187 | } |
||
188 | else if (line == "R") |
||
189 | { |
||
190 | angle += 90; |
||
191 | |||
192 | if (angle >= 360) |
||
193 | { |
||
194 | angle -= 360; |
||
195 | } |
||
196 | if (angle < 0) |
||
197 | { |
||
198 | angle += 360; |
||
199 | } |
||
200 | hadGo = true; |
||
201 | } |
||
202 | else if (line == "A") |
||
203 | { |
||
204 | velocity += 0.1; |
||
205 | hadGo = true; |
||
206 | } |
||
207 | else if (line == "D") |
||
208 | { |
||
209 | velocity -= 0.2; |
||
210 | hadGo = true; |
||
211 | } |
||
212 | else if (line == "N") |
||
213 | { |
||
214 | hadGo = true; |
||
215 | } |
||
216 | else if (line.startsWith("NAME ")) |
||
217 | { |
||
218 | name = line.remove(0, 5); |
||
219 | isReady = true; |
||
220 | show = true; |
||
221 | } |
||
222 | else if (line.startsWith("COLOUR ")) |
||
223 | { |
||
224 | QStringList list = line.split(" "); |
||
225 | if (list.count() >= 2) |
||
226 | { |
||
227 | colour.setRed(list[1].toInt()); |
||
228 | } |
||
229 | else if (list.count() >= 3) |
||
230 | { |
||
231 | colour.setGreen(list[2].toInt()); |
||
232 | } |
||
233 | else if (list.count() >= 4) |
||
234 | { |
||
235 | colour.setBlue(list[3].toInt()); |
||
236 | } |
||
237 | } |
||
238 | } |
||
239 | } |
||
240 | |||
241 | void Bike::disconnected() |
||
242 | { |
||
243 | dead = true; |
||
244 | isDisconnected = true; |
||
245 | cout << ":: Disconnected: " << socket->peerAddress().toString().toStdString() << endl; |
||
246 | } |