Rev 11 |
Rev 16 |
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));
velocity = 1;
angle = 0;
name = "";
show = false;
isReady = false;
hadGo = false;
dead = false;
collided = 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 (!collided)
{
collided = hasCollided();
if (angle == 0)
{
y -= velocity;
}
else if (angle == 90)
{
x += velocity;
}
else if (angle == 180)
{
y += velocity;
}
else if (angle == 270)
{
x -= velocity;
}
}
else
{
linePoints.clear();
}
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 || bike->collided)
{
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(" ");
socket->write(QString::number(bike->colour.red()).toAscii());
socket->write(" ");
socket->write(QString::number(bike->colour.green()).toAscii());
socket->write(" ");
socket->write(QString::number(bike->colour.blue()).toAscii());
socket->write("\n");
}
}
}
linePoints.append(QPoint(x, y));
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;
}
int sign(float x){
if(x > 0){
return 1;
}
if(x < 0){
return -1;
}
return 0;
}
bool Bike::hasCollided()
{
// Do collision detection here
// use linePoints
int i = linePoints.count() - 1;
if(linePoints[i-1].x() < 0 || linePoints[i-1].x() > 800 || linePoints[i-1].y() < 0 || linePoints[i-1].y() > 600)
return true;
if(linePoints[i].x() < 0 || linePoints[i].x() > 800 || linePoints[i].y() < 0 || linePoints[i].y() > 600)
return true;
for(int j = 0; j < linePoints.count() - 2; j++)
{
if(!(linePoints[j].x() == linePoints[j+1].x() && linePoints[i].x() == linePoints[i-1].x()) && !(linePoints[j].y() == linePoints[j+1].y() && linePoints[i].y() == linePoints[i-1].y()))
{
// If not parallel
if(linePoints[j].x() == linePoints[j+1].x())
{
// x equal
if(linePoints[i-1].x() > linePoints[j].x() && linePoints[i].x() < linePoints[j].x() || linePoints[i-1].x() < linePoints[j].x() && linePoints[i].x() > linePoints[j].x()){
if((sign(linePoints[i-1].y() - linePoints[j].y()) != sign(linePoints[i-1].y() - linePoints[j+1].y())))
return true;
}
}
else if(linePoints[j].y() == linePoints[j+1].y())
{
if(linePoints[i-1].y() > linePoints[j].y() && linePoints[i].y() < linePoints[j].y() || linePoints[i-1].y() < linePoints[j].y() && linePoints[i].y() > linePoints[j].y())
{
if((sign(linePoints[i-1].x() - linePoints[j].x()) != sign(linePoints[i-1].x() - linePoints[j+1].x())))
return true;
}
}
}
}
return false;
}
void Bike::reset()
{
x = rand() % 800;
y = rand() % 600;
linePoints.clear();
linePoints.append(QPoint(x, y));
velocity = 1;
angle = 0;
show = true;
isReady = true;
hadGo = false;
dead = false;
collided = false;
socket->write("RESET\n");
}
void Bike::readyRead()
{
while (socket->canReadLine())
{
QByteArray data = socket->readLine();
QString line = data.trimmed();
if (line == "L")
{
angle -= 90;
if (angle >= 360)
{
angle -= 360;
}
if (angle < 0)
{
angle += 360;
}
hadGo = true;
}
else if (line == "R")
{
angle += 90;
if (angle >= 360)
{
angle -= 360;
}
if (angle < 0)
{
angle += 360;
}
hadGo = true;
}
else if (line == "A")
{
velocity += 0.1;
hadGo = true;
}
else if (line == "D")
{
velocity -= 0.2;
hadGo = true;
}
else if (line == "N")
{
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());
}
if (list.count() >= 3)
{
colour.setGreen(list[2].toInt());
}
if (list.count() >= 4)
{
colour.setBlue(list[3].toInt());
}
}
}
}
void Bike::disconnected()
{
dead = true;
isDisconnected = true;
cout << ":: Disconnected: " << socket->peerAddress().toString().toStdString() << endl;
}