-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsampleCity.cpp
More file actions
344 lines (290 loc) · 11.8 KB
/
sampleCity.cpp
File metadata and controls
344 lines (290 loc) · 11.8 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
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#include "sampleCity.h"
#include <iostream>
#include "travelFunc.h"
#include <math.h>
std::map<std::string, sf::Texture> buildingTextures;
void loadBuildingTextures() {
std::vector<std::pair<std::string, std::string>> files = {
{"Park", "images/park.png"},
{"School", "images/school.png"},
{"Office", "images/office.png"},
{"Apartment", "images/apartment.png"},
{"Market", "images/market.png"},
{"Hospital", "images/hospital.png"}
};
for (auto &[type, path] : files) {
sf::Texture tex;
if (!tex.loadFromFile(path))
std::cerr << "Failed to load " << path << "\n";
else
buildingTextures[type] = tex;
}
}
SampleCity::SampleCity() {
// Load font
if (!font.loadFromFile("arial.ttf")) {
std::cerr << "Failed to load font!\n";
}
// Close button
closeButton.setSize(sf::Vector2f(40, 40));
closeButton.setPosition(10, 10);
closeButton.setFillColor(sf::Color(200, 50, 50));
closeText.setFont(font);
closeText.setString("X");
closeText.setCharacterSize(28);
closeText.setFillColor(sf::Color::White);
closeText.setPosition(20, 12);
// Travel button initialization
const float btnWidth = 120.f, btnHeight = 40.f;
const float travelBtnX = 100.f;
const float travelBtnY = 650.f;
travelButton.setSize(sf::Vector2f(btnWidth, btnHeight));
travelButton.setPosition(travelBtnX, travelBtnY);
travelButton.setFillColor(sf::Color(70, 130, 180));
travelButton.setOutlineThickness(2);
travelButton.setOutlineColor(sf::Color::Black);
travelText.setFont(font);
travelText.setString("Travel");
travelText.setCharacterSize(18);
travelText.setFillColor(sf::Color::White);
travelText.setPosition(travelBtnX + 28, travelBtnY + 8);
// ==== ORGANIZED CITY MAP (OPTION A) ====
std::vector<std::string> types = {
"Park", "School", "Market", "Office", "Hospital", "Apartment"
};
locations.clear();
adjacencyList.clear();
auto addLocation = [&](std::string name, std::string type, int x, int y) {
sampleLocation loc;
loc.name = name;
loc.type = type;
loc.x = x;
loc.y = y;
loc.sprite.setTexture(buildingTextures[type]);
loc.sprite.setPosition(x - 16, y - 16);
locations.push_back(loc);
};
// ------- DISTRICT 1: PARKS (LEFT) -------
addLocation("Greenwood Park", "Park", 150, 150);
addLocation("Maple Garden", "Park", 150, 260);
addLocation("Playground Park", "Park", 150, 370);
addLocation("Sunset Park", "Park", 150, 480);
addLocation("Rose Park", "Park", 150, 590);
// ------- DISTRICT 2: SCHOOLS (TOP-LEFT) -------
addLocation("City School A", "School", 350, 120);
addLocation("City School B", "School", 450, 120);
addLocation("Bright Future School", "School", 550, 120);
addLocation("Educators Academy", "School", 650, 120);
addLocation("Beaconhouse", "School", 750, 120);
// ------- DISTRICT 3: MARKETS (CENTER) -------
addLocation("Central Market", "Market", 450, 300);
addLocation("Fresh Store", "Market", 550, 300);
addLocation("Daily Mart", "Market", 650, 300);
addLocation("Super Grocery", "Market", 750, 300);
addLocation("Trade Center", "Market", 850, 300);
// ------- DISTRICT 4: OFFICES (CENTER-RIGHT) -------
addLocation("Tech Hub", "Office", 950, 350);
addLocation("Finance Tower", "Office", 950, 450);
addLocation("Business Plaza", "Office", 950, 550);
addLocation("Corporate Center", "Office", 950, 650);
// ------- DISTRICT 5: HOSPITALS (TOP-RIGHT) -------
addLocation("General Hospital", "Hospital", 1100, 150);
addLocation("City Emergency", "Hospital", 1100, 250);
addLocation("Heart Care Center", "Hospital", 1100, 350);
addLocation("Children Hospital", "Hospital", 1100, 450);
addLocation("Medical Diagnostics", "Hospital", 1100, 550);
// ------- DISTRICT 6: APARTMENTS (BOTTOM AREA) -------
addLocation("Skyline Apartments", "Apartment", 500, 400);
addLocation("Sunrise Apartments", "Apartment", 600, 400);
addLocation("Lifestyle Residency", "Apartment", 700, 400);
addLocation("Green Villas", "Apartment", 800, 400);
addLocation("Golden Heights", "Apartment", 900, 400);
// ------- Add more apartments to reach 50 -------
addLocation("City Homes A", "Apartment", 300, 500);
addLocation("City Homes B", "Apartment", 400, 500);
addLocation("City Homes C", "Apartment", 500, 500);
addLocation("City Homes D", "Apartment", 600, 500);
addLocation("City Homes E", "Apartment", 700, 500);
addLocation("Elite Residency A", "Apartment", 300, 600);
addLocation("Elite Residency B", "Apartment", 400, 600);
addLocation("Elite Residency C", "Apartment", 500, 600);
addLocation("Elite Residency D", "Apartment", 600, 600);
addLocation("Elite Residency E", "Apartment", 700, 600);
for (int i = 0; i < locations.size(); i++) {
for (int j = 0; j < locations.size(); j++) {
if (i == j) continue;
float dx = locations[i].x - locations[j].x;
float dy = locations[i].y - locations[j].y;
float dist = sqrt(dx*dx + dy*dy);
if (dist < 220) // threshold for "road"
locations[i].neighbors.push_back(j);
}
}
for (int i = 0; i < locations.size(); i++) {
adjacencyList[i] = locations[i].neighbors;
}
}
void SampleCity::handleEvent(sf::Event& event, bool& returnToMenu) {
if (event.type == sf::Event::MouseButtonPressed) {
sf::Vector2f mouse(event.mouseButton.x, event.mouseButton.y);
// Close button
if (closeButton.getGlobalBounds().contains(mouse)) {
returnToMenu = true;
}
// Travel button
else if (travelButton.getGlobalBounds().contains(mouse)) {
showTravelPopup = true;
showErrorPopup = false;
typingStart = typingEnd = false;
}
else if (showTravelPopup) {
sf::Vector2f popupSize(400.f, 200.f);
sf::Vector2f popupPos(400.f, 300.f);
// Define rectangles
sf::FloatRect startBoxRect(popupPos.x + 130, popupPos.y + 25, 200.f, 30.f);
sf::FloatRect endBoxRect(popupPos.x + 130, popupPos.y + 75, 200.f, 30.f);
sf::FloatRect submitBtnRect(popupPos.x + popupSize.x/2.f - 50.f, popupPos.y + 140, 100.f, 34.f);
if (startBoxRect.contains(mouse)) {
typingStart = true;
typingEnd = false;
}
else if (endBoxRect.contains(mouse)) {
typingStart = false;
typingEnd = true;
}
else if (submitBtnRect.contains(mouse)) {
// Validate input
if (startPoint.empty() || endPoint.empty() ||
startPoint == endPoint ||
findLocationByName(startPoint) == -1 ||
findLocationByName(endPoint) == -1) {
showErrorPopup = true;
} else {
std::cout << "Finding route: " << startPoint << " -> " << endPoint << "\n";
showTravelPopup = false;
}
}
}
// Error popup close button
else if (showErrorPopup) {
sf::Vector2f errorSize(300.f, 120.f);
sf::Vector2f errorPos(350.f, 300.f);
sf::FloatRect closeBtnRect(errorPos.x + errorSize.x/2 - 40.f, errorPos.y + 70.f, 80.f, 30.f);
if (closeBtnRect.contains(mouse)) {
showErrorPopup = false;
}
}
}
if (showTravelPopup && event.type == sf::Event::TextEntered) {
if (typingStart) {
if (event.text.unicode == '\b') { // Backspace
if (!startPoint.empty()) startPoint.pop_back();
}
else if (event.text.unicode < 128) {
startPoint += static_cast<char>(event.text.unicode);
}
}
else if (typingEnd) {
if (event.text.unicode == '\b') { // Backspace
if (!endPoint.empty()) endPoint.pop_back();
}
else if (event.text.unicode < 128) {
endPoint += static_cast<char>(event.text.unicode);
}
}
}
// Escape key to close popups
if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape) {
if (showTravelPopup) showTravelPopup = false;
if (showErrorPopup) showErrorPopup = false;
typingStart = typingEnd = false;
}
}
void SampleCity::update(sf::RenderWindow& window) {
sf::Vector2i mousePos = sf::Mouse::getPosition(window);
hoveredLocation = -1;
for (size_t i = 0; i < locations.size(); i++) {
sf::FloatRect bounds = locations[i].sprite.getGlobalBounds();
if (bounds.contains((sf::Vector2f)mousePos)) {
hoveredLocation = i;
break;
}
}
}
void SampleCity::draw(sf::RenderWindow& window) {
window.clear(sf::Color(170, 255, 170)); // light green background
// Draw roads
for (size_t i = 0; i < locations.size(); i++) {
for (int neighbor : locations[i].neighbors) {
sf::Vertex line[] = {
sf::Vertex(sf::Vector2f(locations[i].x, locations[i].y), sf::Color::Black),
sf::Vertex(sf::Vector2f(locations[neighbor].x, locations[neighbor].y), sf::Color::Black)
};
window.draw(line, 2, sf::Lines);
}
}
// Draw locations
for (auto &loc : locations) {
window.draw(loc.sprite);
}
// Draw close button
window.draw(closeButton);
window.draw(closeText);
if (!showTravelPopup && !showErrorPopup) {
window.draw(travelButton);
window.draw(travelText);
}
if (hoveredLocation != -1) {
sf::Text label;
label.setFont(font);
label.setString(locations[hoveredLocation].name);
label.setCharacterSize(10);
label.setFillColor(sf::Color::Black);
float offsetX = -20;
float offsetY = -30;
sf::FloatRect bounds = label.getLocalBounds();
label.setPosition(
locations[hoveredLocation].x + offsetX - bounds.width,
locations[hoveredLocation].y + offsetY
);
// Create background box
sf::RectangleShape bg;
bg.setSize(sf::Vector2f(bounds.width + 10, bounds.height + 10));
bg.setFillColor(sf::Color(255, 255, 255, 220));
bg.setPosition(label.getPosition().x - 5, label.getPosition().y - 5);
bg.setOutlineColor(sf::Color::Black);
bg.setOutlineThickness(1);
window.draw(bg);
window.draw(label);
}
Graph graph;
// Add all locations as nodes to the graph
for (size_t i = 0; i < locations.size(); i++) {
const auto& loc = locations[i];
Location graphLoc;
graphLoc.id = i;
graphLoc.name = loc.name;
graphLoc.type = loc.type;
graphLoc.pos = sf::Vector2f(loc.x, loc.y);
graphLoc.active = true;
graph.addNode(graphLoc);
}
// Add edges to the graph based on neighbors
for (size_t i = 0; i < locations.size(); i++) {
for (int neighbor : locations[i].neighbors) {
graph.addEdge(i, neighbor);
}
}
travel(window, graph, showTravelPopup, showErrorPopup,
startPoint, endPoint, typingStart, typingEnd,
currentPath, showPath, window.getDefaultView());
}
const std::vector<int>& SampleCity::getNeighbors(int index) const {
return adjacencyList.at(index);
}
int SampleCity::findLocationByName(const std::string& name) const {
for (size_t i = 0; i < locations.size(); i++) {
if (locations[i].name == name) return i;
}
return -1; // not found
}