From f641112a2d540488d196874f5696308c46532948 Mon Sep 17 00:00:00 2001 From: DeLiss <96916811+deliss-btw@users.noreply.github.com> Date: Thu, 11 Jun 2026 13:20:44 +0500 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B0=20=D1=81=D0=B8=D1=81=D1=82=D0=B5=D0=BC=D0=B0=20?= =?UTF-8?q?=D1=81=D0=BE=D1=85=D1=80=D0=B0=D0=BD=D0=B5=D0=BD=D0=B8=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ASCII-G.vcxproj | 2 + ASCII-G.vcxproj.filters | 6 +++ include/inventory.h | 5 ++- include/save_manager.h | 11 +++++ saves/save.txt | 7 +++ src/game.cpp | 18 +++++++- src/save_manager.cpp | 97 +++++++++++++++++++++++++++++++++++++++++ 7 files changed, 143 insertions(+), 3 deletions(-) create mode 100644 include/save_manager.h create mode 100644 saves/save.txt create mode 100644 src/save_manager.cpp diff --git a/ASCII-G.vcxproj b/ASCII-G.vcxproj index 291e33f..8373c2e 100644 --- a/ASCII-G.vcxproj +++ b/ASCII-G.vcxproj @@ -129,6 +129,7 @@ + @@ -137,6 +138,7 @@ + diff --git a/ASCII-G.vcxproj.filters b/ASCII-G.vcxproj.filters index d24560e..dbb67a9 100644 --- a/ASCII-G.vcxproj.filters +++ b/ASCII-G.vcxproj.filters @@ -33,6 +33,9 @@ Исходные файлы + + Исходные файлы + @@ -56,5 +59,8 @@ Файлы заголовков + + Файлы заголовков + \ No newline at end of file diff --git a/include/inventory.h b/include/inventory.h index bb54dbd..9a2cb88 100644 --- a/include/inventory.h +++ b/include/inventory.h @@ -9,6 +9,8 @@ struct InventorySlot { class Inventory { public: + std::vector items; + void addItem(int itemId, int count = 1); bool removeItem(int itemId, int count = 1); @@ -16,6 +18,5 @@ public: bool hasItem(int itemId) const; -private: - std::vector items; + }; \ No newline at end of file diff --git a/include/save_manager.h b/include/save_manager.h new file mode 100644 index 0000000..c0fb4b3 --- /dev/null +++ b/include/save_manager.h @@ -0,0 +1,11 @@ +#pragma once + +#include "player.h" +#include +#include + +class SaveManager { +public: + static bool saveGame(const Player& player, const std::string& path); + static bool loadGame(Player& player, const std::string& path); +}; \ No newline at end of file diff --git a/saves/save.txt b/saves/save.txt new file mode 100644 index 0000000..8e25275 --- /dev/null +++ b/saves/save.txt @@ -0,0 +1,7 @@ +NAME=Zero +HP=100 +LOCATION=1 +X=5 +Y=2 +WEAPON=0 +INVENTORY=101:2; diff --git a/src/game.cpp b/src/game.cpp index 0317fae..f931118 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -1,4 +1,5 @@ #include "../include/game.h" +#include "../include/save_manager.h" #include #include #include @@ -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 - use potion\n"; std::cout << "equip - 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"; } diff --git a/src/save_manager.cpp b/src/save_manager.cpp new file mode 100644 index 0000000..27b9c1d --- /dev/null +++ b/src/save_manager.cpp @@ -0,0 +1,97 @@ +#include "../include/save_manager.h" +#include +#include + +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; +} \ No newline at end of file