osb/source/game/StarPlayerTech.cpp

100 lines
2.7 KiB
C++
Raw Normal View History

2023-06-20 04:33:09 +00:00
#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"));
2024-05-28 14:44:34 +00:00
auto techDatabase = Root::singleton().techDatabase();
2024-05-29 01:31:54 +00:00
for (auto& p : json.getObject("equippedTechs")) {
String techName = p.second.toString();
if (techDatabase->contains(techName))
m_equippedTechs.set(TechTypeNames.getLeft(p.first), techName);
2024-05-28 14:44:34 +00:00
else
2024-05-29 01:31:54 +00:00
Logger::warn("Unequipping unknown tech '{}' from slot '{}'", techName, p.first);
2024-05-28 14:44:34 +00:00
}
2023-06-20 04:33:09 +00:00
}
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))
2023-06-27 10:23:44 +00:00
throw PlayerTechException::format("Enabling tech module '{}' when not available", techModule);
2023-06-20 04:33:09 +00:00
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))
2023-06-27 10:23:44 +00:00
throw PlayerTechException::format("Equipping tech module '{}' when not enabled", techModule);
2023-06-20 04:33:09 +00:00
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();
}
}