Subversion Repositories QTron

Rev

Rev 21 | Rev 24 | 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 muzer 19
    abpool = 0;
9 muzer 20
    name = "";
21
    show = false;
22
    isReady = false;
23
    hadGo = false;
24
    dead = false;
25
    collided = false;
1 muzer 26
 
9 muzer 27
    colour.setBlue(0);
28
    colour.setRed(0);
29
    colour.setGreen(0);
1 muzer 30
 
9 muzer 31
    if (!socket->waitForReadyRead(2000))
32
    {
33
        socket->disconnectFromHost();
34
        show = false;
35
        isReady = true;
36
        hadGo = false;
37
    }
1 muzer 38
}
39
 
14 muzer 40
void Bike::draw(QPainter *painter, QList<Bike *> bikes)
1 muzer 41
{
9 muzer 42
    if (show)
43
    {
44
        painter->setPen(colour);
45
        painter->setFont(QFont("sans", 12));
1 muzer 46
 
9 muzer 47
        for (int i = 0; i < linePoints.count(); i++)
48
        {
49
            QPoint point1;
50
            QPoint point2;
51
            if (i == 0)
52
            {
53
                point1 = linePoints[0];
54
            }
55
            else
56
            {
57
                point1 = linePoints[i - 1];
58
            }
1 muzer 59
 
9 muzer 60
            point2 = linePoints[i];
1 muzer 61
 
9 muzer 62
            painter->drawLine(point1, point2);
63
        }
1 muzer 64
 
9 muzer 65
        if (!collided)
66
        {
14 muzer 67
            collided = hasCollided(bikes);
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
 
19 muzer 165
int sign(int x){
166
    if(x > 0){
9 muzer 167
        return 1;
168
    }
19 muzer 169
    if(x < 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;
18 muzer 184
    int j, r;
14 muzer 185
    Bike *bike;
19 muzer 186
    for(r = 0; r < bikes.count(); r++)
18 muzer 187
    {
188
        bike = bikes[r];
21 muzer 189
        int forsubtract = 1;
190
        if(bike->name == name){
191
            forsubtract = 3;
192
        }
193
        for(j = 0; j < bike->linePoints.count() - forsubtract; j++)
18 muzer 194
        {
20 muzer 195
            int jx = bike->linePoints[j].x();
196
            int j1x = bike->linePoints[j+1].x();
197
            int jy = bike->linePoints[j].y();
198
            int j1y = bike->linePoints[j+1].y();
199
            int ix = linePoints[i-1].x();
200
            int i1x = linePoints[i].x();
201
            int iy = linePoints[i-1].y();
202
            int i1y = linePoints[i].y();
21 muzer 203
 
20 muzer 204
            if(angle == 0){
205
                iy += 1;
206
                i1y -= 1;
207
            }
208
            if(angle == 90){
209
                ix -= 1;
210
                i1x += 1;
211
            }
212
            if(angle == 180){
213
                iy -= 1;
214
                i1y += 1;
215
            }
216
            if(angle == 270){
217
                ix += 1;
218
                i1x -= 1;
219
            }
220
            if(!(jx == j1x && i1x == ix) && !(jy == j1y && i1y == iy))
18 muzer 221
            {
16 muzer 222
 
18 muzer 223
                // If not parallel
20 muzer 224
                if(jx == j1x)
18 muzer 225
                {
226
                    // x equal
16 muzer 227
 
20 muzer 228
                    if(ix > jx && i1x < jx || ix < jx && i1x > jx)
18 muzer 229
                    {
20 muzer 230
                        if((sign(iy - jy) != sign(iy - j1y)))
18 muzer 231
                            return true;
232
                    }
233
                }
20 muzer 234
                if(jy == j1y)
18 muzer 235
                {
16 muzer 236
 
20 muzer 237
                    if(iy > jy && i1y < jy || iy < jy && i1y > jy)
18 muzer 238
                    {
20 muzer 239
                        if((sign(ix - jx) != sign(ix - j1x)))
18 muzer 240
                            return true;
241
                    }
242
                }
243
            }
244
        }
9 muzer 245
    }
246
    return false;
8 tom 247
}
248
 
1 muzer 249
void Bike::reset()
250
{
9 muzer 251
    x = rand() % 800;
252
    y = rand() % 600;
253
    linePoints.clear();
1 muzer 254
 
9 muzer 255
    linePoints.append(QPoint(x, y));
1 muzer 256
 
9 muzer 257
    velocity = 1;
258
    angle = 0;
19 muzer 259
    abpool = 0;
9 muzer 260
    show = true;
261
    isReady = true;
262
    hadGo = false;
263
    dead = false;
264
    collided = false;
1 muzer 265
 
9 muzer 266
    socket->write("RESET\n");
1 muzer 267
}
268
 
269
void Bike::readyRead()
270
{
9 muzer 271
    while (socket->canReadLine())
272
    {
273
        QByteArray data = socket->readLine();
274
        QString line = data.trimmed();
1 muzer 275
 
9 muzer 276
        if (line == "L")
277
        {
278
            angle -= 90;
1 muzer 279
 
9 muzer 280
            if (angle >= 360)
281
            {
282
                angle -= 360;
283
            }
284
            if (angle < 0)
285
            {
286
                angle += 360;
287
            }
19 muzer 288
            if(velocity < 5)
289
                velocity += 0.5;
290
            else if(velocity > 5)
291
                velocity -= 0.5;
22 muzer 292
            if(abs(5-velocity)<0.5)
293
                velocity = 5;
19 muzer 294
            if(abpool<10)
295
                abpool += 0.2;
9 muzer 296
            hadGo = true;
297
        }
298
        else if (line == "R")
299
        {
300
            angle += 90;
1 muzer 301
 
9 muzer 302
            if (angle >= 360)
303
            {
304
                angle -= 360;
305
            }
306
            if (angle < 0)
307
            {
308
                angle += 360;
309
            }
19 muzer 310
            if(velocity < 5)
311
                velocity += 0.5;
312
            else if(velocity > 5)
313
                velocity -= 0.5;
22 muzer 314
            if(abs(5-velocity)<0.5)
315
                velocity = 5;
19 muzer 316
            if(abpool<10)
317
                abpool += 0.2;
9 muzer 318
            hadGo = true;
319
        }
320
        else if (line == "A")
321
        {
19 muzer 322
            if(abpool > 0){
323
                velocity += 0.1;
324
                abpool -= 0.5;
20 muzer 325
            } else {
326
                if(velocity < 5)
327
                    velocity += 0.5;
328
                else if(velocity > 5)
329
                    velocity -= 0.5;
22 muzer 330
                if(abs(5-velocity)<0.5)
331
                    velocity = 5;
19 muzer 332
            }
9 muzer 333
            hadGo = true;
334
        }
335
        else if (line == "D")
336
        {
19 muzer 337
            if(abpool > 0){
338
                velocity -= 0.2;
339
                abpool -= 0.5;
340
            }
20 muzer 341
            else {
342
                if(velocity < 5)
343
                    velocity += 0.5;
344
                else if(velocity > 5)
345
                    velocity -= 0.5;
22 muzer 346
                if(abs(5-velocity)<0.5)
347
                    velocity = 5;
20 muzer 348
            }
9 muzer 349
            hadGo = true;
350
        }
351
        else if (line == "N")
352
        {
19 muzer 353
            if(velocity < 5)
354
                velocity += 0.5;
355
            else if(velocity > 5)
356
                velocity -= 0.5;
22 muzer 357
            if(abs(5-velocity)<0.5)
358
                velocity = 5;
19 muzer 359
            if(abpool<10)
360
                abpool += 0.2;
9 muzer 361
            hadGo = true;
362
        }
363
        else if (line.startsWith("NAME "))
364
        {
365
            name = line.remove(0, 5);
366
            isReady = true;
367
            show = true;
368
        }
369
        else if (line.startsWith("COLOUR "))
370
        {
371
            QStringList list = line.split(" ");
372
            if (list.count() >= 2)
373
            {
374
                colour.setRed(list[1].toInt());
375
            }
376
            if (list.count() >= 3)
377
            {
378
                colour.setGreen(list[2].toInt());
379
            }
380
            if (list.count() >= 4)
381
            {
382
                colour.setBlue(list[3].toInt());
383
            }
384
        }
385
    }
1 muzer 386
}
387
 
388
void Bike::disconnected()
389
{
9 muzer 390
    dead = true;
391
    isDisconnected = true;
392
    cout << ":: Disconnected: " << socket->peerAddress().toString().toStdString() << endl;
1 muzer 393
}