osb/source/game/StarDamageDatabase.cpp

70 lines
2.2 KiB
C++
Raw Permalink Normal View History

2023-06-20 04:33:09 +00:00
#include "StarDamageDatabase.hpp"
#include "StarRandom.hpp"
#include "StarAssets.hpp"
#include "StarRoot.hpp"
namespace Star {
DamageDatabase::DamageDatabase() {
auto assets = Root::singleton().assets();
auto elementalConfig = assets->json("/damage/elementaltypes.config");
for (auto p : elementalConfig.iterateObject()) {
ElementalType type;
type.resistanceStat = p.second.getString("resistanceStat");
for (auto particle : p.second.getObject("damageNumberParticles")) {
type.damageNumberParticles.set(HitTypeNames.getLeft(particle.first), particle.second.toString());
}
m_elementalTypes.set(p.first, std::move(type));
2023-06-20 04:33:09 +00:00
}
2024-03-15 10:28:11 +00:00
auto& files = assets->scanExtension("damage");
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 config = assets->json(file);
String name = config.getString("kind");
if (m_damageKinds.contains(name))
2023-06-27 10:23:44 +00:00
throw StarException(strf("Duplicate damage kind Name {}. configfile {}", name, file));
2023-06-20 04:33:09 +00:00
DamageKind kind;
kind.name = name;
for (auto effect : config.getObject("effects", JsonObject())) {
TargetMaterial material = effect.first;
kind.effects.set(material, {});
for (auto hit : effect.second.toObject()) {
DamageEffect effect = DamageEffect {
hit.second.get("sounds", JsonArray()),
hit.second.get("particles", JsonArray())
};
kind.effects[material].set(HitTypeNames.getLeft(hit.first), effect);
}
}
kind.elementalType = config.getString("elementalType", "default");
if (!m_elementalTypes.contains(kind.elementalType))
2023-06-27 10:23:44 +00:00
throw StarException(strf("Undefined elemental type {} in damage kind {}", kind.elementalType, name));
2023-06-20 04:33:09 +00:00
m_damageKinds.set(name, std::move(kind));
2023-06-20 04:33:09 +00:00
}
}
DamageKind const& DamageDatabase::damageKind(String kind) const {
if (kind.empty())
kind = "default";
else
kind = kind.toLower();
if (!m_damageKinds.contains(kind))
2023-06-27 10:23:44 +00:00
throw StarException(strf("Unknown damage definition with kind '{}'.", kind));
2023-06-20 04:33:09 +00:00
return m_damageKinds.get(kind);
}
ElementalType const& DamageDatabase::elementalType(String const& name) const {
if (!m_damageKinds.contains(name))
2023-06-27 10:23:44 +00:00
throw StarException(strf("Unknown elemental type with name '{}'.", name));
2023-06-20 04:33:09 +00:00
return m_elementalTypes.get(name);
}
}