refactor(item): decouple items from block types

The item system now supports an ItemKind and property, allowing item JSON to declare a type instead of assuming every item is a block. BlockManager was moved to its own header, redesigned around concurrent hash maps, and exposes id_from_name(). Texture and UI code now key items by ItemID, while block placement resolves the block type from the item registry.
This commit is contained in:
2026-08-04 16:14:00 +08:00
parent 2358cfd9a4
commit 0259ca71ce
28 changed files with 436 additions and 323 deletions

View File

@@ -1,5 +1,6 @@
#include "Cubed/gameplay/item_manager.hpp"
#include "Cubed/gameplay/block_manager.hpp"
#include "Cubed/tools/cubed_assert.hpp"
#include "Cubed/tools/log.hpp"
@@ -63,27 +64,41 @@ void ItemManager::add(const std::filesystem::path& path) {
if (doc.HasMember("texture")) {
data.path = doc["texture"].GetString();
}
if (doc.HasMember("type")) {
std::string type = doc["type"].GetString();
data.kind = get_item_kind(type);
}
acc a;
if (m_map.emplace(a, data.id, std::move(data))) {
if (!m_id_map.emplace(a->second.name, a->first)) {
Logger::error("ItemManager: Can't Insterd {} {} to id map",
a->second.name, a->first);
}
if (a->second.kind == ItemKind::BLOCK) {
BlockType b = BlockManager::id_from_name(a->second.name);
m_block_to_id_map.emplace(b, a->first);
}
} else {
Logger::error("ItemManager: Can't Insterd {} to map", path.string());
}
}
const ItemData& ItemManager::get_item_data(std::string_view key) const {
IDMap::const_accessor cacc;
ItemID id = 0;
if (m_id_map.find(cacc, key)) {
id = cacc->second;
} else {
Logger::error("Can't Find key {} in id map", key);
ASSERT(false);
return EMPTY;
{
IDMap::const_accessor cacc;
if (m_id_map.find(cacc, key)) {
id = cacc->second;
} else {
Logger::error("Can't Find key {} in id map", key);
ASSERT(false);
return EMPTY;
}
}
return get_item_data(id);
}
const ItemData& ItemManager::get_item_data(ItemID id) const {
@@ -102,5 +117,5 @@ const ItemData& ItemManager::get(std::string_view key) {
const ItemData& ItemManager::get(ItemID id) {
return instance().get_item_data(id);
}
ItemID ItemManager::size() { return instance().m_map.size(); }
} // namespace Cubed