Rev 5 |
Go to most recent revision |
Blame |
Compare with Previous |
Last modification |
View Log
| RSS feed
#include "bike.h"
Bike::Bike(QTcpSocket *sock, int i)
{
socket = sock;
id = i;
connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()));
connect(socket, SIGNAL(disconnected()), this, SLOT(disconnected()));
isDisconnected = false;
x = rand() % 800;
y = rand() % 600;
linePoints.append(QPoint(x, y));
linePoints.append(QPoint(x, y));
velocity = 1;
angle = 0;
name = "";
show = false;
isReady = false;
hadGo = false;
dead = false;
colour.setBlue(0);
colour.setRed(0);
colour.setGreen(0);
if (!socket->waitForReadyRead(2000))
{
socket->disconnectFromHost();
show = false;
isReady = true;
hadGo = false;
}
}
void Bike::draw(QPainter *painter)
{
if (show)
{
painter->setPen(colour);
painter->setFont(QFont("sans", 12));
for (int i = 0; i < linePoints.count(); i++)
{
QPoint point1;
QPoint point2;
if (i == 0)
{
point1 = linePoints[0];
}
else
{
point1 = linePoints[i - 1];
}
point2 = linePoints[i];
painter->drawLine(point1, point2);
}
if (angle == 0)
{
y -= velocity;
}
else if (angle == 90)
{
x += velocity;
}
else if (angle == 180)
{
y += velocity;
}
else if (angle == 270)
{
x -= velocity;
}
if (angle == 0 || angle == 180)
{
painter->fillRect(x - 5, y - 15, 10, 30, colour);
}
else
{
painter->fillRect(x - 15, y - 5, 30, 10, colour);
}
painter->drawText(x, y - 20, name);
}
}
void Bike::run(QList<Bike *> bikes)
{
for (int i = 0; i < bikes.count(); i++)
{
Bike *bike = bikes[i];
if (bike->isReady && bike->show)
{
if (bike->dead)
{
socket->write("DEAD ");
socket->write(bike->name.toAscii());
socket->write("\n");
}
else
{
socket->write("BIKE ");
socket->write(bike->name.toAscii());
socket->write(" ");
socket->write(QString::number(bike->x).toAscii());
socket->write(" ");
socket->write(QString::number(bike->y).toAscii());
socket->write("\n");
}
}
}
if (dead)
{
show = false;
isReady = true;
hadGo = false;
dead = false;
}
if (!dead)
{
socket->write("G\n");
socket->flush();
hadGo = false;
if (!socket->waitForReadyRead(2000))
{
socket->disconnectFromHost();
dead = true;
}
if (!hadGo)
{
socket->disconnectFromHost();
dead = true;
}
}
hasHadGo = true;
}
void Bike::reset()
{
x = rand() % 800;
y = rand() % 600;
linePoints.clear();
linePoints.append(QPoint(x, y));
linePoints.append(QPoint(x, y));
velocity = 1;
angle = 0;
show = true;
isReady = true;
hadGo = false;
dead = false;
socket->write("RESET\n");
}
void Bike::readyRead()
{
while (socket->canReadLine())
{
QByteArray data = socket->readLine();
QString line = data.trimmed();
if (line == "L")
{
linePoints.append(QPoint(x, y));
angle -= 90;
if (angle >= 360)
{
angle -= 360;
}
if (angle < 0)
{
angle += 360;
}
hadGo = true;
}
else if (line == "R")
{
linePoints.append(QPoint(x, y));
angle += 90;
if (angle >= 360)
{
angle -= 360;
}
if (angle < 0)
{
angle += 360;
}
hadGo = true;
}
else if (line == "A")
{
linePoints[linePoints.size() - 1] = QPoint(x, y);
velocity += 0.1;
hadGo = true;
}
else if (line == "D")
{
linePoints[linePoints.size() - 1] = QPoint(x, y);
velocity -= 0.2;
hadGo = true;
}
else if (line == "N")
{
linePoints[linePoints.size() - 1] = QPoint(x, y);
hadGo = true;
}
else if (line.startsWith("NAME "))
{
name = line.remove(0, 5);
isReady = true;
show = true;
}
else if (line.startsWith("COLOUR "))
{
QStringList list = line.split(" ");
if (list.count() >= 2)
{
colour.setRed(list[1].toInt());
}
else if (list.count() >= 3)
{
colour.setGreen(list[2].toInt());
}
else if (list.count() >= 4)
{
colour.setBlue(list[3].toInt());
}
}
}
}
void Bike::disconnected()
{
dead = true;
isDisconnected = true;
cout << ":: Disconnected: " << socket->peerAddress().toString().toStdString() << endl;
}