-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
134 lines (107 loc) · 4.3 KB
/
Copy pathmain.cpp
File metadata and controls
134 lines (107 loc) · 4.3 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
#include <SFML/Graphics.hpp>
#include <iostream>
#include "Car.h"
#include "Traffic.h"
#include "main.h"
using namespace std;
float getRandomNumber(int min, int max)
{
static const double fraction = 1.0 / (static_cast<double>(RAND_MAX) + 1.0);
return static_cast<float>(rand() * fraction * (max - min + 1) + min);
}
struct Tile {
sf::RectangleShape shape;
int x = 0;
int y = 0;
bool isRoad = false;
};
sf::Vector2u WINDOW_SIZE{ 1000, 800 };
constexpr unsigned TPS = 60;
const sf::Time timePerUpdate = sf::seconds(1.0f / float(TPS));
sf::Vector2f TILE_SIZE{ 16.f, 16.f };
const sf::Color GRASS_COLOR{ 124, 252, 0, 45 };
const sf::Color ROAD_COLOR{ 105, 105, 105, 45 };
constexpr int FRAMERATE = 240;
int main() {
sf::RenderWindow window{ sf::VideoMode{WINDOW_SIZE.x,WINDOW_SIZE.y},"" };
window.setFramerateLimit(FRAMERATE);
window.setPosition(sf::Vector2i{ window.getPosition().x, 0 });
window.setTitle("Traffic Simulator");
sf::View view = window.getDefaultView();
vector<vector<Tile>> doubleTileMap;
for (int y = 0; y < WINDOW_SIZE.y / TILE_SIZE.y; y++) {
for (int x = 0; x < WINDOW_SIZE.x / TILE_SIZE.x; x++) {
doubleTileMap.emplace_back();
Tile* tile = new Tile();
tile->x = x;
tile->y = y;
tile->shape.setSize({ TILE_SIZE.x, TILE_SIZE.y });
tile->shape.setFillColor(GRASS_COLOR);
/*tile->shape.setOutlineThickness(0.5f);
tile->shape.setOutlineColor({ sf::Color::White });*/
tile->shape.setPosition(x * TILE_SIZE.x, y * TILE_SIZE.y);
doubleTileMap[y].push_back(*tile);
}
}
sf::CircleShape car(10.f);
Car my_car(1, 500, 400, 0, 0, 0, 0);
my_car.set_throttle(0.1);
my_car.set_steering_angle(0.4);
while (window.isOpen()) {
sf::Vector2i mousePos = sf::Mouse::getPosition(window);
sf::Vector2f mouseWorldPos = window.mapPixelToCoords(mousePos, view);
sf::Event event{};
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed || event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape)
window.close();
else if (event.type == sf::Event::KeyPressed) {
switch (event.key.code) {
case sf::Keyboard::Enter: cout << "Enter Pressed\n"; break;
case sf::Keyboard::Space: cout << "Space Pressed\n"; break;
default: break;
}
}
else if (event.type == sf::Event::MouseButtonPressed) {
if (mouseWorldPos.x >= 0 && mouseWorldPos.y >= 0 && mouseWorldPos.x < WINDOW_SIZE.x && mouseWorldPos.y < WINDOW_SIZE.y) {
sf::Vector2i position = mousePos;
position.x /= static_cast<int>(TILE_SIZE.x);
position.y /= static_cast<int>(TILE_SIZE.y);
Tile tile = doubleTileMap[position.y][position.x];
switch (event.mouseButton.button) {
case sf::Mouse::Left: {
tile.shape.setFillColor(ROAD_COLOR);
tile.isRoad = true;
break;
}
case sf::Mouse::Right: {
tile.shape.setFillColor(GRASS_COLOR);
tile.isRoad = false;
break;
default: break;
}
}
}
}
}
my_car.update_pos();
float x = my_car.get_x();
float y = my_car.get_y();
float vx = my_car.get_vx();
float vy = my_car.get_vy();
car.setPosition(x, y);
// Log acceleration
cout << "Acceleration: " << my_car.get_ax() << ", " << my_car.get_ay() << endl;
// Log direction
cout << "Direction: " << my_car.get_direction() << endl;
window.clear();
for (vector<Tile>& index : doubleTileMap) {
for (Tile& tile : index) {
window.draw(tile.shape);
}
}
window.draw(car);
window.setView(window.getDefaultView());
window.display();
}
return EXIT_SUCCESS;
}