Subversion Repositories QTron

Rev

Rev 30 | Rev 32 | 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;
26 muzer 18
    angle = (rand() % 4) * 90;
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
            }
28 muzer 84
            if (angle == 0 || angle == 180)
85
            {
86
                painter->fillRect(x - 5, y - 15, 10, 30, colour);
87
            }
88
            else
89
            {
90
                painter->fillRect(x - 15, y - 5, 30, 10, colour);
91
            }
92
            painter->drawText(x, y - 20, name);
9 muzer 93
        }
94
        else
95
        {
96
            linePoints.clear();
97
        }
1 muzer 98
 
99
 
28 muzer 100
 
101
 
9 muzer 102
    }
1 muzer 103
}
104
 
105
void Bike::run(QList<Bike *> bikes)
106
{
9 muzer 107
    for (int i = 0; i < bikes.count(); i++)
108
    {
109
        Bike *bike = bikes[i];
110
        if (bike->isReady && bike->show)
111
        {
25 muzer 112
            if (bike->collided)
9 muzer 113
            {
114
                socket->write("DEAD ");
115
                socket->write(bike->name.toAscii());
116
                socket->write("\n");
117
            }
118
            else
119
            {
120
                socket->write("BIKE ");
121
                socket->write(bike->name.toAscii());
122
                socket->write(" ");
123
                socket->write(QString::number(bike->x).toAscii());
124
                socket->write(" ");
125
                socket->write(QString::number(bike->y).toAscii());
126
                socket->write(" ");
127
                socket->write(QString::number(bike->colour.red()).toAscii());
128
                socket->write(" ");
129
                socket->write(QString::number(bike->colour.green()).toAscii());
130
                socket->write(" ");
131
                socket->write(QString::number(bike->colour.blue()).toAscii());
132
                socket->write("\n");
133
            }
134
        }
135
    }
1 muzer 136
 
9 muzer 137
    linePoints.append(QPoint(x, y));
4 tom 138
 
9 muzer 139
    if (dead)
140
    {
141
        show = false;
142
        isReady = true;
143
        hadGo = false;
144
        dead = false;
145
    }
1 muzer 146
 
9 muzer 147
    if (!dead)
148
    {
149
        socket->write("G\n");
150
        socket->flush();
1 muzer 151
 
9 muzer 152
        hadGo = false;
29 tom 153
                if (!socket->waitForReadyRead(10000))
9 muzer 154
        {
155
            socket->disconnectFromHost();
156
            dead = true;
157
        }
158
        if (!hadGo)
159
        {
160
            socket->disconnectFromHost();
161
            dead = true;
162
        }
163
    }
164
    hasHadGo = true;
1 muzer 165
}
166
 
19 muzer 167
int sign(int x){
168
    if(x > 0){
9 muzer 169
        return 1;
170
    }
19 muzer 171
    if(x < 0){
9 muzer 172
        return -1;
173
    }
174
    return 0;
175
}
176
 
14 muzer 177
bool Bike::hasCollided(QList<Bike *> bikes)
8 tom 178
{
9 muzer 179
    // Do collision detection here
180
    // use linePoints
181
    int i = linePoints.count() - 1;
182
    if(linePoints[i-1].x() < 0 || linePoints[i-1].x() > 800 || linePoints[i-1].y() < 0 || linePoints[i-1].y() > 600)
183
        return true;
184
    if(linePoints[i].x() < 0 || linePoints[i].x() > 800 || linePoints[i].y() < 0 || linePoints[i].y() > 600)
185
        return true;
18 muzer 186
    int j, r;
14 muzer 187
    Bike *bike;
19 muzer 188
    for(r = 0; r < bikes.count(); r++)
18 muzer 189
    {
190
        bike = bikes[r];
21 muzer 191
        int forsubtract = 1;
192
        if(bike->name == name){
193
            forsubtract = 3;
194
        }
195
        for(j = 0; j < bike->linePoints.count() - forsubtract; j++)
18 muzer 196
        {
20 muzer 197
            int jx = bike->linePoints[j].x();
198
            int j1x = bike->linePoints[j+1].x();
199
            int jy = bike->linePoints[j].y();
200
            int j1y = bike->linePoints[j+1].y();
201
            int ix = linePoints[i-1].x();
202
            int i1x = linePoints[i].x();
203
            int iy = linePoints[i-1].y();
204
            int i1y = linePoints[i].y();
21 muzer 205
 
20 muzer 206
            if(angle == 0){
207
                iy += 1;
208
                i1y -= 1;
209
            }
210
            if(angle == 90){
211
                ix -= 1;
212
                i1x += 1;
213
            }
214
            if(angle == 180){
215
                iy -= 1;
216
                i1y += 1;
217
            }
218
            if(angle == 270){
219
                ix += 1;
220
                i1x -= 1;
221
            }
222
            if(!(jx == j1x && i1x == ix) && !(jy == j1y && i1y == iy))
18 muzer 223
            {
16 muzer 224
 
18 muzer 225
                // If not parallel
20 muzer 226
                if(jx == j1x)
18 muzer 227
                {
228
                    // x equal
16 muzer 229
 
20 muzer 230
                    if(ix > jx && i1x < jx || ix < jx && i1x > jx)
18 muzer 231
                    {
20 muzer 232
                        if((sign(iy - jy) != sign(iy - j1y)))
18 muzer 233
                            return true;
234
                    }
235
                }
20 muzer 236
                if(jy == j1y)
18 muzer 237
                {
16 muzer 238
 
20 muzer 239
                    if(iy > jy && i1y < jy || iy < jy && i1y > jy)
18 muzer 240
                    {
20 muzer 241
                        if((sign(ix - jx) != sign(ix - j1x)))
18 muzer 242
                            return true;
243
                    }
244
                }
245
            }
246
        }
9 muzer 247
    }
248
    return false;
8 tom 249
}
250
 
30 tom 251
void Bike::setText(QString text)
252
{
253
        socket->write(text.toAscii().data());
254
        socket->flush();
255
}
256
 
1 muzer 257
void Bike::reset()
258
{
9 muzer 259
    x = rand() % 800;
260
    y = rand() % 600;
261
    linePoints.clear();
1 muzer 262
 
9 muzer 263
    linePoints.append(QPoint(x, y));
1 muzer 264
 
9 muzer 265
    velocity = 1;
266
    angle = 0;
19 muzer 267
    abpool = 0;
9 muzer 268
    show = true;
269
    isReady = true;
270
    hadGo = false;
271
    dead = false;
272
    collided = false;
1 muzer 273
 
9 muzer 274
    socket->write("RESET\n");
1 muzer 275
}
276
 
277
void Bike::readyRead()
278
{
9 muzer 279
    while (socket->canReadLine())
280
    {
281
        QByteArray data = socket->readLine();
282
        QString line = data.trimmed();
1 muzer 283
 
9 muzer 284
        if (line == "L")
285
        {
286
            angle -= 90;
1 muzer 287
 
9 muzer 288
            if (angle >= 360)
289
            {
290
                angle -= 360;
291
            }
292
            if (angle < 0)
293
            {
294
                angle += 360;
295
            }
19 muzer 296
            if(velocity < 5)
24 muzer 297
                velocity += 0.3;
19 muzer 298
            else if(velocity > 5)
24 muzer 299
                velocity -= 0.3;
300
            if(abs(5-velocity)<0.3)
22 muzer 301
                velocity = 5;
19 muzer 302
            if(abpool<10)
303
                abpool += 0.2;
9 muzer 304
            hadGo = true;
305
        }
306
        else if (line == "R")
307
        {
308
            angle += 90;
1 muzer 309
 
9 muzer 310
            if (angle >= 360)
311
            {
312
                angle -= 360;
313
            }
314
            if (angle < 0)
315
            {
316
                angle += 360;
317
            }
19 muzer 318
            if(velocity < 5)
24 muzer 319
                velocity += 0.3;
19 muzer 320
            else if(velocity > 5)
24 muzer 321
                velocity -= 0.3;
322
            if(abs(5-velocity)<0.3)
22 muzer 323
                velocity = 5;
19 muzer 324
            if(abpool<10)
325
                abpool += 0.2;
9 muzer 326
            hadGo = true;
327
        }
328
        else if (line == "A")
329
        {
19 muzer 330
            if(abpool > 0){
331
                velocity += 0.1;
332
                abpool -= 0.5;
20 muzer 333
            } else {
334
                if(velocity < 5)
24 muzer 335
                    velocity += 0.3;
20 muzer 336
                else if(velocity > 5)
24 muzer 337
                    velocity -= 0.3;
338
                if(abs(5-velocity)<0.3)
22 muzer 339
                    velocity = 5;
19 muzer 340
            }
9 muzer 341
            hadGo = true;
342
        }
343
        else if (line == "D")
344
        {
19 muzer 345
            if(abpool > 0){
346
                velocity -= 0.2;
347
                abpool -= 0.5;
348
            }
20 muzer 349
            else {
350
                if(velocity < 5)
24 muzer 351
                    velocity += 0.3;
20 muzer 352
                else if(velocity > 5)
24 muzer 353
                    velocity -= 0.3;
354
                if(abs(5-velocity)<0.3)
22 muzer 355
                    velocity = 5;
20 muzer 356
            }
9 muzer 357
            hadGo = true;
358
        }
359
        else if (line == "N")
360
        {
28 muzer 361
 
19 muzer 362
            if(velocity < 5)
24 muzer 363
                velocity += 0.3;
19 muzer 364
            else if(velocity > 5)
24 muzer 365
                velocity -= 0.3;
366
            if(abs(5-velocity)<0.3)
22 muzer 367
                velocity = 5;
19 muzer 368
            if(abpool<10)
369
                abpool += 0.2;
9 muzer 370
            hadGo = true;
371
        }
372
        else if (line.startsWith("NAME "))
373
        {
374
            name = line.remove(0, 5);
375
            isReady = true;
376
            show = true;
377
        }
378
        else if (line.startsWith("COLOUR "))
379
        {
380
            QStringList list = line.split(" ");
381
            if (list.count() >= 2)
382
            {
383
                colour.setRed(list[1].toInt());
384
            }
385
            if (list.count() >= 3)
386
            {
387
                colour.setGreen(list[2].toInt());
388
            }
389
            if (list.count() >= 4)
390
            {
391
                colour.setBlue(list[3].toInt());
392
            }
393
        }
31 tom 394
                else if (line.startsWith("CHAT "))
395
                {
396
                        QString message = line.remove(0, 5);
397
 
398
                        if (!message.isEmpty())
399
                        {
400
                                emit chat(name, message);
401
                        }
402
 
403
                        hadGo = true;
404
                }
9 muzer 405
    }
1 muzer 406
}
407
 
408
void Bike::disconnected()
409
{
9 muzer 410
    dead = true;
411
    isDisconnected = true;
412
    cout << ":: Disconnected: " << socket->peerAddress().toString().toStdString() << endl;
1 muzer 413
}