Subversion Repositories QTron

Rev

Rev 18 | Rev 20 | 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];
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
 
18 muzer 194
                // If not parallel
195
                if(bike->linePoints[j].x() == bike->linePoints[j+1].x())
196
                {
197
                    // x equal
16 muzer 198
 
18 muzer 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
                }
19 muzer 205
                if(bike->linePoints[j].y() == bike->linePoints[j+1].y())
18 muzer 206
                {
16 muzer 207
 
18 muzer 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;
19 muzer 230
    abpool = 0;
9 muzer 231
    show = true;
232
    isReady = true;
233
    hadGo = false;
234
    dead = false;
235
    collided = false;
1 muzer 236
 
9 muzer 237
    socket->write("RESET\n");
1 muzer 238
}
239
 
240
void Bike::readyRead()
241
{
9 muzer 242
    while (socket->canReadLine())
243
    {
244
        QByteArray data = socket->readLine();
245
        QString line = data.trimmed();
1 muzer 246
 
9 muzer 247
        if (line == "L")
248
        {
249
            angle -= 90;
1 muzer 250
 
9 muzer 251
            if (angle >= 360)
252
            {
253
                angle -= 360;
254
            }
255
            if (angle < 0)
256
            {
257
                angle += 360;
258
            }
19 muzer 259
            if(velocity < 5)
260
                velocity += 0.5;
261
            else if(velocity > 5)
262
                velocity -= 0.5;
263
            if(abpool<10)
264
                abpool += 0.2;
9 muzer 265
            hadGo = true;
266
        }
267
        else if (line == "R")
268
        {
269
            angle += 90;
1 muzer 270
 
9 muzer 271
            if (angle >= 360)
272
            {
273
                angle -= 360;
274
            }
275
            if (angle < 0)
276
            {
277
                angle += 360;
278
            }
19 muzer 279
            if(velocity < 5)
280
                velocity += 0.5;
281
            else if(velocity > 5)
282
                velocity -= 0.5;
283
            if(abpool<10)
284
                abpool += 0.2;
9 muzer 285
            hadGo = true;
286
        }
287
        else if (line == "A")
288
        {
19 muzer 289
            if(abpool > 0){
290
                velocity += 0.1;
291
                abpool -= 0.5;
292
            }
9 muzer 293
            hadGo = true;
294
        }
295
        else if (line == "D")
296
        {
19 muzer 297
            if(abpool > 0){
298
                velocity -= 0.2;
299
                abpool -= 0.5;
300
            }
9 muzer 301
            hadGo = true;
302
        }
303
        else if (line == "N")
304
        {
19 muzer 305
            if(velocity < 5)
306
                velocity += 0.5;
307
            else if(velocity > 5)
308
                velocity -= 0.5;
309
            if(abpool<10)
310
                abpool += 0.2;
9 muzer 311
            hadGo = true;
312
        }
313
        else if (line.startsWith("NAME "))
314
        {
315
            name = line.remove(0, 5);
316
            isReady = true;
317
            show = true;
318
        }
319
        else if (line.startsWith("COLOUR "))
320
        {
321
            QStringList list = line.split(" ");
322
            if (list.count() >= 2)
323
            {
324
                colour.setRed(list[1].toInt());
325
            }
326
            if (list.count() >= 3)
327
            {
328
                colour.setGreen(list[2].toInt());
329
            }
330
            if (list.count() >= 4)
331
            {
332
                colour.setBlue(list[3].toInt());
333
            }
334
        }
335
    }
1 muzer 336
}
337
 
338
void Bike::disconnected()
339
{
9 muzer 340
    dead = true;
341
    isDisconnected = true;
342
    cout << ":: Disconnected: " << socket->peerAddress().toString().toStdString() << endl;
1 muzer 343
}