Добавлены локации и перемещение по ним
This commit is contained in:
127
src/location.cpp
Normal file
127
src/location.cpp
Normal file
@@ -0,0 +1,127 @@
|
||||
#include "../include/location.h"
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
|
||||
bool Location::loadFromFile(const std::string& path) {
|
||||
std::ifstream file(path);
|
||||
|
||||
if (!file.is_open()) {
|
||||
std::cout << "Cannot open location file: " << path << "\n";
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string line;
|
||||
bool readingMap = false;
|
||||
|
||||
while (std::getline(file, line)) {
|
||||
if (line.empty()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (line == "MAP_BEGIN") {
|
||||
readingMap = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (line == "MAP_END") {
|
||||
readingMap = false;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (readingMap) {
|
||||
map.push_back(line);
|
||||
continue;
|
||||
}
|
||||
|
||||
size_t pos = line.find('=');
|
||||
if (pos == std::string::npos) {
|
||||
continue;
|
||||
}
|
||||
|
||||
std::string key = line.substr(0, pos);
|
||||
std::string value = line.substr(pos + 1);
|
||||
|
||||
if (key == "ID") id = std::stoi(value);
|
||||
else if (key == "TITLE") title = value;
|
||||
else if (key == "WIDTH") width = std::stoi(value);
|
||||
else if (key == "HEIGHT") height = std::stoi(value);
|
||||
else if (key == "START_X") startPosition.x = std::stoi(value);
|
||||
else if (key == "START_Y") startPosition.y = std::stoi(value);
|
||||
else if (key == "NORTH") north = std::stoi(value);
|
||||
else if (key == "SOUTH") south = std::stoi(value);
|
||||
else if (key == "WEST") west = std::stoi(value);
|
||||
else if (key == "EAST") east = std::stoi(value);
|
||||
else if (key == "RIDDLE_TYPE") riddle.type = std::stoi(value);
|
||||
else if (key == "RIDDLE_TEXT") riddle.text = value;
|
||||
else if (key == "RIDDLE_ANSWER") riddle.answer = value;
|
||||
}
|
||||
|
||||
for (int y = 0; y < map.size(); y++) {
|
||||
for (int x = 0; x < map[y].size(); x++) {
|
||||
char tile = map[y][x];
|
||||
|
||||
if (tile == 'C') {
|
||||
Chest chest;
|
||||
chest.pos = { x, y };
|
||||
chest.gold = 25;
|
||||
chests.push_back(chest);
|
||||
}
|
||||
else if (tile == 'E') {
|
||||
Enemy enemy;
|
||||
enemy.pos = { x, y };
|
||||
enemy.type = "Goblin";
|
||||
enemy.hp = 50;
|
||||
enemies.push_back(enemy);
|
||||
}
|
||||
else if (tile == 'N') {
|
||||
NPC npc;
|
||||
npc.pos = { x, y };
|
||||
npc.name = "Stranger";
|
||||
npcs.push_back(npc);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void Location::draw(const Point& playerPosition) const {
|
||||
std::cout << "\n=== " << title << " ===\n";
|
||||
|
||||
for (int y = 0; y < map.size(); y++) {
|
||||
for (int x = 0; x < map[y].size(); x++) {
|
||||
if (playerPosition.x == x && playerPosition.y == y) {
|
||||
std::cout << '@';
|
||||
}
|
||||
else {
|
||||
char tile = map[y][x];
|
||||
|
||||
if (tile == '@') {
|
||||
std::cout << '.';
|
||||
}
|
||||
else {
|
||||
std::cout << tile;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::cout << "\n";
|
||||
}
|
||||
}
|
||||
|
||||
bool Location::isWalkable(int x, int y) const {
|
||||
if (y < 0 || y >= map.size()) return false;
|
||||
if (x < 0 || x >= map[y].size()) return false;
|
||||
|
||||
char tile = map[y][x];
|
||||
|
||||
return tile != '#';
|
||||
}
|
||||
|
||||
char Location::getTile(int x, int y) const {
|
||||
if (y < 0 || y >= map.size()) return '#';
|
||||
if (x < 0 || x >= map[y].size()) return '#';
|
||||
|
||||
return map[y][x];
|
||||
}
|
||||
Reference in New Issue
Block a user