class VehicleDriver : public Agent
{
Q_OBJECT
public:
VehicleDriver(Vehicle* vehicle = 0)
void run();
signals:
void updateUI(Agent* agent);
protected slots:
// Start the agents behaviour
virtual void started();
void calculateRoute();
void moveToNextRoad();
virtual void ended();
private:
// Vehicle
Vehicle* vehicle;
QList<Road*> route;
Road* current_road;
int pos; // For iterating route
};
class VehicleDriver : public Agent
{
Q_OBJECT
public:
VehicleDriver(Vehicle* vehicle = 0)
void run();
signals:
void updateUI(Agent* agent);
protected slots:
// Start the agents behaviour
virtual void started();
void calculateRoute();
void moveToNextRoad();
virtual void ended();
private:
// Vehicle
Vehicle* vehicle;
QList<Road*> route;
Road* current_road;
int pos; // For iterating route
};
To copy to clipboard, switch view to plain text mode
#include "VehicleDriver.h"
VehicleDriver::VehicleDriver(Vehicle* vehicle){
// Vehicle
this->vehicle = vehicle;
// Route
this->pos = 0;
this->current_road = 0;
// Connect to websocket_server
this->connect( this , SIGNAL(updateUI(Agent*)) , WebSocketServer::getInstance() , SLOT(push(Agent*)) )
}
void VehicleDriver::run(){
// Move to its own thread
this->moveToThread( thread );
this->connect( this->thread(), SIGNAL( started() ), this, SLOT( started() ) );
this->thread()->start();
}
void VehicleDriver::started(){
qDebug() << this->getId() << "STARTED";
QTimer::singleShot( 0 ,
this ,
SLOT(calculateRoute
()) );
}
void VehicleDriver::calculateRoute(){
// Calculate route...
QTimer::singleShot( 0 ,
this ,
SLOT(moveToNextRoad
()) );
}
void VehicleDriver::moveToNextRoad(){
if( this->pos >= this->route.size() ){
QTimer::singleShot(0 ,
this ,
SLOT(ended
()) );
return;
}
// Update stuff
this->pos++;
emit updateUI( this );
double seconds = // Seconds you would spend travelling through road
QTimer::singleShot( seconds ,
this ,
SLOT(moveToNextRoad
()) );
}
void VehicleDriver::ended(){
qDebug() << this->getId() << "ENDED";
}
#include "VehicleDriver.h"
VehicleDriver::VehicleDriver(Vehicle* vehicle){
// Vehicle
this->vehicle = vehicle;
// Route
this->pos = 0;
this->current_road = 0;
// Connect to websocket_server
this->connect( this , SIGNAL(updateUI(Agent*)) , WebSocketServer::getInstance() , SLOT(push(Agent*)) )
}
void VehicleDriver::run(){
// Move to its own thread
QThread* thread = new QThread();
this->moveToThread( thread );
this->connect( this->thread(), SIGNAL( started() ), this, SLOT( started() ) );
this->thread()->start();
}
void VehicleDriver::started(){
qDebug() << this->getId() << "STARTED";
QTimer::singleShot( 0 , this , SLOT(calculateRoute()) );
}
void VehicleDriver::calculateRoute(){
// Calculate route...
QTimer::singleShot( 0 , this , SLOT(moveToNextRoad()) );
}
void VehicleDriver::moveToNextRoad(){
if( this->pos >= this->route.size() ){
QTimer::singleShot(0 , this , SLOT(ended()) );
return;
}
// Update stuff
this->pos++;
emit updateUI( this );
double seconds = // Seconds you would spend travelling through road
QTimer::singleShot( seconds , this , SLOT(moveToNextRoad()) );
}
void VehicleDriver::ended(){
qDebug() << this->getId() << "ENDED";
}
To copy to clipboard, switch view to plain text mode
Bookmarks