-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTrafficControl.h
237 lines (211 loc) · 6.42 KB
/
TrafficControl.h
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#include <SFML/Graphics.hpp>
#include <vector>
#include <map>
using namespace sf;
using namespace std;
enum State
{
RED,
YELLOW,
GREEN
};
struct LightCircle
{
CircleShape circle;
State state;
};
struct TrafficLight
{
vector<LightCircle> lights;
State currentState;
RectangleShape frame; // Optional frame for visual enhancement
TrafficLight(Vector2f position, bool horizontal = false, float lightSpacing = 50.0f)
: currentState(RED)
{
const Color defaultColor(50, 50, 50); // Default "off" color
const float rad = lightSpacing / 2.0f;
// Create frame for traffic light
float width = horizontal ? 3 * lightSpacing : lightSpacing;
float height = horizontal ? lightSpacing : 3 * lightSpacing;
frame.setSize(Vector2f(width, height));
frame.setFillColor(Color(30, 30, 30));
frame.setOutlineThickness(2);
frame.setOutlineColor(Color::Black);
frame.setOrigin(width / 2, height / 2);
frame.setPosition(position);
// Create lights
for (int i = 0; i < 3; ++i)
{
LightCircle light;
light.circle.setRadius(rad);
light.circle.setOrigin(rad, rad);
light.circle.setFillColor(defaultColor);
Vector2f lightPos = horizontal
? Vector2f(position.x + i * lightSpacing - width / 2 + rad, position.y)
: Vector2f(position.x, position.y + i * lightSpacing - height / 2 + rad);
light.circle.setPosition(lightPos);
// Assign states
switch (i)
{
case 0:
light.state = RED;
break;
case 1:
light.state = YELLOW;
break;
case 2:
light.state = GREEN;
break;
}
lights.push_back(light);
}
updateLightColors(RED); // Set initial light state
}
void updateLightColors(State activeState)
{
currentState = activeState; // Update current state
const Color offColor(50, 50, 50);
for (auto &light : lights)
{
if (light.state == activeState)
{
switch (light.state)
{
case RED:
light.circle.setFillColor(Color::Red);
break;
case YELLOW:
light.circle.setFillColor(Color::Yellow);
break;
case GREEN:
light.circle.setFillColor(Color::Green);
break;
}
}
else
{
light.circle.setFillColor(offColor);
}
}
}
void draw(RenderWindow &window) const
{
window.draw(frame); // Draw frame
for (const auto &light : lights)
{
window.draw(light.circle);
}
}
};
class TrafficController
{
public:
// static constexpr float EMERGENCY_ADJUSTMENT = 1.0f;
// static constexpr float YELLOW_EMERGENCY_FACTOR = 0.5f;
enum class LightPhase
{
NORTH_SOUTH_GREEN,
NORTH_SOUTH_YELLOW,
EAST_WEST_GREEN,
EAST_WEST_YELLOW
};
struct PhaseConfig
{
float duration;
State northSouthState;
State eastWestState;
};
private:
TrafficLight northSouthLight;
TrafficLight eastWestLight;
LightPhase currentPhase;
float phaseTimer;
const std::map<LightPhase, PhaseConfig> phaseMap = {
{LightPhase::NORTH_SOUTH_GREEN, {6.0f, State::GREEN, State::RED}},
{LightPhase::NORTH_SOUTH_YELLOW, {3.0f, State::YELLOW, State::RED}},
{LightPhase::EAST_WEST_GREEN, {6.0f, State::RED, State::GREEN}},
{LightPhase::EAST_WEST_YELLOW, {3.0f, State::RED, State::YELLOW}},
};
public:
TrafficController(const Vector2f &northSouthPosition, const Vector2f &eastWestPosition)
: northSouthLight(northSouthPosition, false),
eastWestLight(eastWestPosition, true),
currentPhase(LightPhase::EAST_WEST_GREEN),
phaseTimer(0.0f)
{
}
std::string getEastWestState() const
{
return stateToString(phaseMap.at(currentPhase).eastWestState);
}
std::string getNorthSouthState() const
{
return stateToString(phaseMap.at(currentPhase).northSouthState);
}
void update(float deltaTime)
{
phaseTimer += deltaTime;
const auto &config = phaseMap.at(currentPhase);
if (phaseTimer >= config.duration)
{
// Transition to the next phase
phaseTimer = 0.0f;
switch (currentPhase)
{
case LightPhase::NORTH_SOUTH_GREEN:
currentPhase = LightPhase::NORTH_SOUTH_YELLOW;
break;
case LightPhase::NORTH_SOUTH_YELLOW:
currentPhase = LightPhase::EAST_WEST_GREEN;
break;
case LightPhase::EAST_WEST_GREEN:
currentPhase = LightPhase::EAST_WEST_YELLOW;
break;
case LightPhase::EAST_WEST_YELLOW:
currentPhase = LightPhase::NORTH_SOUTH_GREEN;
break;
}
}
// Update lights based on the current phase
northSouthLight.updateLightColors(config.northSouthState);
eastWestLight.updateLightColors(config.eastWestState);
}
void draw(RenderWindow &window)
{
northSouthLight.draw(window);
eastWestLight.draw(window);
}
private:
static std::string stateToString(State state)
{
switch (state)
{
case State::GREEN:
return "green";
case State::YELLOW:
return "yellow";
case State::RED:
return "red";
default:
return "unknown";
}
}
void transitionToNextPhase()
{
switch (currentPhase)
{
case LightPhase::NORTH_SOUTH_GREEN:
currentPhase = LightPhase::NORTH_SOUTH_YELLOW;
break;
case LightPhase::NORTH_SOUTH_YELLOW:
currentPhase = LightPhase::EAST_WEST_GREEN;
break;
case LightPhase::EAST_WEST_GREEN:
currentPhase = LightPhase::EAST_WEST_YELLOW;
break;
case LightPhase::EAST_WEST_YELLOW:
currentPhase = LightPhase::NORTH_SOUTH_GREEN;
break;
}
}
};