Subversion Repositories QTron

Rev

Rev 4 | Rev 7 | 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());
5 muzer 114
                                socket->write(" ");
115
                                socket->write(QString::number(bike->colour.red()).toAscii());
116
                                socket->write(" ");
117
                                socket->write(QString::number(bike->colour.green()).toAscii());
118
                                socket->write(" ");
119
                                socket->write(QString::number(bike->colour.blue()).toAscii());
1 muzer 120
				socket->write("\n");
121
			}
122
		}
123
	}
124
 
4 tom 125
	linePoints.append(QPoint(x, y));
126
 
1 muzer 127
	if (dead)
128
	{
129
		show = false;
130
		isReady = true;
131
		hadGo = false;
132
		dead = false;
133
	}
134
 
135
	if (!dead)
136
	{
137
		socket->write("G\n");
138
		socket->flush();
139
 
140
		hadGo = false;
141
                if (!socket->waitForReadyRead(2000))
142
		{
143
			socket->disconnectFromHost();
144
			dead = true;
145
		}
146
		if (!hadGo)
147
		{
148
			socket->disconnectFromHost();
149
			dead = true;
150
		}
151
	}
4 tom 152
	hasHadGo = true;
1 muzer 153
}
154
 
155
void Bike::reset()
156
{
157
	x = rand() % 800;
158
	y = rand() % 600;
159
	linePoints.clear();
160
 
161
	linePoints.append(QPoint(x, y));
162
 
163
	velocity = 1;
164
	angle = 0;
165
	show = true;
166
	isReady = true;
167
	hadGo = false;
168
	dead = false;
169
 
170
	socket->write("RESET\n");
171
}
172
 
173
void Bike::readyRead()
174
{
175
	while (socket->canReadLine())
176
	{
177
		QByteArray data = socket->readLine();
178
		QString line = data.trimmed();
179
 
180
		if (line == "L")
181
		{
182
			angle -= 90;
183
 
184
			if (angle >= 360)
185
			{
186
				angle -= 360;
187
			}
188
			if (angle < 0)
189
			{
190
				angle += 360;
191
			}
192
			hadGo = true;
193
		}
194
		else if (line == "R")
195
		{
196
			angle += 90;
197
 
198
			if (angle >= 360)
199
			{
200
				angle -= 360;
201
			}
202
			if (angle < 0)
203
			{
204
				angle += 360;
205
			}
206
			hadGo = true;
207
		}
208
		else if (line == "A")
209
		{
210
			velocity += 0.1;
211
			hadGo = true;
212
		}
213
		else if (line == "D")
214
		{
215
			velocity -= 0.2;
216
			hadGo = true;
217
		}
218
		else if (line == "N")
219
		{
220
			hadGo = true;
221
		}
222
		else if (line.startsWith("NAME "))
223
		{
224
			name = line.remove(0, 5);
225
			isReady = true;
226
			show = true;
227
		}
228
		else if (line.startsWith("COLOUR "))
229
		{
230
			QStringList list = line.split(" ");
231
			if (list.count() >= 2)
232
			{
233
				colour.setRed(list[1].toInt());
234
			}
235
			else if (list.count() >= 3)
236
			{
237
				colour.setGreen(list[2].toInt());
238
			}
239
			else if (list.count() >= 4)
240
			{
241
				colour.setBlue(list[3].toInt());
242
			}
243
		}
244
	}
245
}
246
 
247
void Bike::disconnected()
248
{
249
	dead = true;
250
	isDisconnected = true;
251
	cout << ":: Disconnected: " << socket->peerAddress().toString().toStdString() << endl;
252
}