Files
aee-arcade/source/game/entities/entity_factory.cpp
T

30 lines
876 B
C++

#include "game/entities/entity_factory.hpp"
#include <utility>
#include <vector>
#include "game/entities/entity.hpp" // tipus complet per a unique_ptr<Entity>
auto EntityFactory::instance() -> EntityFactory& {
static EntityFactory k_factory_;
return k_factory_;
}
void EntityFactory::registerCreator(const std::string& name, Creator creator) {
creators_[name] = std::move(creator);
}
auto EntityFactory::create(const std::string& name, const fkyaml::node& yaml) const
-> std::unique_ptr<Entity> {
auto it = creators_.find(name);
if (it == creators_.end()) { return nullptr; }
return it->second(yaml);
}
auto EntityFactory::registeredNames() const -> std::vector<std::string> {
std::vector<std::string> names;
names.reserve(creators_.size());
for (const auto& [name, _] : creators_) { names.push_back(name); }
return names;
}