160 lines
4.4 KiB
C++
160 lines
4.4 KiB
C++
#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 };
|
|
|
|
if (id == 1) {
|
|
chest.itemIds = { 101, 101 };
|
|
}
|
|
else if (id == 2) {
|
|
chest.itemIds = { 101, 201 };
|
|
}
|
|
else if (id == 3) {
|
|
chest.itemIds = { 102 };
|
|
}
|
|
else if (id == 4) {
|
|
chest.itemIds = { 102, 202 };
|
|
}
|
|
else if (id == 5) {
|
|
chest.itemIds = { 101, 102, 202 };
|
|
}
|
|
|
|
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;
|
|
|
|
std::string npcPath;
|
|
|
|
if (id == 1) {
|
|
npcPath = "data/npcs/npc_1.txt";
|
|
}
|
|
else if (id == 2) {
|
|
npcPath = "data/npcs/npc_2.txt";
|
|
}
|
|
else if (id == 3) {
|
|
npcPath = "data/npcs/npc_3.txt";
|
|
}
|
|
else {
|
|
npcPath = "data/npcs/npc_1.txt";
|
|
}
|
|
|
|
if (npc.loadFromFile(npcPath)) {
|
|
npc.position = { x, y };
|
|
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];
|
|
} |