osb/source/game/StarStatusEffectDatabase.cpp

55 lines
2.1 KiB
C++
Raw Normal View History

2023-06-20 04:33:09 +00:00
#include "StarStatusEffectDatabase.hpp"
#include "StarJsonExtra.hpp"
#include "StarRoot.hpp"
#include "StarAssets.hpp"
namespace Star {
StatusEffectDatabase::StatusEffectDatabase() {
auto assets = Root::singleton().assets();
2024-03-15 10:28:11 +00:00
auto& files = assets->scanExtension("statuseffect");
2023-06-20 04:33:09 +00:00
assets->queueJsons(files);
2024-03-15 10:28:11 +00:00
for (auto& file : files) {
2023-06-20 04:33:09 +00:00
auto uniqueEffect = parseUniqueEffect(assets->json(file), file);
if (m_uniqueEffects.contains(uniqueEffect.name))
throw StatusEffectDatabaseException::format(
2023-06-27 10:23:44 +00:00
"Duplicate stat effect named '{}', config file '{}'", uniqueEffect.name, file);
2023-06-20 04:33:09 +00:00
m_uniqueEffects[uniqueEffect.name] = uniqueEffect;
}
}
bool StatusEffectDatabase::isUniqueEffect(UniqueStatusEffect const& effect) const {
return m_uniqueEffects.contains(effect);
}
UniqueStatusEffectConfig StatusEffectDatabase::uniqueEffectConfig(UniqueStatusEffect const& effect) const {
if (auto uniqueEffect = m_uniqueEffects.maybe(effect))
return uniqueEffect.take();
2023-06-27 10:23:44 +00:00
throw StatusEffectDatabaseException::format("No such unique stat effect '{}'", effect);
2023-06-20 04:33:09 +00:00
}
UniqueStatusEffectConfig StatusEffectDatabase::parseUniqueEffect(Json const& config, String const& path) const {
try {
auto assets = Root::singleton().assets();
UniqueStatusEffectConfig effect;
effect.name = config.getString("name");
effect.blockingStat = config.optString("blockingStat");
effect.effectConfig = config.get("effectConfig", JsonObject());
effect.defaultDuration = config.getFloat("defaultDuration", 0.0f);
effect.scripts =
jsonToStringList(config.get("scripts", JsonArray{})).transformed(bind(&AssetPath::relativeTo, path, _1));
effect.scriptDelta = config.getUInt("scriptDelta", 1);
effect.animationConfig = config.optString("animationConfig").apply(bind(&AssetPath::relativeTo, path, _1));
effect.label = config.getString("label", "");
effect.description = config.getString("description", "");
effect.icon = config.optString("icon").apply(bind(&AssetPath::relativeTo, path, _1));
return effect;
} catch (std::exception const& e) {
throw StatusEffectDatabaseException("Error reading StatusEffect config", e);
}
}
}