From a1f4a7cfe05d68b738d54149633d414c8ca4a251 Mon Sep 17 00:00:00 2001 From: zhenyan121 <3367366583@qq.com> Date: Sat, 8 Aug 2026 21:19:00 +0800 Subject: [PATCH] feat(block): add block texture types and sounds Introduce BlockTextureType and BlockSound, parse texture and sound configs from block JSON, and add data() accessors to BlockManager. --- include/Cubed/gameplay/block.hpp | 20 +++++++ include/Cubed/gameplay/block_manager.hpp | 4 ++ src/gameplay/block_manager.cpp | 75 ++++++++++++++++++++++++ 3 files changed, 99 insertions(+) diff --git a/include/Cubed/gameplay/block.hpp b/include/Cubed/gameplay/block.hpp index 18c1053..32d5c2c 100644 --- a/include/Cubed/gameplay/block.hpp +++ b/include/Cubed/gameplay/block.hpp @@ -42,6 +42,18 @@ struct LookBlock { glm::ivec3 normal; }; +enum class BlockTextureType { + CUBOID, + CROSS + +}; + +struct BlockSound { + std::optional break_s; + std::optional place; + std::optional walk; +}; + struct BlockData { ResourceLocation name{}; BlockType id = 0; @@ -57,6 +69,14 @@ struct BlockData { bool is_blend = false; bool is_transitional = false; float roughness = 1.0f; + + BlockTextureType texture_type = BlockTextureType::CUBOID; + + std::optional texture_path; + std::optional normal; + + BlockSound sound; + BlockData() = default; }; diff --git a/include/Cubed/gameplay/block_manager.hpp b/include/Cubed/gameplay/block_manager.hpp index c29f6a4..977191d 100644 --- a/include/Cubed/gameplay/block_manager.hpp +++ b/include/Cubed/gameplay/block_manager.hpp @@ -27,6 +27,10 @@ public: static BlockType id_from_name(std::string_view name); static BlockType id_from_name(const ResourceLocation& name); + static const BlockData& data(std::string_view name); + static const BlockData& data(const ResourceLocation& location); + static const BlockData& data(BlockType type); + private: using BlockMap = tbb::concurrent_hash_map; using acc = BlockMap::accessor; diff --git a/src/gameplay/block_manager.cpp b/src/gameplay/block_manager.cpp index 689d9ef..e33575a 100644 --- a/src/gameplay/block_manager.cpp +++ b/src/gameplay/block_manager.cpp @@ -171,6 +171,7 @@ void BlockManager::init() { data.name.to_string()); continue; } + auto& properties = doc["properties"]; Tools::get_json_value(properties, "is_liquid", data.is_liquid); @@ -186,6 +187,49 @@ void BlockManager::init() { data.is_transitional); Tools::get_json_value(properties, "roughness", data.roughness); + if (doc.HasMember("texture") && doc["texture"].IsObject()) { + auto& texture = doc["texture"]; + std::string s; + if (Tools::get_json_value(texture, "type", s)) { + if (s == "cuboid") { + data.texture_type = BlockTextureType::CUBOID; + } else if (s == "cross") { + data.texture_type = BlockTextureType::CROSS; + } else { + Logger::error("Block {} texture type {} unknown", + data.name.to_string(), s); + ASSERT(false); + } + } else { + Logger::error("Block {} need texture type", + data.name.to_string()); + ASSERT(false); + continue; + } + + if (Tools::get_json_value(texture, "path", s)) { + data.texture_path = ResourceLocation::parse(s); + } + + if (Tools::get_json_value(texture, "normal", s)) { + data.normal = ResourceLocation::parse(s); + } + } + + if (doc.HasMember("sounds") && doc["sounds"].IsObject()) { + auto& sound = doc["sounds"]; + std::string s; + if (Tools::get_json_value(sound, "break", s)) { + data.sound.break_s = ResourceLocation::parse(s); + } + if (Tools::get_json_value(sound, "walk", s)) { + data.sound.walk = ResourceLocation::parse(s); + } + if (Tools::get_json_value(sound, "place", s)) { + data.sound.place = ResourceLocation::parse(s); + } + } + const auto LOCATION = data.name; const auto IS_CROSS_PLANE = data.is_cross_plane; const auto ID = data.id; @@ -231,6 +275,37 @@ BlockType BlockManager::id_from_name(const ResourceLocation& name) { return 0; } +const BlockData& BlockManager::data(std::string_view name) { + auto s = ResourceLocation::parse(name); + if (!s) { + return EMPTY; + } + + return data(*s); +} + +const BlockData& BlockManager::data(const ResourceLocation& location) { + + IDMap::const_accessor cacc; + if (!m_id_map.find(cacc, location)) { + Logger::error("Can't find block {} id", location.to_string()); + ASSERT(false); + return EMPTY; + } + return data(cacc->second); +} + +const BlockData& BlockManager::data(BlockType type) { + cacc c; + if (!m_datas.find(c, type)) { + Logger::error("Can't find block {} data", type); + ASSERT(false); + return EMPTY; + } + + return c->second; +} + void BlockManager::set_up_cross_plane_map( const std::vector>& types) { unsigned cur_id = 0;