-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcar.cpp
149 lines (128 loc) · 3.71 KB
/
car.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
142
143
144
145
146
147
148
149
#include "car.h"
Car::Car(const Car &other)
: pos(other.pos)
, angle(other.angle)
, velocity(other.velocity)
, timer(new QTimer)
, color(other.color)
{
ConnectTimer();
}
Car::Car(Car &&other) noexcept
: pos(other.pos)
, angle(other.angle)
, velocity(other.velocity)
, timer(other.timer)
, color(other.color)
{
ConnectTimer();
other.timer = nullptr;
}
Car::Car(RoadPoint pos, float angle)
: pos(pos)
, angle(angle)
, velocity(0)
, timer(new QTimer)
{
QRandomGenerator rng(std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::system_clock::now().time_since_epoch())
.count());
color = colors[rng() % 4];
ConnectTimer();
}
Car::~Car()
{
if (timer != nullptr)
{
delete timer;
timer = nullptr;
}
}
void Car::Rotate(float delta)
{
angle += delta;
}
void Car::setVelocity(float newVelocity)
{
if (newVelocity < 0) {
velocity = 0;
} else if (newVelocity > MAX_SPEED) {
velocity = MAX_SPEED;
} else {
velocity = newVelocity;
}
}
void Car::setLevel(int newLevel)
{
if (newLevel != pos.level)
{
pos.level = newLevel;
emit needToUpdateLevelOfCar();
}
}
void Car::Update()
{
pos.pos.setX(pos.pos.x() + velocity * qCos(angle));
pos.pos.setY(pos.pos.y() + velocity * qSin(angle));
}
void Car::ConnectTimer()
{
connect(timer, &QTimer::timeout, this, &Car::Update);
if (!timer->isActive())
{
timer->setInterval(TICK_TIME);
timer->start();
}
}
RoadPoint Car::GetPosition() const
{
return pos;
}
RoadPoint Car::PredictPosition() const
{
RoadPoint prediction = pos;
prediction.pos.setX(prediction.pos.x() + velocity * qCos(angle));
prediction.pos.setY(prediction.pos.y() + velocity * qSin(angle));
return prediction;
}
float Car::GetAngle() const
{
return angle;
}
float Car::GetVelocity() const
{
return velocity;
}
void Car::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
auto vectorToPoint1 = rotateVector(QPointF(CAR_LENGTH / 2, CAR_WIDTH / 2), angle);
auto vectorToPoint2 = rotateVector(QPointF(CAR_LENGTH / 2, -CAR_WIDTH / 2), angle);
auto vectorToPoint3 = rotateVector(QPointF(-CAR_LENGTH / 2, -CAR_WIDTH / 2), angle);
auto vectorToPoint4 = rotateVector(QPointF(-CAR_LENGTH / 2, CAR_WIDTH / 2), angle);
auto point1 = pos.pos + vectorToPoint1;
auto point2 = pos.pos + vectorToPoint2;
auto point3 = pos.pos + vectorToPoint3;
auto point4 = pos.pos + vectorToPoint4;
QPolygonF poly;
poly << point1 << point2 << point3 << point4;
painter->save();
painter->setPen(QPen(color));
painter->setBrush(QBrush(color));
painter->drawPolygon(poly);
painter->restore();
}
QRectF Car::boundingRect() const
{
auto point1 = QPointF(pos.pos.x() + CAR_LENGTH * qCos(angle),
pos.pos.y() + CAR_WIDTH * qSin(angle));
auto point2 = QPointF(pos.pos.x() + CAR_LENGTH * qCos(angle),
pos.pos.y() - CAR_WIDTH * qSin(angle));
auto point3 = QPointF(pos.pos.x() - CAR_LENGTH * qCos(angle),
pos.pos.y() + CAR_WIDTH * qSin(angle));
auto point4 = QPointF(pos.pos.x() - CAR_LENGTH * qCos(angle),
pos.pos.y() - CAR_WIDTH * qSin(angle));
return QRectF(QPointF(std::min({point1.x(), point2.x(), point3.x(), point4.x()}),
std::min({point1.y(), point2.y(), point3.y(), point4.y()})),
QPointF(std::max({point1.x(), point2.x(), point3.x(), point4.x()}),
std::max({point1.y(), point2.y(), point3.y(), point4.y()})));
}