-
Notifications
You must be signed in to change notification settings - Fork 1
/
carwithtrailer.cpp
142 lines (116 loc) · 4.73 KB
/
carwithtrailer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include "vehicle.h";
#include "carwithtrailer.h"
#include <QGraphicsPixmapItem>
#include <QTimer>
#include "qmath.h"
#include <typeinfo>
#include <QDebug>
#include <iostream>
#include <QPen>
#include <iostream>
#include "trafficlights.h"
using namespace std;
//Performance monitoring methods, comment out in release
//void timer::start() {
// begTime = clock();
//}
//unsigned long timer::elapsedTime() {
// return ((unsigned long) clock() - begTime) / CLOCKS_PER_SEC;
//}
//bool timer::isTimeout(unsigned long seconds) {
// return seconds >= elapsedTime();
//}
//timer performance_timer;
carwithtrailer::carwithtrailer(Subject *aVehicle, int width, int height, double aggression, int xStartingPos, int yStartingPos,string direction, QGraphicsItem *parent):Vehicle(width,height,aggression,xStartingPos,yStartingPos,direction)
{
//qDebug()<< xStartingPos;
// Colin changed for debugging setPos(xStartingPos, yStartingPos);
//setUp();
//this->topSpeed = 100;
this->aVehicle = aVehicle;
this->aVehicle->attach(this);
}
//Thread setup , correct way to do it, QT documentation is incorrect in subclassing from QThread
//i.e. QObject and move this object to qthread in main states
void carwithtrailer::DoThreadSetup(QThread &cThread, vector <Vehicle*> vehicles,trafficlights *trafficlight){
//connect the signal emitted from the thread "starting" to the slot update
this->vehicles = vehicles;
this->trafficlight= trafficlight;
connect(&cThread,SIGNAL(started()),this,SLOT(update()));
}
void carwithtrailer::updateTrafficLightsSignal(bool trafficlights_On)
{
trafficLightsOn = trafficlights_On;
}
//Update no longer accepts vehicles as they are now threads, and you cannot
//pass data through this siganl and slot therefore we make a copy in do thread setup
void carwithtrailer::update(){ //Maybe only pass in vehicles that are in its scope
//Performance monitoring methods, comment out in release
// performance_timer.start();
//Only execute the following when the thread status is active
while(this->getThreadStatus() == true){
//Sleep thread for 100 ms every iteration
QThread::currentThread()->msleep(100);
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
bool stopIfNearLights;
//Colins - Thread Safety
if(this->area->collidesWithItem(trafficlight)) {
stopIfNearLights = true ;
// qDebug()<< "touching traffic light";
}
else{
stopIfNearLights = false;
// qDebug()<< " not touching traffic light";
}
if(trafficLightsOn == true){
for(int i=0; i< vehicles.size();i++){
//If lights are on and if the current car collides with a vehicle and that vehicle collides with the traffic lights and the lights are on
if(this->area->collidesWithItem(vehicles.at(i)->area) && vehicles.at(i)->area->collidesWithItem(trafficlight))
stopIfNearLights =true;
}
}
//qDebug()<< "trafficLightsOn boolean is " << trafficLightsOn;
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
if(trafficLightsOn == true && stopIfNearLights ==true){
//stop
}
else{
if(aggression == High){
context = new StrategyContext(new IncreaseSpeedAggressive());
context->executeStrategyDecision(this->vehicles, this);
context = new StrategyContext(new DecreaseSpeed());
context->executeStrategyDecision(this->vehicles, this);
if(this->direction== "east"){
context = new StrategyContext(new OvertakeHigh());
context->executeStrategyDecision(this->vehicles, this);
}
}
if(aggression == Medium){
context = new StrategyContext(new IncreaseSpeedMedium());
context->executeStrategyDecision(this->vehicles, this);
context = new StrategyContext(new DecreaseSpeed());
context->executeStrategyDecision(this->vehicles, this);
if(this->direction== "east"){
context = new StrategyContext(new OverTakeMedium());
context->executeStrategyDecision(this->vehicles, this);}
}
if(aggression == Low){
context = new StrategyContext(new IncreaseSpeedLow());
context->executeStrategyDecision(this->vehicles, this);
context = new StrategyContext(new DecreaseSpeed());
context->executeStrategyDecision(this->vehicles, this);
}
// Used for performance monitoring
timesCalled++;
/* //Performance monitoring methods, comment out in release
unsigned long seconds = 5;
cout << "\nelapsed time: "<<performance_timer.elapsedTime() << endl;
if(performance_timer.elapsedTime() >= seconds) {
cout<< "\nPerformance Monitoring timed stop \n";
//Turn off all threads
this->setThreadStatus(false);
cout << "Total times vehicals have been called: " << timesCalled << "\n";
}*/
}
}
}