57 lines
1.8 KiB
C++
57 lines
1.8 KiB
C++
// bullet_registry.cpp - Implementació del registre de bales
|
|
// © 2026 JailDesigner
|
|
|
|
#include "game/entities/bullet_registry.hpp"
|
|
|
|
#include <cstdlib>
|
|
#include <iostream>
|
|
|
|
#include "core/entities/entity_loader.hpp"
|
|
|
|
std::unordered_map<std::string, BulletConfig> BulletRegistry::configs;
|
|
bool BulletRegistry::loaded = false;
|
|
|
|
namespace {
|
|
// Tria comú: carrega el YAML d'un name, parseja a BulletConfig i el guarda
|
|
// al map. Retorna punter al config inserit, o nullptr si falla.
|
|
auto loadInto(std::unordered_map<std::string, BulletConfig>& configs, const std::string& name) -> const BulletConfig* {
|
|
auto yaml = Entities::EntityLoader::load(name);
|
|
if (!yaml) {
|
|
std::cerr << "[BulletRegistry] Error: no s'ha pogut carregar " << name << ".yaml\n";
|
|
return nullptr;
|
|
}
|
|
auto cfg = BulletConfig::fromYaml(*yaml);
|
|
if (!cfg) {
|
|
std::cerr << "[BulletRegistry] Error: format invàlid a " << name << ".yaml\n";
|
|
return nullptr;
|
|
}
|
|
auto [it, _] = configs.insert_or_assign(name, *cfg);
|
|
return &it->second;
|
|
}
|
|
} // namespace
|
|
|
|
auto BulletRegistry::load() -> bool {
|
|
if (loadInto(configs, "bullet") == nullptr) {
|
|
return false;
|
|
}
|
|
loaded = true;
|
|
std::cout << "[BulletRegistry] Configuració de bala 'bullet' carregada.\n";
|
|
return true;
|
|
}
|
|
|
|
auto BulletRegistry::get() -> const BulletConfig& {
|
|
if (!loaded) {
|
|
std::cerr << "[BulletRegistry] FATAL: get() abans de load()\n";
|
|
std::exit(EXIT_FAILURE);
|
|
}
|
|
return configs.at("bullet");
|
|
}
|
|
|
|
auto BulletRegistry::get(const std::string& name) -> const BulletConfig* {
|
|
auto it = configs.find(name);
|
|
if (it != configs.end()) {
|
|
return &it->second;
|
|
}
|
|
return loadInto(configs, name);
|
|
}
|