-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroad.cpp
125 lines (106 loc) · 3.41 KB
/
road.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
#include "road.h"
float Road::speed = 0.015;
Road::Road(QWidget* parent) : QOpenGLWidget(parent)
{
currentCar = 0;
setPreset(DISABLED, RIGHT);
timer = new QTimer(this);
timer->start(33);
connect(timer, SIGNAL(timeout()), this, SLOT(update()));
connect(timer, SIGNAL(timeout()), this, SLOT(updateCars()));
spawnTimer = new QTimer(this);
spawnTimer->start(0);
paused = false;
connect(spawnTimer, SIGNAL (timeout()), this, SLOT (spawnCar()));
}
void Road::clear()
{
cars.clear();
}
void Road::setPreset(Road::RoadPreset preset, Road::Direction direction)
{
std::normal_distribution<> dist_a(4000,1500); // normal distribution with mean 4s and standard deviation 1.5s
std::normal_distribution<> dist_b(3000,500);
std::normal_distribution<> dist_c(6000,2000);
this->preset = preset;
this->direction = direction;
// hard-coded traffic flow
switch(preset) {
case RAND_A:
for (int i=0; i < 50; i++) {
gaps.push_back(abs(dist_a(*QRandomGenerator::global())));
}
break;
case RAND_B:
for (int i=0; i < 50; i++) {
gaps.push_back(abs(dist_b(*QRandomGenerator::global())));
}
break;
case RAND_C:
for (int i=0; i < 50; i++) {
gaps.push_back(abs(dist_c(*QRandomGenerator::global())));
}
break;
case RAND_U:
for (int i=0; i < 50; i++) {
gaps.push_back(QRandomGenerator::global()->bounded(500,4000)); // uniform distribution 0.5s to 4s
}
break;
case DISABLED:
gaps = {};
}
if (preset != DISABLED) spawnTimer->start(gaps[0]);
}
void Road::setPaused(bool paused)
{
// only do this if it wasn't already paused
if (paused && !this->paused) {
for (Car* car : cars) {
car->setBlocked(true);
}
disconnect(timer, SIGNAL(timeout()), this, SLOT(updateCars()));
disconnect(spawnTimer, SIGNAL (timeout()), this, SLOT (spawnCar()));
} else if (!paused && this->paused) {
for (Car* car : cars) {
car->setBlocked(false);
}
connect(timer, SIGNAL(timeout()), this, SLOT(updateCars()));
connect(spawnTimer, SIGNAL (timeout()), this, SLOT (spawnCar()));
}
this->paused = paused;
}
void Road::initializeGL()
{
openGLFunctions = QOpenGLContext::currentContext()->functions();
// glClearColor doesnt have active alpha channel by default, this allows roads to
// be transparent at the cost of always drawing the cars on top of everything
setAttribute(Qt::WA_AlwaysStackOnTop);
glClearColor(0,0,0,0);
}
void Road::paintGL()
{
// paint every car
for (Car* car : cars) {
drawCar(car);
}
}
void Road::drawCar(Car* car)
{
glBegin(GL_POLYGON);
glColor3f(car->getColor()->redF(), car->getColor()->greenF(), car->getColor()->blueF()); // grab the car's color
for (std::vector<Vertex>::iterator vertex = car->vertices.begin(); vertex != car->vertices.end(); ++vertex) // draw every vertex beloning to the car as a polygon
{
glVertex2f((vertex->x) + car->offsetX, (vertex->y) + car->offsetY);
}
glEnd();
}
void Road::spawnCar()
{
if (preset == DISABLED) return;
cars.insert(cars.begin(), createCar());
currentCar++;
// loop through gaps
if (currentCar == (int) gaps.size()) currentCar = 0;
// update next spawn time
spawnTimer->setInterval(gaps[currentCar]);
}