Subversion Repositories QTron

Rev

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