osb/source/game/StarInventoryTypes.cpp

92 lines
2.7 KiB
C++
Raw Normal View History

2023-06-20 04:33:09 +00:00
#include "StarInventoryTypes.hpp"
#include "StarFormat.hpp"
#include "StarJsonExtra.hpp"
namespace Star {
EnumMap<EquipmentSlot> const EquipmentSlotNames{
{EquipmentSlot::Head, "head"},
{EquipmentSlot::Chest, "chest"},
{EquipmentSlot::Legs, "legs"},
{EquipmentSlot::Back, "back"},
{EquipmentSlot::HeadCosmetic, "headCosmetic"},
{EquipmentSlot::ChestCosmetic, "chestCosmetic"},
{EquipmentSlot::LegsCosmetic, "legsCosmetic"},
{EquipmentSlot::BackCosmetic, "backCosmetic"}
};
InventorySlot jsonToInventorySlot(Json const& json) {
String type = json.getString("type");
Json location = json.get("location", Json());
if (type == "equipment")
return EquipmentSlotNames.getLeft(location.toString());
else if (type == "swap")
return SwapSlot();
else if (type == "trash")
return TrashSlot();
else
return BagSlot(type, location.toUInt());
}
Json jsonFromInventorySlot(InventorySlot const& slot) {
if (slot.is<EquipmentSlot>()) {
return JsonObject{
{"type", "equipment"},
{"location", EquipmentSlotNames.getRight(slot.get<EquipmentSlot>())}
};
} else if (slot.is<SwapSlot>()) {
return JsonObject{{"type", "swap"}};
} else if (slot.is<TrashSlot>()) {
return JsonObject{{"type", "trash"}};
} else {
auto bagSlot = slot.get<BagSlot>();
return JsonObject{
{"type", bagSlot.first},
{"location", bagSlot.second}
};
}
}
std::ostream& operator<<(std::ostream& ostream, InventorySlot const& slot) {
Json json = jsonFromInventorySlot(slot);
String type = json.getString("type");
Json location = json.get("location", {});
if (location.isNull())
2023-06-27 10:23:44 +00:00
format(ostream, "InventorySlot{type: {}}", type);
2023-06-20 04:33:09 +00:00
if (location.isType(Json::Type::String))
2023-06-27 10:23:44 +00:00
format(ostream, "InventorySlot{type: {}, location: {}}", type, location.toString());
2023-06-20 04:33:09 +00:00
else
2023-06-27 10:23:44 +00:00
format(ostream, "InventorySlot{type: {}, location: {}}", type, location.toInt());
2023-06-20 04:33:09 +00:00
return ostream;
}
EnumMap<EssentialItem> const EssentialItemNames{
{EssentialItem::BeamAxe, "beamaxe"},
{EssentialItem::WireTool, "wiretool"},
{EssentialItem::PaintTool, "painttool"},
{EssentialItem::InspectionTool, "inspectiontool"}
};
SelectedActionBarLocation jsonToSelectedActionBarLocation(Json const& json) {
if (json.isType(Json::Type::String))
return EssentialItemNames.getLeft(json.toString());
else if (json.isNull())
return SelectedActionBarLocation();
else
return (CustomBarIndex)json.toUInt();
}
Json jsonFromSelectedActionBarLocation(SelectedActionBarLocation const& location) {
if (location.is<CustomBarIndex>())
return location.get<CustomBarIndex>();
else if (location.is<EssentialItem>())
return EssentialItemNames.getRight(location.get<EssentialItem>());
else
return Json();
}
}