-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVehicle.cpp
More file actions
149 lines (124 loc) · 4.46 KB
/
Copy pathVehicle.cpp
File metadata and controls
149 lines (124 loc) · 4.46 KB
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
#include "Vehicle.hpp"
#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;
Vehicle::Vehicle(const sf::Vector2f& startPos, VehicleType type, Direction dir)
: type(type), direction(dir), textureLoaded(false), passedStopLine(false), stoppedTime(0.f) // initialize false
{
lastPosition = startPos;
// Choose a texture file
std::string fileName = "C:/TrafficLightSimulation/assets/car.png";
switch (type) {
case VehicleType::Normal: fileName = "C:/TrafficLightSimulation/assets/car.png"; break;
case VehicleType::Taxi: fileName = "C:/TrafficLightSimulation/assets/taxi.png"; break;
case VehicleType::Ambulance: fileName = "C:/TrafficLightSimulation/assets/ambulance.png"; break;
case VehicleType::Audi: fileName = "C:/TrafficLightSimulation/assets/Audi.png"; break;
case VehicleType::Truck: fileName = "C:/TrafficLightSimulation/assets/mini_truck.png"; break;
case VehicleType::Bus: fileName = "C:/TrafficLightSimulation/assets/police.png"; break;
case VehicleType::BlackViper:fileName = "C:/TrafficLightSimulation/assets/black_viper.png"; break;
case VehicleType::BigTruck: fileName = "C:/TrafficLightSimulation/assets/truck.png"; break;
}
if (texture.loadFromFile(fileName)) {
textureLoaded = true;
sprite.setTexture(texture);
// Scale down if needed
sprite.setScale(0.3f, 0.3f);
// Center origin
sf::Vector2u texSize = texture.getSize();
sprite.setOrigin(texSize.x * 0.5f, texSize.y * 0.5f);
// Position
sprite.setPosition(startPos);
// Rotate based on direction
switch (dir) {
case Direction::LeftToRight:
// No rotation needed if your car.png faces right by default
sprite.setRotation(0.f);
break;
case Direction::RightToLeft:
// 180 degrees if your car.png is oriented to the right by default
sprite.setRotation(180.f);
break;
case Direction::TopToBottom:
// 90 degrees clockwise
sprite.setRotation(90.f);
break;
case Direction::BottomToTop:
// 270 degrees clockwise (or -90)
sprite.setRotation(270.f);
break;
}
} else {
std::cerr << "Failed to load vehicle image from " << fileName << "\n";
fallback.setSize(sf::Vector2f(40.f, 20.f));
fallback.setOrigin(20.f, 10.f);
fallback.setPosition(startPos);
fallback.setFillColor(sf::Color::Blue);
}
}
void Vehicle::update(float dt, float speed) {
float dx = 0.f;
float dy = 0.f;
// Decide direction of movement
switch (direction) {
case Direction::LeftToRight: dx = speed; break;
case Direction::RightToLeft: dx = -speed; break;
case Direction::TopToBottom: dy = speed; break;
case Direction::BottomToTop: dy = -speed; break;
}
dx *= dt;
dy *= dt;
if (textureLoaded) {
sprite.move(dx, dy);
} else {
fallback.move(dx, dy);
}
// Compute actual displacement from last frame.
sf::Vector2f currentPos = textureLoaded ? sprite.getPosition() : fallback.getPosition();
float displacement = std::sqrt(std::pow(currentPos.x - lastPosition.x, 2) +
std::pow(currentPos.y - lastPosition.y, 2));
const float epsilon = 0.1f;
// Use displacement to update stoppedTime
if (displacement < epsilon) {
stoppedTime += dt;
} else {
stoppedTime = 0.f;
}
// Update lastPosition.
lastPosition = currentPos;
}
void Vehicle::render(sf::RenderWindow& window) {
if (textureLoaded) {
window.draw(sprite);
} else {
window.draw(fallback);
}
}
VehicleType Vehicle::getType() const {
return type;
}
float Vehicle::getX() const {
// For sorting horizontally
if (textureLoaded) {
return sprite.getPosition().x;
} else {
return fallback.getPosition().x;
}
}
float Vehicle::getY() const {
// For sorting vertically
if (textureLoaded) {
return sprite.getPosition().y;
} else {
return fallback.getPosition().y;
}
}
Direction Vehicle::getDirection() const {
return direction;
}
bool Vehicle::hasPassedStopLine() const {
return passedStopLine;
}
void Vehicle::setPassedStopLine(bool val) {
passedStopLine = val;
}