Working on translations
This commit is contained in:
1 parent
9d8ca865c7
commit
e2d9105b02
18 files changed
+271
-88
No files matched your search
+29
-30
@@ -1,56 +1,55 @@
|
||||
#include "Item.h"
|
||||
#include "ItemRegistry.h"
|
||||
#include "external/nlohmann/json.hpp"
|
||||
#include <algorithm>
|
||||
#include <iostream>
|
||||
|
||||
namespace ZL {
|
||||
|
||||
void Inventory::addItem(const Item& item) {
|
||||
items.push_back(item);
|
||||
std::cout << "Item added to inventory: " << item.name << std::endl;
|
||||
if (onItemAdded) onItemAdded(item.id);
|
||||
void Inventory::addItem(const std::string& itemId) {
|
||||
itemIds.push_back(itemId);
|
||||
std::cout << "Item added to inventory: " << itemId << std::endl;
|
||||
if (onItemAdded) onItemAdded(itemId);
|
||||
}
|
||||
|
||||
void Inventory::removeItem(const std::string& itemId) {
|
||||
auto it = std::find_if(items.begin(), items.end(),
|
||||
[&itemId](const Item& item) { return item.id == itemId; });
|
||||
if (it != items.end()) {
|
||||
std::cout << "Item removed from inventory: " << it->name << std::endl;
|
||||
auto it = std::find(itemIds.begin(), itemIds.end(), itemId);
|
||||
if (it != itemIds.end()) {
|
||||
std::cout << "Item removed from inventory: " << itemId << std::endl;
|
||||
if (onItemRemoved) onItemRemoved(itemId);
|
||||
items.erase(it);
|
||||
itemIds.erase(it);
|
||||
}
|
||||
}
|
||||
|
||||
bool Inventory::hasItem(const std::string& itemId) const {
|
||||
return std::find_if(items.begin(), items.end(),
|
||||
[&itemId](const Item& item) { return item.id == itemId; }) != items.end();
|
||||
return std::find(itemIds.begin(), itemIds.end(), itemId) != itemIds.end();
|
||||
}
|
||||
|
||||
std::vector<Item> Inventory::getItems() const {
|
||||
std::vector<Item> result;
|
||||
result.reserve(itemIds.size());
|
||||
for (const auto& id : itemIds) {
|
||||
const Item* def = ItemRegistry::instance().findById(id);
|
||||
if (def) result.push_back(*def);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
void Inventory::save(nlohmann::json& out) const {
|
||||
nlohmann::json arr = nlohmann::json::array();
|
||||
for (const auto& item : items) {
|
||||
arr.push_back({
|
||||
{"id", item.id},
|
||||
{"name", item.name},
|
||||
{"description", item.description},
|
||||
{"icon", item.icon},
|
||||
{"selectedIcon", item.selectedIcon}
|
||||
});
|
||||
}
|
||||
out["items"] = std::move(arr);
|
||||
out["items"] = itemIds;
|
||||
}
|
||||
|
||||
void Inventory::load(const nlohmann::json& in) {
|
||||
items.clear();
|
||||
itemIds.clear();
|
||||
if (!in.contains("items") || !in["items"].is_array()) return;
|
||||
for (const auto& j : in["items"]) {
|
||||
Item item;
|
||||
item.id = j.value("id", "");
|
||||
item.name = j.value("name", "");
|
||||
item.description = j.value("description", "");
|
||||
item.icon = j.value("icon", "");
|
||||
item.selectedIcon = j.value("selectedIcon", "");
|
||||
items.push_back(std::move(item));
|
||||
if (j.is_string()) {
|
||||
itemIds.push_back(j.get<std::string>());
|
||||
} else if (j.is_object()) {
|
||||
// Backward-compat with old save files that stored full item structs.
|
||||
const std::string id = j.value("id", "");
|
||||
if (!id.empty()) itemIds.push_back(id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+9
-5
@@ -22,13 +22,14 @@ namespace ZL {
|
||||
|
||||
class Inventory : public ISaveable {
|
||||
public:
|
||||
std::vector<Item> items;
|
||||
void addItem(const Item& item);
|
||||
void addItem(const std::string& itemId);
|
||||
void removeItem(const std::string& itemId);
|
||||
const std::vector<Item>& getItems() const { return items; }
|
||||
// Resolves current items from ItemRegistry by id, so returned data
|
||||
// always reflects the currently loaded (localized) definitions.
|
||||
std::vector<Item> getItems() const;
|
||||
bool hasItem(const std::string& itemId) const;
|
||||
void clear() { items.clear(); }
|
||||
size_t getCount() const { return items.size(); }
|
||||
void clear() { itemIds.clear(); }
|
||||
size_t getCount() const { return itemIds.size(); }
|
||||
|
||||
void save(nlohmann::json& out) const override;
|
||||
void load(const nlohmann::json& in) override;
|
||||
@@ -36,6 +37,9 @@ namespace ZL {
|
||||
// Called whenever an item is added/removed; receives the item id.
|
||||
std::function<void(const std::string&)> onItemAdded;
|
||||
std::function<void(const std::string&)> onItemRemoved;
|
||||
|
||||
private:
|
||||
std::vector<std::string> itemIds;
|
||||
};
|
||||
|
||||
} // namespace ZL
|
||||
@@ -1,5 +1,6 @@
|
||||
#include "ItemRegistry.h"
|
||||
#include "utils/Utils.h"
|
||||
#include "Localization.h"
|
||||
#include "external/nlohmann/json.hpp"
|
||||
#include <iostream>
|
||||
|
||||
@@ -11,20 +12,9 @@ ItemRegistry& ItemRegistry::instance() {
|
||||
}
|
||||
|
||||
void ItemRegistry::loadFromJson(const std::string& jsonPath, const std::string& zipPath) {
|
||||
std::string content;
|
||||
try {
|
||||
if (zipPath.empty()) {
|
||||
content = readTextFile(jsonPath);
|
||||
} else {
|
||||
auto buf = readFileFromZIP(jsonPath, zipPath);
|
||||
if (buf.empty()) {
|
||||
std::cerr << "[ItemRegistry] Failed to read " << jsonPath << " from zip\n";
|
||||
return;
|
||||
}
|
||||
content.assign(buf.begin(), buf.end());
|
||||
}
|
||||
} catch (const std::exception& e) {
|
||||
std::cerr << "[ItemRegistry] Failed to open " << jsonPath << ": " << e.what() << "\n";
|
||||
const std::string content = readLocalizedConfigFile(jsonPath, zipPath);
|
||||
if (content.empty()) {
|
||||
std::cerr << "[ItemRegistry] Failed to read " << jsonPath << "\n";
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user