Move load config for world to factory

This commit is contained in:
Jiga228
2025-10-07 20:12:10 +07:00
parent bd8b0aec96
commit bb48fb6dfb
2 changed files with 14 additions and 9 deletions
-8
View File
@@ -19,13 +19,6 @@ GameInstance::GameInstance(CoreInstance& core) : core(core)
// Init directories // Init directories
std::filesystem::create_directories(std::filesystem::path("./Resources")); std::filesystem::create_directories(std::filesystem::path("./Resources"));
std::ifstream file("./Worlds/" + base_world + ".world");
if (file.fail())
throw std::runtime_error("Can't open world file");
std::string data;
std::getline(file, data);
for (auto& factories : world_factories) for (auto& factories : world_factories)
{ {
if (factories.world_name == base_world) if (factories.world_name == base_world)
@@ -38,7 +31,6 @@ GameInstance::GameInstance(CoreInstance& core) : core(core)
if (world == nullptr) if (world == nullptr)
throw std::runtime_error("Can't find world"); throw std::runtime_error("Can't find world");
world->load(std::make_shared<SaveMap>(data));
} }
GameInstance::~GameInstance() GameInstance::~GameInstance()
+14 -1
View File
@@ -1,6 +1,9 @@
#pragma once #pragma once
#include <vector> #include <vector>
#include <fstream>
#include <string>
#include "Game/SaveMap/SaveMap.h"
class GameInstance; class GameInstance;
class World; class World;
@@ -16,7 +19,17 @@ struct WorldFactory
#define WORLDS_LIST std::vector<WorldFactory> world_factories = #define WORLDS_LIST std::vector<WorldFactory> world_factories =
// Генерирует элемент списка // Генерирует элемент списка
#define GENERATE_WORLD_FACTORY(Class, WorldName) WorldFactory{#WorldName, [](GameInstance& game_insance)->World* { return new Class(game_insance); }}, #define GENERATE_WORLD_FACTORY(Class, WorldName) WorldFactory{#WorldName,\
[](GameInstance& game_insance)->World* {\
Class* world = new Class(game_insance);\
std::ifstream config_file("./Worlds/" #WorldName ".world");\
if (config_file.fail())\
throw std::runtime_error("Can't open world file");\
std::string data;\
std::getline(config_file, data);\
world->load(std::make_shared<SaveMap>(data));\
return world;\
}},
/* /*
* Пример использования: * Пример использования: