Files
UwU-Engine/Core/Game/WorldFactory.hpp
T

45 lines
1.4 KiB
C++

#pragma once
#include <vector>
#include <fstream>
#include <string>
#include "Game/SaveMap/SaveMap.hpp"
class GameInstance;
class World;
struct WorldFactory
{
const char* world_name;
std::shared_ptr<World>(*factory)(GameInstance&);
};
// Создаёт ассоцеативный список
// В нём соспоставляются имина уровней и фабрики их классов
#define WORLDS_LIST std::vector<WorldFactory> world_factories =
// Генерирует элемент списка
#define GENERATE_WORLD_FACTORY(Class, WorldName) WorldFactory{#WorldName,\
[](GameInstance& game_insance)->std::shared_ptr<World> {\
std::shared_ptr<Class> world = std::make_shared<Class>(game_insance);\
std::ifstream config_file("./Worlds/" #WorldName ".world");\
if (config_file.fail())\
throw std::runtime_error("Can't open world file");\
config_file.seekg(0, std::ios::end);\
size_t file_size = config_file.tellg();\
config_file.seekg(0, std::ios::beg);\
std::string data;\
data.resize(file_size);\
config_file.read(data.data(), file_size);\
world->load(std::make_shared<SaveMap>(data));\
return std::static_pointer_cast<World>(world);\
}},
/*
* Пример использования:
WORLDS_LIST {
GENERATE_WORLD_FACTORY(Sometime_class_world_One, WorldName_One)
GENERATE_WORLD_FACTORY(Sometime_class_world_Two, WorldName_Two)
};
*/