0
0
mirror of https://github.com/Nimac0/SDL_Minigame synced 2026-01-12 07:53:43 +00:00

#58 Replaced map based game factory with simpler implementation

This commit is contained in:
Benedikt Galbavy 2024-06-24 16:03:46 +02:00
parent 58e2bb0d30
commit 3d639b89d0
3 changed files with 18 additions and 14 deletions

View File

@ -31,23 +31,20 @@ public:
return game;
}*/
void registerClass(const std::string& className, CreateFunc createFunc) {
this->creators[className] = createFunc;
void registerClass(CreateFunc createFunc) {
this->creatorFunc = createFunc;
}
Game* create(const std::string& className, GameInternal* gameInternal) {
auto it = this->creators.find(className);
if (it != creators.end()) {
Game* game = it->second();
game->gameInternal = gameInternal;
return game;
}
return nullptr;
Game* create(GameInternal* gameInternal) {
if (this->creatorFunc == nullptr)
return nullptr;
Game* game = (this->creatorFunc)();
game->gameInternal = gameInternal;
return game;
}
private:
CreateFunc creator;
std::map<std::string, CreateFunc> creators;
CreateFunc creatorFunc = nullptr;
};
/*

View File

@ -6,10 +6,17 @@ namespace vego {
template<typename T>
class GameRegistryHelper {
public:
[[deprecated("GameRegistryHelper() does not take a className anymore")]]
GameRegistryHelper(const std::string& className) {
static_assert(std::is_base_of<Game, T>::value, "Your class must inherit from Game");
GameFactory::instance().registerClass(
className,
[]() -> Game* { return new T; }
);
};
GameRegistryHelper() {
static_assert(std::is_base_of<Game, T>::value, "Your class must inherit from Game");
GameFactory::instance().registerClass(
[]() -> Game* { return new T; }
);
};

View File

@ -176,7 +176,7 @@ void GameInternal::init(const char* title, int xpos, int ypos, int width, int he
// player2.addComponent<StatEffectsComponent>();
// player2.addGroup((size_t) Entity::GroupLabel::PLAYERS);
this->gameInstance = GameFactory::instance().create("Chickengame", this); //!< \todo Should be specified via a config file
this->gameInstance = GameFactory::instance().create(this);
this->gameInstance->init();
}