Добавлена система сохранений
This commit is contained in:
@@ -129,6 +129,7 @@
|
|||||||
<ClCompile Include="src\main.cpp" />
|
<ClCompile Include="src\main.cpp" />
|
||||||
<ClCompile Include="src\npc.cpp" />
|
<ClCompile Include="src\npc.cpp" />
|
||||||
<ClCompile Include="src\player.cpp" />
|
<ClCompile Include="src\player.cpp" />
|
||||||
|
<ClCompile Include="src\save_manager.cpp" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClInclude Include="include\game.h" />
|
<ClInclude Include="include\game.h" />
|
||||||
@@ -137,6 +138,7 @@
|
|||||||
<ClInclude Include="include\player.h" />
|
<ClInclude Include="include\player.h" />
|
||||||
<ClInclude Include="include\location.h" />
|
<ClInclude Include="include\location.h" />
|
||||||
<ClInclude Include="include\point.h" />
|
<ClInclude Include="include\point.h" />
|
||||||
|
<ClInclude Include="include\save_manager.h" />
|
||||||
<ClInclude Include="item.h" />
|
<ClInclude Include="item.h" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||||
|
|||||||
@@ -33,6 +33,9 @@
|
|||||||
<ClCompile Include="src\npc.cpp">
|
<ClCompile Include="src\npc.cpp">
|
||||||
<Filter>Исходные файлы</Filter>
|
<Filter>Исходные файлы</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
|
<ClCompile Include="src\save_manager.cpp">
|
||||||
|
<Filter>Исходные файлы</Filter>
|
||||||
|
</ClCompile>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClInclude Include="include\location.h">
|
<ClInclude Include="include\location.h">
|
||||||
@@ -56,5 +59,8 @@
|
|||||||
<ClInclude Include="include\point.h">
|
<ClInclude Include="include\point.h">
|
||||||
<Filter>Файлы заголовков</Filter>
|
<Filter>Файлы заголовков</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
|
<ClInclude Include="include\save_manager.h">
|
||||||
|
<Filter>Файлы заголовков</Filter>
|
||||||
|
</ClInclude>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
||||||
@@ -9,6 +9,8 @@ struct InventorySlot {
|
|||||||
|
|
||||||
class Inventory {
|
class Inventory {
|
||||||
public:
|
public:
|
||||||
|
std::vector<InventorySlot> items;
|
||||||
|
|
||||||
void addItem(int itemId, int count = 1);
|
void addItem(int itemId, int count = 1);
|
||||||
bool removeItem(int itemId, int count = 1);
|
bool removeItem(int itemId, int count = 1);
|
||||||
|
|
||||||
@@ -16,6 +18,5 @@ public:
|
|||||||
|
|
||||||
bool hasItem(int itemId) const;
|
bool hasItem(int itemId) const;
|
||||||
|
|
||||||
private:
|
|
||||||
std::vector<InventorySlot> items;
|
|
||||||
};
|
};
|
||||||
11
include/save_manager.h
Normal file
11
include/save_manager.h
Normal 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
7
saves/save.txt
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
NAME=Zero
|
||||||
|
HP=100
|
||||||
|
LOCATION=1
|
||||||
|
X=5
|
||||||
|
Y=2
|
||||||
|
WEAPON=0
|
||||||
|
INVENTORY=101:2;
|
||||||
18
src/game.cpp
18
src/game.cpp
@@ -1,4 +1,5 @@
|
|||||||
#include "../include/game.h"
|
#include "../include/game.h"
|
||||||
|
#include "../include/save_manager.h"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
@@ -71,7 +72,10 @@ void Game::showMainMenu() {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
else if (str == "2") {
|
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") {
|
else if (str == "3") {
|
||||||
isRunning = false;
|
isRunning = false;
|
||||||
@@ -101,6 +105,8 @@ void Game::render() {
|
|||||||
std::cout << "inventory - show inventory\n";
|
std::cout << "inventory - show inventory\n";
|
||||||
std::cout << "use <id> - use potion\n";
|
std::cout << "use <id> - use potion\n";
|
||||||
std::cout << "equip <id> - equip weapon\n";
|
std::cout << "equip <id> - equip weapon\n";
|
||||||
|
std::cout << "save - save game\n";
|
||||||
|
std::cout << "load - load game\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
void Game::processCommand() {
|
void Game::processCommand() {
|
||||||
@@ -140,6 +146,16 @@ void Game::processCommand() {
|
|||||||
std::cin >> itemId;
|
std::cin >> itemId;
|
||||||
player.equipWeapon(itemId, itemDatabase);
|
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 {
|
else {
|
||||||
std::cout << "Unknown command.\n";
|
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