-
Notifications
You must be signed in to change notification settings - Fork 0
/
Segment.cpp
161 lines (132 loc) · 3.9 KB
/
Segment.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
150
151
152
153
154
155
156
157
158
159
160
161
#include "Segment.hpp"
#include "Utility.hpp"
#include <SFML/Graphics/RenderTarget.hpp>
void Segment::setCurve(float i)
{
mCurve = i;
}
float Segment::getCurve() const
{
return mCurve;
}
Point& Segment::point1()
{
return mPoint1;
}
Point&Segment::point2()
{
return mPoint2;
}
const Point& Segment::point1() const
{
return mPoint1;
}
const Point& Segment::point2() const
{
return mPoint2;
}
void Segment::setSegmentColors(Colors::Type color)
{
mColors.setType(color);
}
void Segment::setIndex(std::size_t index)
{
mIndex = index;
}
std::size_t Segment::getIndex() const
{
return mIndex;
}
void Segment::setClip(float clip)
{
mClip = clip;
}
const float Segment::getClip() const
{
return mClip;
}
const Segment::SpritesContainer& Segment::getSprites() const
{
return mSprites;
}
void Segment::addSprite(Sprites::Ptr sprite)
{
mSprites.push_back(std::move(sprite));
}
const Segment::CarsContainer& Segment::getCars() const
{
return mCars;
}
void Segment::removeCar()
{
if(!mCars.empty())
mCars.pop_front();
}
void Segment::addCar(Cars::Ptr car)
{
mCars.push_back(car);
}
void Segment::setGrounds(float width, float fog)
{
auto lanes = 3u;
// Landscape
mLandscape.setSize({ width, mPoint1.screen.y - mPoint2.screen.y });
mLandscape.setPosition(0, mPoint2.screen.y);
mLandscape.setFillColor(mColors.getGrass());
// Rumble sides
auto rumbleWidth1 = utility::rumbleWidth(mPoint1.screen.w, lanes);
auto rumbleWidth2 = utility::rumbleWidth(mPoint2.screen.w, lanes);
mRumbleSide1.setVertices(
mPoint1.screen.x - mPoint1.screen.w - rumbleWidth1, mPoint1.screen.y,
mPoint1.screen.x - mPoint1.screen.w, mPoint1.screen.y,
mPoint2.screen.x - mPoint2.screen.w, mPoint2.screen.y,
mPoint2.screen.x - mPoint2.screen.w - rumbleWidth2, mPoint2.screen.y, mColors.getRumble());
mRumbleSide2.setVertices(
mPoint1.screen.x + mPoint1.screen.w + rumbleWidth1, mPoint1.screen.y,
mPoint1.screen.x + mPoint1.screen.w, mPoint1.screen.y,
mPoint2.screen.x + mPoint2.screen.w, mPoint2.screen.y,
mPoint2.screen.x + mPoint2.screen.w + rumbleWidth2, mPoint2.screen.y, mColors.getRumble());
// Main Road
mMainRoad.setVertices(
mPoint1.screen.x - mPoint1.screen.w, mPoint1.screen.y,
mPoint1.screen.x + mPoint1.screen.w, mPoint1.screen.y,
mPoint2.screen.x + mPoint2.screen.w, mPoint2.screen.y,
mPoint2.screen.x - mPoint2.screen.w, mPoint2.screen.y, mColors.getRoad());
// Lanes
auto laneMarkerWidth1 = utility::laneMarkerWidth(mPoint1.screen.w, lanes);
auto laneMarkerWidth2 = utility::laneMarkerWidth(mPoint2.screen.w, lanes);
auto lanew1 = mPoint1.screen.w * 2 / lanes;
auto lanew2 = mPoint2.screen.w * 2 / lanes;
auto lanex1 = mPoint1.screen.x - mPoint1.screen.w + lanew1;
auto lanex2 = mPoint2.screen.x - mPoint2.screen.w + lanew2;
for (auto lane = 1u; lane < lanes; lanex1 += lanew1 + 1, lanex2 += lanew2 + 1, lane++)
{
if (lane == 1)
mLanes1.setVertices(
lanex1 - laneMarkerWidth1 / 2, mPoint1.screen.y,
lanex1 + laneMarkerWidth1 / 2, mPoint1.screen.y,
lanex2 + laneMarkerWidth2 / 2, mPoint2.screen.y,
lanex2 - laneMarkerWidth2 / 2, mPoint2.screen.y, mColors.getLane());
else
mLanes2.setVertices(
lanex1 - laneMarkerWidth1 / 2, mPoint1.screen.y,
lanex1 + laneMarkerWidth1 / 2, mPoint1.screen.y,
lanex2 + laneMarkerWidth2 / 2, mPoint2.screen.y,
lanex2 - laneMarkerWidth2 / 2, mPoint2.screen.y, mColors.getLane());
}
// Fog effect
mFog.setSize({ width, mPoint1.screen.y - mPoint2.screen.y });
mFog.setPosition(0, mPoint2.screen.y);
mFog.setFillColor(sf::Color(0, 81, 8, 255 - static_cast<unsigned char>(fog * 255)));
}
void Segment::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
states.transform *= getTransform();
target.draw(mLandscape, states);
target.draw(mRumbleSide1, states);
target.draw(mRumbleSide2, states);
target.draw(mMainRoad, states);
target.draw(mLanes1, states);
target.draw(mLanes2, states);
target.draw(mFog, states);
}