Добавлена система сохранений

This commit is contained in:
DeLiss
2026-06-11 13:20:44 +05:00
parent 1ecf70937d
commit f641112a2d
7 changed files with 143 additions and 3 deletions

View File

@@ -129,6 +129,7 @@
<ClCompile Include="src\main.cpp" />
<ClCompile Include="src\npc.cpp" />
<ClCompile Include="src\player.cpp" />
<ClCompile Include="src\save_manager.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="include\game.h" />
@@ -137,6 +138,7 @@
<ClInclude Include="include\player.h" />
<ClInclude Include="include\location.h" />
<ClInclude Include="include\point.h" />
<ClInclude Include="include\save_manager.h" />
<ClInclude Include="item.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />

View File

@@ -33,6 +33,9 @@
<ClCompile Include="src\npc.cpp">
<Filter>Исходные файлы</Filter>
</ClCompile>
<ClCompile Include="src\save_manager.cpp">
<Filter>Исходные файлы</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="include\location.h">
@@ -56,5 +59,8 @@
<ClInclude Include="include\point.h">
<Filter>Файлы заголовков</Filter>
</ClInclude>
<ClInclude Include="include\save_manager.h">
<Filter>Файлы заголовков</Filter>
</ClInclude>
</ItemGroup>
</Project>

View File

@@ -9,6 +9,8 @@ struct InventorySlot {
class Inventory {
public:
std::vector<InventorySlot> 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<InventorySlot> items;
};

11
include/save_manager.h Normal file
View File

@@ -0,0 +1,11 @@
#pragma once
#include "player.h"
#include <string>
#include <vector>
class SaveManager {
public:
static bool saveGame(const Player& player, const std::string& path);
static bool loadGame(Player& player, const std::string& path);
};

7
saves/save.txt Normal file
View File

@@ -0,0 +1,7 @@
NAME=Zero
HP=100
LOCATION=1
X=5
Y=2
WEAPON=0
INVENTORY=101:2;

View File

@@ -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
View 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;
}