0
0
mirror of https://github.com/Nimac0/SDL_Minigame synced 2026-01-12 10:13:42 +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; return game;
}*/ }*/
void registerClass(const std::string& className, CreateFunc createFunc) { void registerClass(CreateFunc createFunc) {
this->creators[className] = createFunc; this->creatorFunc = createFunc;
} }
Game* create(const std::string& className, GameInternal* gameInternal) { Game* create(GameInternal* gameInternal) {
auto it = this->creators.find(className); if (this->creatorFunc == nullptr)
if (it != creators.end()) { return nullptr;
Game* game = it->second(); Game* game = (this->creatorFunc)();
game->gameInternal = gameInternal; game->gameInternal = gameInternal;
return game; return game;
} }
return nullptr;
}
private: private:
CreateFunc creator; CreateFunc creatorFunc = nullptr;
std::map<std::string, CreateFunc> creators;
}; };
/* /*

View File

@ -6,10 +6,17 @@ namespace vego {
template<typename T> template<typename T>
class GameRegistryHelper { class GameRegistryHelper {
public: public:
[[deprecated("GameRegistryHelper() does not take a className anymore")]]
GameRegistryHelper(const std::string& className) { GameRegistryHelper(const std::string& className) {
static_assert(std::is_base_of<Game, T>::value, "Your class must inherit from Game"); static_assert(std::is_base_of<Game, T>::value, "Your class must inherit from Game");
GameFactory::instance().registerClass( 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; } []() -> 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.addComponent<StatEffectsComponent>();
// player2.addGroup((size_t) Entity::GroupLabel::PLAYERS); // 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(); this->gameInstance->init();
} }