-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
131 lines (106 loc) · 4.39 KB
/
Copy pathmain.cpp
File metadata and controls
131 lines (106 loc) · 4.39 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
#include <SFML/Graphics.hpp>
#include <thread>
#include <iostream>
#include <optional>
#include "world.h"
void worker();
int main() {
scanMapDirectory();
std::cout << "Loaded " << map_filenames.size() << " maps." << std::endl;
resetWorld();
sf::RenderWindow window(sf::VideoMode({1050, 600}), "MAPF");
window.setFramerateLimit(60);
sf::Font font;
font.openFromFile("arial.ttf");
std::thread t{worker};
t.detach();
sf::Clock clock;
while (window.isOpen()) {
float dt = clock.restart().asSeconds();
while (const std::optional event = window.pollEvent()) {
if (event->is<sf::Event::Closed>()) window.close();
if (const auto* mB = event->getIf<sf::Event::MouseButtonPressed>()) {
sf::Vector2f mPos = {(float)mB->position.x, (float)mB->position.y};
for (int i = 0; i < (int)map_filenames.size(); i++) {
if (sf::FloatRect({810.f, 60.f + i * 40.f}, {230.f, 30.f}).contains(mPos)) {
selected_map_idx = i;
resetWorld();
}
}
for (int i = 0; i < (int)algo_names.size(); i++) {
if (sf::FloatRect({810.f, 400.f + i * 50.f}, {230.f, 40.f}).contains(mPos)) {
current_algo = (Algorithm)i;
simulationStarted = true;
}
}
}
}
window.clear(sf::Color(30, 30, 30));
for (int y = 0; y < (int)grid.size(); y++) {
for (int x = 0; x < (int)grid[y].size(); x++) {
sf::RectangleShape tile({38.f, 38.f});
tile.setPosition({x * 40.f, y * 40.f});
tile.setFillColor(grid[y][x] == '#' ? sf::Color(60, 60, 60) : sf::Color(40, 40, 40));
window.draw(tile);
}
}
for (auto& b : deliveryPoints) {
sf::RectangleShape r({34.f, 34.f});
r.setPosition({b.pos.x * 40.f + 3.f, b.pos.y * 40.f + 3.f});
if (b.ownerId < (int)robots.size()) {
sf::Color c = robots[b.ownerId].color;
r.setOutlineColor(c);
r.setOutlineThickness(2);
r.setFillColor(sf::Color(c.r, c.g, c.b, 60));
}
window.draw(r);
}
for (auto &r : robots) {
sf::Vector2f targetPos({(float)r.gridPos.x * 40.f + 20.f, (float)r.gridPos.y * 40.f + 20.f});
r.realPos += (targetPos - r.realPos) * dt * 10.0f;
sf::CircleShape rs(14.f);
rs.setOrigin({14.f, 14.f});
rs.setPosition(r.realPos);
rs.setFillColor(r.color);
window.draw(rs);
}
for (auto &o : objects) {
if (o.delivered) continue;
sf::RectangleShape box({18.f, 18.f});
box.setOrigin({9.f, 9.f});
if (o.carrierId != -1) box.setPosition(robots[o.carrierId].realPos);
else box.setPosition({o.pos.x * 40.f + 20.f, o.pos.y * 40.f + 20.f});
box.setFillColor(sf::Color::Yellow);
window.draw(box);
}
sf::RectangleShape mBg({250.f, 600.f});
mBg.setPosition({800.f, 0.f});
mBg.setFillColor(sf::Color(50, 50, 50));
window.draw(mBg);
sf::Text uiText(font);
uiText.setCharacterSize(18);
uiText.setString("SELECT MAP:");
uiText.setPosition({810.f, 20.f});
uiText.setFillColor(sf::Color::Yellow);
window.draw(uiText);
for (int i = 0; i < (int)map_filenames.size(); i++) {
std::string name = map_filenames[i];
uiText.setString(name.substr(name.find_last_of("/\\") + 1));
uiText.setPosition({810.f, 60.f + i * 40.f});
uiText.setFillColor(i == selected_map_idx ? sf::Color::Green : sf::Color::White);
window.draw(uiText);
}
uiText.setString("ALGORITHMS:");
uiText.setPosition({810.f, 360.f});
uiText.setFillColor(sf::Color::Yellow);
window.draw(uiText);
for (int i = 0; i < (int)algo_names.size(); i++) {
uiText.setString(algo_names[i]);
uiText.setPosition({810.f, 400.f + i * 50.f});
uiText.setFillColor((int)current_algo == i && simulationStarted ? sf::Color::Green : sf::Color::White);
window.draw(uiText);
}
window.display();
}
return 0;
}