Добавлена система сохранений
This commit is contained in:
18
src/game.cpp
18
src/game.cpp
@@ -1,4 +1,5 @@
|
||||
#include "../include/game.h"
|
||||
#include "../include/save_manager.h"
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <fstream>
|
||||
@@ -71,7 +72,10 @@ void Game::showMainMenu() {
|
||||
break;
|
||||
}
|
||||
else if (str == "2") {
|
||||
std::cout << ">>> Load system is not ready yet.\n";
|
||||
if (SaveManager::loadGame(player, "saves/save.txt")) {
|
||||
std::cout << ">>> Game loaded.\n";
|
||||
break;
|
||||
}
|
||||
}
|
||||
else if (str == "3") {
|
||||
isRunning = false;
|
||||
@@ -101,6 +105,8 @@ void Game::render() {
|
||||
std::cout << "inventory - show inventory\n";
|
||||
std::cout << "use <id> - use potion\n";
|
||||
std::cout << "equip <id> - equip weapon\n";
|
||||
std::cout << "save - save game\n";
|
||||
std::cout << "load - load game\n";
|
||||
}
|
||||
|
||||
void Game::processCommand() {
|
||||
@@ -140,6 +146,16 @@ void Game::processCommand() {
|
||||
std::cin >> itemId;
|
||||
player.equipWeapon(itemId, itemDatabase);
|
||||
}
|
||||
else if (command == "save") {
|
||||
if (SaveManager::saveGame(player, "saves/save.txt")) {
|
||||
std::cout << "Game saved.\n";
|
||||
}
|
||||
}
|
||||
else if (command == "load") {
|
||||
if (SaveManager::loadGame(player, "saves/save.txt")) {
|
||||
std::cout << "Game loaded.\n";
|
||||
}
|
||||
}
|
||||
else {
|
||||
std::cout << "Unknown command.\n";
|
||||
}
|
||||
|
||||
97
src/save_manager.cpp
Normal file
97
src/save_manager.cpp
Normal file
@@ -0,0 +1,97 @@
|
||||
#include "../include/save_manager.h"
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
|
||||
bool SaveManager::saveGame(const Player& player, const std::string& path) {
|
||||
std::ofstream file(path);
|
||||
|
||||
if (!file.is_open()) {
|
||||
std::cout << "Cannot create save file.\n";
|
||||
return false;
|
||||
}
|
||||
|
||||
file << "NAME=" << player.name << "\n";
|
||||
file << "HP=" << player.hp << "\n";
|
||||
file << "LOCATION=" << player.currentLocation << "\n";
|
||||
file << "X=" << player.position.x << "\n";
|
||||
file << "Y=" << player.position.y << "\n";
|
||||
file << "WEAPON=" << player.equippedWeaponId << "\n";
|
||||
|
||||
file << "INVENTORY=";
|
||||
|
||||
for (const auto& slot : player.inventory.items) {
|
||||
file << slot.itemId << ":" << slot.count << ";";
|
||||
}
|
||||
|
||||
file << "\n";
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool SaveManager::loadGame(Player& player, const std::string& path) {
|
||||
std::ifstream file(path);
|
||||
|
||||
if (!file.is_open()) {
|
||||
std::cout << "Save file not found.\n";
|
||||
return false;
|
||||
}
|
||||
|
||||
player.inventory.items.clear();
|
||||
|
||||
std::string line;
|
||||
|
||||
while (std::getline(file, line)) {
|
||||
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 == "NAME") {
|
||||
player.name = value;
|
||||
}
|
||||
else if (key == "HP") {
|
||||
player.hp = std::stoi(value);
|
||||
}
|
||||
else if (key == "LOCATION") {
|
||||
player.currentLocation = std::stoi(value);
|
||||
}
|
||||
else if (key == "X") {
|
||||
player.position.x = std::stoi(value);
|
||||
}
|
||||
else if (key == "Y") {
|
||||
player.position.y = std::stoi(value);
|
||||
}
|
||||
else if (key == "WEAPON") {
|
||||
player.equippedWeaponId = std::stoi(value);
|
||||
}
|
||||
else if (key == "INVENTORY") {
|
||||
size_t start = 0;
|
||||
|
||||
while (start < value.size()) {
|
||||
size_t end = value.find(';', start);
|
||||
|
||||
if (end == std::string::npos) {
|
||||
break;
|
||||
}
|
||||
|
||||
std::string itemData = value.substr(start, end - start);
|
||||
size_t colon = itemData.find(':');
|
||||
|
||||
if (colon != std::string::npos) {
|
||||
int itemId = std::stoi(itemData.substr(0, colon));
|
||||
int count = std::stoi(itemData.substr(colon + 1));
|
||||
|
||||
player.inventory.addItem(itemId, count);
|
||||
}
|
||||
|
||||
start = end + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
Reference in New Issue
Block a user