osb/source/game/StarPlayerTech.cpp
2024-05-28 10:44:34 -04:00

99 lines
2.7 KiB
C++

#include "StarPlayerTech.hpp"
#include "StarJsonExtra.hpp"
#include "StarRoot.hpp"
namespace Star {
PlayerTech::PlayerTech() {}
PlayerTech::PlayerTech(Json const& json) {
m_availableTechs = jsonToStringSet(json.get("availableTechs"));
m_enabledTechs = jsonToStringSet(json.get("enabledTechs"));
auto techDatabase = Root::singleton().techDatabase();
for (auto p : json.get("equippedTechs", JsonObject()).iterateObject()) {
if (techDatabase->contains(p.second.toString()))
m_equippedTechs.set(TechTypeNames.getLeft(p.first), p.second.toString());
else
Logger::warn("Missing tech '%s' in slot '%s'", p.second.toString(), p.first);
}
}
Json PlayerTech::toJson() const {
return JsonObject{
{"availableTechs", jsonFromStringSet(m_availableTechs)},
{"enabledTechs", jsonFromStringSet(m_enabledTechs)},
{"equippedTechs", jsonFromMapK(m_equippedTechs, [](TechType t) {
return TechTypeNames.getRight(t);
})},
};
}
bool PlayerTech::isAvailable(String const& techModule) const {
return m_availableTechs.contains(techModule);
}
void PlayerTech::makeAvailable(String const& techModule) {
m_availableTechs.add(techModule);
}
void PlayerTech::makeUnavailable(String const& techModule) {
disable(techModule);
m_availableTechs.remove(techModule);
}
bool PlayerTech::isEnabled(String const& techModule) const {
return m_enabledTechs.contains(techModule);
}
void PlayerTech::enable(String const& techModule) {
if (!m_availableTechs.contains(techModule))
throw PlayerTechException::format("Enabling tech module '{}' when not available", techModule);
m_enabledTechs.add(techModule);
}
void PlayerTech::disable(String const& techModule) {
unequip(techModule);
m_enabledTechs.remove(techModule);
}
bool PlayerTech::isEquipped(String const& techModule) const {
for (auto t : m_equippedTechs.keys()) {
if (m_equippedTechs.get(t) == techModule)
return true;
}
return false;
}
void PlayerTech::equip(String const& techModule) {
if (!m_enabledTechs.contains(techModule))
throw PlayerTechException::format("Equipping tech module '{}' when not enabled", techModule);
auto techDatabase = Root::singleton().techDatabase();
m_equippedTechs[techDatabase->tech(techModule).type] = techModule;
}
void PlayerTech::unequip(String const& techModule) {
for (auto t : m_equippedTechs.keys()) {
if (m_equippedTechs.get(t) == techModule)
m_equippedTechs.remove(t);
}
}
StringSet const& PlayerTech::availableTechs() const {
return m_availableTechs;
}
StringSet const& PlayerTech::enabledTechs() const {
return m_enabledTechs;
}
HashMap<TechType, String> const& PlayerTech::equippedTechs() const {
return m_equippedTechs;
}
StringList PlayerTech::techModules() const {
return m_equippedTechs.values();
}
}